private static void VacationSetDatesToTargetEntries([NotNull] Random rnd,
                                                            [ItemNotNull][NotNull] List <VacationEntry> targetVacationEntries,
                                                            [NotNull] DateBasedProfile dbp)
        {
            //normalize to first entry for each day
            var year           = DateTime.Now.Year;
            var probabilityarr = dbp.GetValueArray(new DateTime(year, 1, 1), new DateTime(year + 1, 1, 1), new TimeSpan(1, 0, 0, 0));

            foreach (var targetVacationEntry in targetVacationEntries)
            {
                var ranges = VacationGetProbabilityRanges(probabilityarr, year);
                //first find the proper ranges
                var possibleRanges = ranges.Where(x =>
                                                  x.DurationInDays >= targetVacationEntry.TargetDuration &&
                                                  Math.Abs(x.Probability - targetVacationEntry.TargetProbability) < Constants.Ebsilon).ToList();
                if (possibleRanges.Count == 0)
                {
                    var probabilities = targetVacationEntries.Select(x => x.TargetProbability).Distinct().OrderByDescending(x => x).ToList();
                    var pbtoTry       = 0;
                    while (possibleRanges.Count == 0 && pbtoTry < probabilities.Count)
                    {
                        //if nothing is available, use all probability ranges
                        possibleRanges = ranges.Where(x =>
                                                      x.DurationInDays >= targetVacationEntry.TargetDuration &&
                                                      Math.Abs(x.Probability - probabilities[pbtoTry]) < Constants.Ebsilon).ToList();
                        pbtoTry++;
                    }
                }

                if (possibleRanges.Count == 0)
                {
                    throw new LPGException("Could not find any suitable probability range to allocate the vacation with a duration of " +
                                           targetVacationEntry.TargetDuration);
                }

                //pick a range
                var rangeidx = rnd.Next(possibleRanges.Count);
                var vpr      = possibleRanges[rangeidx];
                var vacationStartOffsetRange = (int)(vpr.DurationInDays - targetVacationEntry.TargetDuration);
                if (vacationStartOffsetRange < 0)
                {
                    throw new LPGException("Negative duration? Bug! Please report.");
                }

                var vacationStartOffset = rnd.Next(vacationStartOffsetRange);
                targetVacationEntry.StartDate = vpr.Start.AddDays(vacationStartOffset);
                targetVacationEntry.EndDate   = targetVacationEntry.StartDate.AddDays(targetVacationEntry.TargetDuration);
                var startidx = targetVacationEntry.StartDate.DayOfYear;
                var endidx   = targetVacationEntry.EndDate.DayOfYear;
                for (var i = startidx; i < endidx; i++)
                {
                    probabilityarr[i] = 0;
                }
            }
        }
Ejemplo n.º 2
0
 public Generator([NotNull] string pName, [CanBeNull] string description, [CanBeNull] VLoadType loadType, double scalingFactor,
                  [CanBeNull] DateBasedProfile dateBasedProfile, [NotNull] string connectionString, StrGuid guid, [CanBeNull] int?pID = null)
     : base(pName, TableName, connectionString, guid)
 {
     ID = pID;
     TypeDescription   = "Generator";
     _description      = description;
     _loadType         = loadType;
     _scalingFactor    = scalingFactor;
     _dateBasedProfile = dateBasedProfile;
 }
 public HouseholdTemplate([NotNull] string pName, [CanBeNull] int?id, [CanBeNull] string description, [NotNull] string connectionString, [CanBeNull] string newName, int count,
                          [CanBeNull] DateBasedProfile timeProfileForVacations, TemplateVacationType templateVacationType, int minNumberOfVacations, int maxNumberOfVacations,
                          int averageVacationDuration, int minTotalVacationDays, int maxTotalVacationDays, StrGuid guid) : base(pName, TableName, connectionString, guid)
 {
     _templateVacationType    = templateVacationType;
     _minNumberOfVacations    = minNumberOfVacations;
     _maxNumberOfVacations    = maxNumberOfVacations;
     _averageVacationDuration = averageVacationDuration;
     _minTotalVacationDays    = minTotalVacationDays;
     _maxTotalVacationDays    = maxTotalVacationDays;
     _timeProfileForVacations = timeProfileForVacations;
     ID              = id;
     _count          = count;
     _newHHName      = newName;
     TypeDescription = "Household Template";
     _description    = description;
 }
Ejemplo n.º 4
0
        public void TimeLimitArrayTestDateProfileTest()
        {
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                db.ClearTable(TimeLimit.TableName);
                db.ClearTable(TimeLimitEntry.TableName);
                // test if night + day is always
                var dateBasedProfile = new DateBasedProfile("blub", "bla", db.ConnectionString, Guid.NewGuid().ToStrGuid());
                dateBasedProfile.SaveToDB();
                dateBasedProfile.AddNewDatePoint(new DateTime(2014, 1, 1), 100);
                dateBasedProfile.AddNewDatePoint(new DateTime(2014, 1, 3), 0);
                dateBasedProfile.SaveToDB();
                var start   = new DateTime(1900, 1, 1, 0, 0, 0);
                var end     = new DateTime(1900, 1, 1, 1, 1, 1);
                var dt      = new TimeLimit("blub", db.ConnectionString, Guid.NewGuid().ToStrGuid(), 1);
                var temp    = new TemperatureProfile("blub", 1, "bla", string.Empty, Guid.NewGuid().ToStrGuid());
                var enddate = new DateTime(1900, 1, 2, 0, 0, 0);
                temp.AddTemperature(start, 0, 1, true, false);
                temp.AddTemperature(enddate, 10, 1, true, false);
                var dateBasedProfiles = new ObservableCollection <DateBasedProfile>();
                dt.AddTimeLimitEntry(null, start, end, PermissionMode.ControlledByDateProfile, 1, 1, true, true, true, true,
                                     true, true, true, 1, 1, 1, 1, 1, 1, 1, true, false, AnyAllTimeLimitCondition.Any, start, end,
                                     dateBasedProfile.ID, 150, 50, false, false, true, false, 5, false, dateBasedProfiles, 0, 0, 0);
                dateBasedProfiles.Add(dateBasedProfile);
                var geoloc             = new GeographicLocation(db.ConnectionString, null, Guid.NewGuid().ToStrGuid());
                var r                  = new Random();
                var vacationTimeFrames = new List <VacationTimeframe>();
                var br                 = dt.TimeLimitEntries[0].GetOneYearHourArray(temp, geoloc, r, vacationTimeFrames, "test",
                                                                                    out _);

                for (var i = 0; i < br.Count; i++)
                {
                    if (i < 48)
                    {
                        (br[i]).Should().Be(true);
                    }
                    else
                    {
                        (br[i]).Should().Be(false);
                    }
                }
                db.Cleanup();
            }
        }
 public void DateBasedProfileSaveAndRestore()
 {
     using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
     {
         db.ClearTable(DateBasedProfile.TableName);
         db.ClearTable(DateProfileDataPoint.TableName);
         var profiles = new ObservableCollection <DateBasedProfile>();
         DateBasedProfile.LoadFromDatabase(profiles, db.ConnectionString, false);
         profiles.Count.Should().Be(0);
         var dbp = new DateBasedProfile("tempprofil1", "desc1", db.ConnectionString, Guid.NewGuid().ToStrGuid());
         dbp.SaveToDB();
         dbp.AddNewDatePoint(new DateTime(2014, 1, 1), 15);
         dbp.AddNewDatePoint(new DateTime(2014, 2, 1), 15);
         dbp.SaveToDB();
         DateBasedProfile.LoadFromDatabase(profiles, db.ConnectionString, false);
         profiles.Count.Should().Be(1);
         profiles[0].Datapoints.Count.Should().Be(2);
         profiles[0].DeleteDatePoint(profiles[0].Datapoints[0]);
         profiles[0].Datapoints.Count.Should().Be(1);
         db.Cleanup();
     }
 }
Ejemplo n.º 6
0
 public DateBasedProfilePresenter([NotNull] ApplicationPresenter applicationPresenter, [NotNull] DateBasedProfileView view,
                                  [NotNull] DateBasedProfile tp) : base(view, "ThisProfile.HeaderString", tp, applicationPresenter)
 {
     _dbp         = tp;
     _csvImporter = new CSVImporter(true);
 }
Ejemplo n.º 7
0
        public bool Import([NotNull] CsvImportOptions calcDirectoryOptions, [CanBeNull] out DateBasedProfile newProfile)
        {
            _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            string csvFilename = calcDirectoryOptions.Input;

            newProfile = null;
            if (csvFilename == null)
            {
                Logger.Error("No file was set.");
                return(false);
            }

            if (!File.Exists(csvFilename))
            {
                Logger.Error("File does not exist");
                return(false);
            }

            if (calcDirectoryOptions.Delimiter == null)
            {
                Logger.Error("No delimiter set.");
                return(false);
            }
            Logger.Info("Loading...");
            CSVImporter ci = new CSVImporter(true)
            {
                FileName        = csvFilename,
                TimeColumn      = 1,
                HeaderLineCount = 1,
                Separator       = calcDirectoryOptions.Delimiter[0],
                Column          = 2
            };

            ci.RefreshEntries();
            if (ci.Entries.Count == 0)
            {
                throw new LPGException("Not a single entry was found");
            }
            Logger.Info("PreviewText from the import:");
            Logger.Info(ci.PreviewText, true);
            //asd

            var sim = new Simulator(_connectionString);

            Logger.Info("Loading finished.");

            Logger.Info("Importing " + ci.Entries.Count + " datapoints...");
            var dbp = sim.DateBasedProfiles.CreateNewItem(sim.ConnectionString);

            for (var i = 0; i < ci.Entries.Count; i++)
            {
                var ce   = ci.Entries[i];
                var date = ce.Time;
                dbp.AddNewDatePoint(date, ce.Value, false);
            }
            dbp.Datapoints.Sort();
            dbp.SaveToDB();
            newProfile = dbp;
            Logger.Info("Imported all data points.");

            _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
            return(true);
        }
Ejemplo n.º 8
0
        private List <LoadingEntry> GetLoadingActions(bool ignoreMissingTables)
        {
            var actions = new List <LoadingEntry>
            {
                new LoadingEntry("Loadtypes",
                                 () => VLoadType.LoadFromDatabase(LoadTypes.MyItems, ConnectionString, ignoreMissingTables),
                                 LoadTypes),
                new LoadingEntry("Holidays",
                                 () => Holiday.LoadFromDatabase(Holidays.MyItems, ConnectionString, ignoreMissingTables), Holidays),
                new LoadingEntry("Variables",
                                 () => Variable.LoadFromDatabase(Variables.It, ConnectionString, ignoreMissingTables), Variables),

                new LoadingEntry("Date Based Profiles",
                                 () => DateBasedProfile.LoadFromDatabase(DateBasedProfiles.MyItems, ConnectionString,
                                                                         ignoreMissingTables), DateBasedProfiles),
                new LoadingEntry("Vacations",
                                 () => Vacation.LoadFromDatabase(Vacations.MyItems, ConnectionString, ignoreMissingTables),
                                 Vacations),
                new LoadingEntry("Desires",
                                 () => Desire.LoadFromDatabase(Desires.MyItems, ConnectionString, ignoreMissingTables), Desires),
                new LoadingEntry("Time Profiles",
                                 () => TimeBasedProfile.LoadFromDatabase(Timeprofiles.MyItems, ConnectionString, ignoreMissingTables),
                                 Timeprofiles),
                new LoadingEntry("Temperature Profiles",
                                 () => TemperatureProfile.LoadFromDatabase(TemperatureProfiles.MyItems, ConnectionString,
                                                                           ignoreMissingTables), TemperatureProfiles),
                new LoadingEntry("Generators",
                                 () => Generator.LoadFromDatabase(Generators.MyItems, ConnectionString, LoadTypes.MyItems,
                                                                  DateBasedProfiles.MyItems, ignoreMissingTables), Generators),
                new LoadingEntry("Energy Storages",
                                 () => EnergyStorage.LoadFromDatabase(EnergyStorages.MyItems, ConnectionString, LoadTypes.MyItems, Variables.MyItems,
                                                                      ignoreMissingTables), EnergyStorages),
                new LoadingEntry("Transformation Devices",
                                 () => TransformationDevice.LoadFromDatabase(TransformationDevices.MyItems, ConnectionString,
                                                                             LoadTypes.MyItems, Variables.MyItems, ignoreMissingTables), TransformationDevices),
                new LoadingEntry("Device Categories", () =>
                {
                    DeviceCategory.LoadFromDatabase(DeviceCategories.MyItems, out _dcnone, ConnectionString,
                                                    RealDevices.MyItems, ignoreMissingTables);
                    DeviceCategories.DeviceCategoryNone = _dcnone;
                }, DeviceCategories),

                new LoadingEntry("Real Devices",
                                 () => RealDevice.LoadFromDatabase(RealDevices.MyItems, DeviceCategories.MyItems,
                                                                   DeviceCategories.DeviceCategoryNone, ConnectionString, LoadTypes.MyItems, Timeprofiles.MyItems,
                                                                   ignoreMissingTables), RealDevices),
                new LoadingEntry("Device Action Groups",
                                 () => DeviceActionGroup.LoadFromDatabase(DeviceActionGroups.MyItems, ConnectionString,
                                                                          ignoreMissingTables), DeviceActionGroups),
                new LoadingEntry("Device Actions",
                                 () => DeviceAction.LoadFromDatabase(DeviceActions.MyItems, ConnectionString, Timeprofiles.MyItems,
                                                                     RealDevices.MyItems, LoadTypes.MyItems, DeviceActionGroups.MyItems, ignoreMissingTables),
                                 DeviceActions),
                new LoadingEntry("Device Tagging Sets",
                                 () => DeviceTaggingSet.LoadFromDatabase(DeviceTaggingSets.MyItems, ConnectionString,
                                                                         ignoreMissingTables, RealDevices.MyItems, LoadTypes.It), DeviceTaggingSets),
                new LoadingEntry("Persons",
                                 () => Person.LoadFromDatabase(Persons.MyItems, ConnectionString,
                                                               ignoreMissingTables), Persons),
                new LoadingEntry("Locations",
                                 () => Location.LoadFromDatabase(Locations.MyItems, ConnectionString, RealDevices.MyItems,
                                                                 DeviceCategories.MyItems, LoadTypes.MyItems, ignoreMissingTables), Locations),
                new LoadingEntry("Time Limits",
                                 () => TimeLimit.LoadFromDatabase(TimeLimits.MyItems, DateBasedProfiles.MyItems, ConnectionString,
                                                                  ignoreMissingTables), TimeLimits),
                new LoadingEntry("Geographic Locations",
                                 () => GeographicLocation.LoadFromDatabase(GeographicLocations.MyItems, ConnectionString,
                                                                           Holidays.MyItems, TimeLimits.MyItems, ignoreMissingTables), GeographicLocations),
                new LoadingEntry("Subaffordances",
                                 () => SubAffordance.LoadFromDatabase(SubAffordances.MyItems, ConnectionString, Desires.MyItems,
                                                                      ignoreMissingTables, Locations.It, Variables.It), SubAffordances),
                new LoadingEntry("Affordances",
                                 () => Affordance.LoadFromDatabase(Affordances.MyItems, ConnectionString, Timeprofiles.MyItems,
                                                                   DeviceCategories.MyItems, RealDevices.MyItems, Desires.MyItems, SubAffordances.MyItems,
                                                                   LoadTypes.MyItems, TimeLimits.MyItems, DeviceActions.MyItems, DeviceActionGroups.MyItems,
                                                                   Locations.It, ignoreMissingTables, Variables.It), Affordances),
                new LoadingEntry("Affordance Tagging Sets",
                                 () => AffordanceTaggingSet.LoadFromDatabase(AffordanceTaggingSets.MyItems, ConnectionString,
                                                                             ignoreMissingTables, Affordances.MyItems, LoadTypes.It), AffordanceTaggingSets),
                new LoadingEntry("Trait Tags",
                                 () => TraitTag.LoadFromDatabase(TraitTags.MyItems, ConnectionString, ignoreMissingTables),
                                 TraitTags),
                new LoadingEntry("Household Traits",
                                 () => HouseholdTrait.LoadFromDatabase(HouseholdTraits.MyItems, ConnectionString, Locations.MyItems,
                                                                       Affordances.MyItems, RealDevices.MyItems, DeviceCategories.MyItems, Timeprofiles.MyItems,
                                                                       LoadTypes.MyItems, TimeLimits.MyItems, Desires.MyItems, DeviceActions.It, DeviceActionGroups.It,
                                                                       TraitTags.It, ignoreMissingTables, Variables.It), HouseholdTraits),
                new LoadingEntry("Device Selections",
                                 () => DeviceSelection.LoadFromDatabase(DeviceSelections.MyItems, ConnectionString,
                                                                        DeviceCategories.MyItems, RealDevices.MyItems, DeviceActions.It, DeviceActionGroups.It,
                                                                        ignoreMissingTables), DeviceSelections),
                new LoadingEntry("Household Tags",
                                 () => HouseholdTag.LoadFromDatabase(HouseholdTags.It, ConnectionString, ignoreMissingTables),
                                 HouseholdTags),
                new LoadingEntry("Modular Households",
                                 () => ModularHousehold.LoadFromDatabase(ModularHouseholds.MyItems, ConnectionString,
                                                                         HouseholdTraits.MyItems, DeviceSelections.MyItems, ignoreMissingTables, Persons.MyItems,
                                                                         Vacations.It, HouseholdTags.It, TraitTags.It), ModularHouseholds),
                new LoadingEntry("Household Templates",
                                 () => HouseholdTemplate.LoadFromDatabase(HouseholdTemplates.MyItems, ConnectionString,
                                                                          HouseholdTraits.MyItems, ignoreMissingTables, Persons.MyItems, TraitTags.It, Vacations.It,
                                                                          HouseholdTags.It, DateBasedProfiles.It), HouseholdTemplates),
                new LoadingEntry("Template Persons",
                                 () => TemplatePerson.LoadFromDatabase(TemplatePersons.It, ConnectionString, HouseholdTraits.It,
                                                                       ignoreMissingTables, ModularHouseholds.It, Persons.It), TemplatePersons),
                new LoadingEntry("Household Plans",
                                 () => HouseholdPlan.LoadFromDatabase(HouseholdPlans.MyItems, ConnectionString, ignoreMissingTables,
                                                                      Persons.MyItems, AffordanceTaggingSets.MyItems, ModularHouseholds.MyItems),
                                 HouseholdPlans),
                new LoadingEntry("House Types",
                                 () => HouseType.LoadFromDatabase(HouseTypes.MyItems, ConnectionString, RealDevices.MyItems,
                                                                  DeviceCategories.MyItems, Timeprofiles.MyItems, TimeLimits.MyItems, LoadTypes.MyItems,
                                                                  TransformationDevices.MyItems, EnergyStorages.MyItems, Generators.MyItems, ignoreMissingTables,
                                                                  Locations.MyItems, DeviceActions.It, DeviceActionGroups.It, Variables.It), HouseTypes),
                new LoadingEntry("Transportation Device Categories",
                                 () => TransportationDeviceCategory.LoadFromDatabase(TransportationDeviceCategories.It, ConnectionString,
                                                                                     ignoreMissingTables), TransportationDeviceCategories),
                new LoadingEntry("Sites",
                                 () => Site.LoadFromDatabase(Sites.It,
                                                             ConnectionString, ignoreMissingTables,
                                                             Locations.It), Sites),
                new LoadingEntry("Transportation Devices",
                                 () => TransportationDevice.LoadFromDatabase(TransportationDevices.It, ConnectionString, ignoreMissingTables,
                                                                             TransportationDeviceCategories.It, LoadTypes.It), TransportationDevices),

                new LoadingEntry("Transportation Device Sets",
                                 () => TransportationDeviceSet.LoadFromDatabase(TransportationDeviceSets.It, ConnectionString,
                                                                                ignoreMissingTables, TransportationDevices.It), TransportationDeviceSets),

                new LoadingEntry("Travel Routes",
                                 () => TravelRoute.LoadFromDatabase(TravelRoutes.It, ConnectionString, ignoreMissingTables,
                                                                    TransportationDeviceCategories.It, Sites.It), TravelRoutes),

                new LoadingEntry("Travel Route Sets",
                                 () => TravelRouteSet.LoadFromDatabase(TravelRouteSets.It, ConnectionString, ignoreMissingTables,
                                                                       TravelRoutes.It), TravelRouteSets),
                new LoadingEntry("Charging Station Sets",
                                 () => ChargingStationSet.LoadFromDatabase(ChargingStationSets.It,
                                                                           ConnectionString, ignoreMissingTables,
                                                                           LoadTypes.It, TransportationDeviceCategories.It, Sites.It), ChargingStationSets),

                new LoadingEntry("Houses",
                                 () => House.LoadFromDatabase(Houses.MyItems, ConnectionString, TemperatureProfiles.MyItems,
                                                              GeographicLocations.MyItems, HouseTypes.MyItems,
                                                              ModularHouseholds.MyItems, ChargingStationSets.MyItems,
                                                              TransportationDeviceSets.MyItems, TravelRouteSets.MyItems,
                                                              ignoreMissingTables), Houses),
                new LoadingEntry("Settlements",
                                 () => Settlement.LoadFromDatabase(Settlements.MyItems, ConnectionString,
                                                                   TemperatureProfiles.MyItems, GeographicLocations.MyItems, ModularHouseholds.MyItems, Houses.MyItems,
                                                                   ignoreMissingTables), Settlements),
                new LoadingEntry("Settlement Templates",
                                 () => SettlementTemplate.LoadFromDatabase(SettlementTemplates.It, ConnectionString,
                                                                           HouseholdTemplates.It, HouseTypes.It, ignoreMissingTables, TemperatureProfiles.It,
                                                                           GeographicLocations.It, HouseholdTags.It, HouseholdTraits.It), SettlementTemplates),
                new LoadingEntry("Settings",
                                 () => MyGeneralConfig = GeneralConfig.LoadFromDatabase(ConnectionString, ignoreMissingTables), null),
                new LoadingEntry("Calculation Outcomes",
                                 () => CalculationOutcome.LoadFromDatabase(CalculationOutcomes.MyItems, ConnectionString,
                                                                           ignoreMissingTables), CalculationOutcomes)
            };

            return(actions);
        }