public void PickRandomAffordanceFromEquallyAttractiveOnesTest() { using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) { CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults(); calcParameters.AffordanceRepetitionCount = 0; wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "blub", wd.InputDataLogger)) { fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general", HouseholdKeyType.General, "desc", null, null); //SqlResultLoggingService srls = new SqlResultLoggingService(wd.WorkingDirectory); //DateStampCreator dsc = new DateStampCreator(calcParameters); Random rnd = new Random(); //OnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calcParameters); using (var lf = new LogFile(calcParameters, fft)) { CalcProfile cp = new CalcProfile("cp1", Guid.NewGuid().ToStrGuid(), TimeSpan.FromMinutes(1), ProfileType.Absolute, "bla"); CalcVariableRepository crv = new CalcVariableRepository(); BitArray isBusy = new BitArray(calcParameters.InternalTimesteps, false); using CalcRepo calcRepo = new CalcRepo(lf: lf, calcParameters: calcParameters, rnd: rnd); CalcAffordance aff1 = new CalcAffordance("aff1", cp, null, false, new List <CalcDesire>(), 10, 20, PermittedGender.All, true, 1, LPGColors.AliceBlue, null, false, false, null, null, ActionAfterInterruption.GoBackToOld, "", 900, false, "", Guid.NewGuid().ToStrGuid(), crv, new List <CalcAffordance.DeviceEnergyProfileTuple>(), isBusy, BodilyActivityLevel.Low, calcRepo); CalcAffordance aff2 = new CalcAffordance("aff2", cp, null, false, new List <CalcDesire>(), 10, 20, PermittedGender.All, true, 1, LPGColors.AliceBlue, null, false, false, null, null, ActionAfterInterruption.GoBackToOld, "", 100, false, "", Guid.NewGuid().ToStrGuid(), crv, new List <CalcAffordance.DeviceEnergyProfileTuple>(), isBusy, BodilyActivityLevel.Low, calcRepo); List <ICalcAffordanceBase> affs = new List <ICalcAffordanceBase> { aff1, aff2 }; int aff1C = 0; int aff2C = 0; BitArray isSick = new BitArray(calcParameters.InternalTimesteps); BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps); CalcPersonDto calcPerson = CalcPersonDto.MakeExamplePerson(); CalcPerson cperson = new CalcPerson(calcPerson, null, isSick, isOnVacation, calcRepo); TimeStep ts = new TimeStep(1, 0, true); for (int i = 0; i < 1000; i++) { ICalcAffordanceBase cab = cperson.PickRandomAffordanceFromEquallyAttractiveOnes(affs, ts, null, new HouseholdKey("bla")); if (cab == aff1) { aff1C++; } if (cab == aff2) { aff2C++; } } aff1C.Should().BeApproximatelyWithinPercent(900, 0.1); Logger.Info("Number of selections for 90%:" + aff1C + ", 10%:" + aff2C); } } wd.CleanUp(); } }
private static void AddTraitDesiresToOnePerson([NotNull][ItemNotNull] List <ModularHousehold.PersonTraitDesireEntry> traitDesires, [NotNull] CalcPersonDto calcPerson) { foreach (var personTraitDesireEntry in traitDesires) { switch (personTraitDesireEntry.AssignType) { case ModularHouseholdTrait.ModularHouseholdTraitAssignType.Age: if ((calcPerson.Gender == personTraitDesireEntry.HHTDesire.Gender || personTraitDesireEntry.HHTDesire.Gender == PermittedGender.All) && calcPerson.Age >= personTraitDesireEntry.HHTDesire.MinAge && calcPerson.Age <= personTraitDesireEntry.HHTDesire.MaxAge) { Desire d = personTraitDesireEntry.HHTDesire.Desire; calcPerson.AddDesire(new PersonDesireDto(d.Name, d.IntID, personTraitDesireEntry.HHTDesire.Threshold, personTraitDesireEntry.HHTDesire.DecayTime, personTraitDesireEntry.HHTDesire.Weight, d.CriticalThreshold, personTraitDesireEntry.SrcTrait.PrettyName, d.DesireCategory, personTraitDesireEntry.HHTDesire.SicknessDesire, d.IsSharedDesire)); } break; case ModularHouseholdTrait.ModularHouseholdTraitAssignType.Name: // var foundPerson = false; if (personTraitDesireEntry.Person == null) { throw new LPGException("BUG: Person was null. Please report."); } if (calcPerson.ID == personTraitDesireEntry.Person.ID) { // foundPerson = true; // found calcPerson.AddDesire(new PersonDesireDto(personTraitDesireEntry.HHTDesire.Name, personTraitDesireEntry.HHTDesire.Desire.IntID, personTraitDesireEntry.HHTDesire.Threshold, personTraitDesireEntry.HHTDesire.DecayTime, personTraitDesireEntry.HHTDesire.Weight, personTraitDesireEntry.HHTDesire.Desire.CriticalThreshold, personTraitDesireEntry.SrcTrait.PrettyName, personTraitDesireEntry.HHTDesire.Desire.DesireCategory, personTraitDesireEntry.HHTDesire.SicknessDesire, personTraitDesireEntry.HHTDesire.Desire.IsSharedDesire)); } /* * if (!foundPerson) * { * throw new DataIntegrityException("The modular household " + modularhouseholdName + * " does not contain the Person " + * personTraitDesireEntry.Person.Name); * }*/ break; default: throw new LPGException( "Unknown Assignment Type putting together a modular household. Looks like something was forgotten. Please report to the programmer."); } } }
public void FileFactoryAndTrackerTest() { Config.IsInUnitTesting = true; using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) { wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults(); var clt = new CalcLoadType("calcloadtype", "kwh", "kW", 0.001, true, Guid.NewGuid().ToStrGuid()); BitArray isSick = new BitArray(calcParameters.InternalTimesteps); BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps); var personDto = CalcPersonDto.MakeExamplePerson(); Random r = new Random(); Mock <ILogFile> lf = new Mock <ILogFile>(); using (CalcRepo calcRepo = new CalcRepo(rnd: r, lf: lf.Object, calcParameters: calcParameters)) { CalcLocation cloc = new CalcLocation("blub", Guid.NewGuid().ToStrGuid()); var cp = new CalcPerson(personDto, cloc, isSick, isOnVacation, calcRepo); /*"personname", 1, 1, null, 1, PermittedGender.Female, lf: null, * householdKey: "hh1", startingLocation: null, traitTag: "traittag", * householdName: "hhname0",calcParameters:calcParameters,isSick:isSick, guid:Guid.NewGuid().ToStrGuid()); */ var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "testhh", wd.InputDataLogger); fft.RegisterHousehold(new HouseholdKey("hh1"), "test key", HouseholdKeyType.Household, "desc", null, null); fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general key", HouseholdKeyType.General, "desc", null, null); fft.MakeFile <StreamWriter>("file1", "desc", true, ResultFileID.Actions, new HouseholdKey("hh1"), TargetDirectory.Charts, TimeSpan.FromMinutes(1), CalcOption.HouseholdContents, clt.ConvertToLoadTypeInformation(), cp.MakePersonInformation()); //fft.ResultFileList.WriteResultEntries(wd.WorkingDirectory); ResultFileEntryLogger rfel = new ResultFileEntryLogger(wd.SqlResultLoggingService); var rfes = rfel.Load(); rfes.Count.Should().BeGreaterThan(0); //ResultFileList.ReadResultEntries(wd.WorkingDirectory); fft.GetResultFileEntry(ResultFileID.Actions, clt.Name, new HouseholdKey("hh1"), cp.MakePersonInformation(), null); fft.Dispose(); } wd.CleanUp(); } }
public void BasicTest() { using (WorkingDir wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) { wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults().EnableShowSettlingPeriod().SetSettlingDays(5); using (FileFactoryAndTracker fft = new FileFactoryAndTracker(wd.WorkingDirectory, "blub", wd.InputDataLogger)) { fft.RegisterGeneralHouse(); fft.RegisterHousehold(new HouseholdKey("HH1"), "test", HouseholdKeyType.Household, "desc", null, null); ThoughtsLogFile tlf = new ThoughtsLogFile(fft, calcParameters); Random rnd = new Random(); //NormalRandom nr = new NormalRandom(0, 0.1, rnd); // this array is pure nonsense and only to make it stop crashing the unit test //_calcParameters.InternalDateTimeForSteps = new List<DateTime>(4); //for (int i = 0; i < 4; i++) //{ // _calcParameters.InternalDateTimeForSteps.Add(DateTime.Now); //} CalcLocation cloc = new CalcLocation("cloc", Guid.NewGuid().ToStrGuid()); BitArray isSick = new BitArray(calcParameters.InternalTimesteps); BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps); CalcPersonDto dto = CalcPersonDto.MakeExamplePerson(); Mock <ILogFile> lf = new Mock <ILogFile>(); NormalRandom nr = new NormalRandom(0, 0.1, rnd); using (CalcRepo calcRepo = new CalcRepo(rnd: rnd, lf: lf.Object, calcParameters: calcParameters, normalRandom: nr)) { CalcPerson cp = new CalcPerson(dto, cloc, isSick, isOnVacation, calcRepo); //"personName", 0, 1, rnd, 1, PermittedGender.Male, null, "HH1" ,cloc,"traittag", "hhname0",calcParameters,isSick,Guid.NewGuid().ToStrGuid()); TimeStep ts = new TimeStep(0, 0, false); ThoughtEntry te = new ThoughtEntry(cp, ts, "blua"); calcParameters.SetSettlingDays(0); tlf.WriteEntry(te, new HouseholdKey("HH1")); } tlf.Dispose(); } wd.CleanUp(); } }
public void TestBasics() { // needs major redesign of calcperson class using (WorkingDir wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) {// StreamFactory sf = new StreamFactory(); DateTime startdate = new DateTime(2018, 1, 1); DateTime enddate = startdate.AddMinutes(100); CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate).EnableShowSettlingPeriod().SetSettlingDays(0); wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "test1", wd.InputDataLogger)) { fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general", HouseholdKeyType.General, "desc", null, null); //SqlResultLoggingService srls = new SqlResultLoggingService(wd.WorkingDirectory); CalculationProfiler profiler = new CalculationProfiler(); CalcRepo calcRepo = CalcRepo.Make(calcParameters, wd.InputDataLogger, wd.WorkingDirectory, "name", profiler); DesiresLogFile dlf = new DesiresLogFile(fft, calcParameters); CalcDesire cd1 = new CalcDesire("desire1", 1, 0.5m, 12, 1, 1, 60, -1, null, "", ""); //NormalRandom nr = new NormalRandom(0, 0.1, r); CalcLocation cloc = new CalcLocation("cloc", Guid.NewGuid().ToStrGuid()); BitArray isSick = new BitArray(calcParameters.InternalTimesteps); BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps); CalcPersonDto calcPerson = CalcPersonDto.MakeExamplePerson(); CalcPerson cp = new CalcPerson(calcPerson, cloc, isSick, isOnVacation, calcRepo); //"bla", 1, 5, r, 48, PermittedGender.Male, lf, "HH1", cloc, "traittag", "hhname0",calcParameters,isSick,Guid.NewGuid().ToStrGuid()); cp.PersonDesires.AddDesires(cd1); dlf.RegisterDesires(cp.PersonDesires.Desires.Values); TimeStep ts = new TimeStep(0, 0, true); DesireEntry de = new DesireEntry(cp, ts, cp.PersonDesires, dlf, calcParameters); fft.RegisterHousehold(new HouseholdKey("hh1"), "bla", HouseholdKeyType.Household, "desc", null, null); dlf.WriteEntry(de, new HouseholdKey("hh1")); dlf.Dispose(); } wd.CleanUp(); } }
public List <CalcPersonDto> MakePersonDtos([NotNull][ItemNotNull] List <ModularHouseholdPerson> persons, [NotNull] HouseholdKey householdKey, [NotNull][ItemNotNull] List <VacationTimeframe> vacationTimes, [NotNull][ItemNotNull] List <ModularHousehold.PersonTraitDesireEntry> traitDesires, [NotNull] string modularHouseholdName) { List <CalcPersonDto> personDtOs = new List <CalcPersonDto>(); foreach (ModularHouseholdPerson hhPerson in persons) { string traitTagName = hhPerson.TraitTag?.Name; if (traitTagName == null) { traitTagName = "no trait tag set"; } var sicknessSpans = CalculateSicknessdays(hhPerson.Person.SickDays, hhPerson.Person.AverageSicknessDuration); var vacationSpans = _vacationDtoFactory.GetVacationSpans(vacationTimes); CalcPersonDto dto = new CalcPersonDto(hhPerson.Person.PrettyName, Guid.NewGuid().ToStrGuid(), hhPerson.Person.Age, hhPerson.Person.Gender, householdKey, sicknessSpans, vacationSpans, hhPerson.Person.IntID, traitTagName, modularHouseholdName); AddTraitDesiresToOnePerson(traitDesires, dto); personDtOs.Add(dto); } return(personDtOs); }
public void TestInterruptionTest() { using var wd = new WorkingDir(Utili.GetCurrentMethodAndClass()); DateTime startdate = new DateTime(2018, 1, 1); DateTime enddate = startdate.AddMinutes(100); CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate).SetSettlingDays(0).EnableShowSettlingPeriod().DisableShowSettlingPeriod().SetAffordanceRepetitionCount(1); //var r = new Random(1); //var nr = new NormalRandom(0, 1, r); var desire1 = new CalcDesire("desire1", 1, 0.5m, 4, 1, 1, 60, -1, null, "", ""); var lt = new CalcLoadType("calcLoadtype1", "kwh", "W", 1, true, Guid.NewGuid().ToStrGuid()); //var variableOperator = new VariableOperator(); using var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "blub", wd.InputDataLogger); var key = new HouseholdKey("HH1"); wd.InputDataLogger.AddSaver(new ActionEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new LocationEntryLogger(wd.SqlResultLoggingService)); fft.RegisterHousehold(key, "hh1", HouseholdKeyType.Household, "desc", null, null); fft.RegisterHousehold(Constants.GeneralHouseholdKey, "General", HouseholdKeyType.General, "Desc", null, null); //SqlResultLoggingService srls = new SqlResultLoggingService(wd.WorkingDirectory); DateStampCreator dsc = new DateStampCreator(calcParameters); using OnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calcParameters); using var lf = new LogFile(calcParameters, fft); var cloc = new CalcLocation("loc1", Guid.NewGuid().ToStrGuid()); BitArray isSick = new BitArray(calcParameters.InternalTimesteps); BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps); CalcPersonDto calcPerson = CalcPersonDto.MakeExamplePerson(); var odap = new OnlineDeviceActivationProcessor(old, calcParameters, fft); Random rnd = new Random(); NormalRandom nr = new NormalRandom(0, 1, rnd); using CalcRepo calcRepo = new CalcRepo(lf: lf, odap: odap, calcParameters: calcParameters, rnd: rnd, normalRandom: nr, onlineLoggingData: old); var cp = new CalcPerson(calcPerson, cloc, isSick, isOnVacation, calcRepo); //"blub", 1, 1, r,20, PermittedGender.Male, lf, "HH1", cloc,"traittag","hhname0", calcParameters,isSick, Guid.NewGuid().ToStrGuid()); cp.PersonDesires.AddDesires(desire1); cp.SicknessDesires.AddDesires(desire1); var deviceLoads = new List <CalcDeviceLoad> { new CalcDeviceLoad("devload1", 1, lt, 100, 1) }; var devCategoryGuid = Guid.NewGuid().ToStrGuid(); CalcDeviceDto cdd1 = new CalcDeviceDto("cdevice1", devCategoryGuid, key, OefcDeviceType.Device, "category", "", Guid.NewGuid().ToStrGuid(), cloc.Guid, cloc.Name); var cdev1 = new CalcDevice(deviceLoads, cloc, cdd1, calcRepo); CalcDeviceDto cdd2 = new CalcDeviceDto("cdevice2", devCategoryGuid, key, OefcDeviceType.Device, "category", "", Guid.NewGuid().ToStrGuid(), cloc.Guid, cloc.Name); var cdev2 = new CalcDevice(deviceLoads, cloc, cdd2, calcRepo); CalcDeviceDto cdd3 = new CalcDeviceDto("cdevice3", devCategoryGuid, key, OefcDeviceType.Device, "category", "", Guid.NewGuid().ToStrGuid(), cloc.Guid, cloc.Name); var cdev3 = new CalcDevice(deviceLoads, cloc, cdd3, calcRepo); cloc.Devices.Add(cdev1); cloc.Devices.Add(cdev2); cloc.Devices.Add(cdev3); var daylight = new BitArray(100); daylight.SetAll(true); DayLightStatus dls = new DayLightStatus(daylight); double[] newValues = { 1, 1, 1.0, 1, 1, 1, 1, 1 }; var newList = new List <double>(newValues); var cprof = new CalcProfile("cp1", Guid.NewGuid().ToStrGuid(), newList, ProfileType.Relative, "bla"); var desires = new List <CalcDesire> { desire1 }; var color = new ColorRGB(255, 0, 0); CalcVariableRepository crv = new CalcVariableRepository(); BitArray isBusy = new BitArray(calcParameters.InternalTimesteps, false); var aff1 = new CalcAffordance("aff1", cprof, cloc, false, desires, 1, 100, PermittedGender.All, false, 0, color, "aff category", true, false, new List <CalcAffordanceVariableOp>(), new List <VariableRequirement>(), ActionAfterInterruption.GoBackToOld, "bla", 100, false, "", Guid.NewGuid().ToStrGuid(), crv , new List <CalcAffordance.DeviceEnergyProfileTuple>(), isBusy, BodilyActivityLevel.Low, calcRepo); aff1.AddDeviceTuple(cdev1, cprof, lt, 0, calcParameters.InternalStepsize, 1, 1); cloc.AddAffordance(aff1); var aff2 = new CalcAffordance("aff2", cprof, cloc, false, desires, 1, 100, PermittedGender.All, false, 0, color, "aff category", false, false, new List <CalcAffordanceVariableOp>(), new List <VariableRequirement>(), ActionAfterInterruption.GoBackToOld, "bla", 100, false, "", Guid.NewGuid().ToStrGuid(), crv , new List <CalcAffordance.DeviceEnergyProfileTuple>(), isBusy, BodilyActivityLevel.Low, calcRepo); aff2.AddDeviceTuple(cdev2, cprof, lt, 0, calcParameters.InternalStepsize, 1, 1); cloc.AddAffordance(aff2); var clocs = new List <CalcLocation> { cloc }; BitArray isBusySub = new BitArray(calcParameters.InternalTimesteps, false); var calcSubAffordance = new CalcSubAffordance("subaffname", cloc, desires, 0, 100, 1, PermittedGender.All, "subaff", false, true, aff1, new List <CalcAffordanceVariableOp>(), 100, "testing", Guid.NewGuid().ToStrGuid(), isBusySub, crv, BodilyActivityLevel.Low, calcRepo); aff2.SubAffordances.Add(calcSubAffordance); calcSubAffordance.SetDurations(2); var persons = new List <CalcPerson> { cp }; for (var i = 0; i < 100; i++) { TimeStep ts = new TimeStep(i, 0, true); cp.NextStep(ts, clocs, dls, new HouseholdKey("hh1"), persons, 1); } //wd.CleanUp(); }
public void PostProcessingTestTwoActivation() { var startdate = new DateTime(2018, 1, 1); var enddate = startdate.AddMinutes(100); var calcParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate).EnableShowSettlingPeriod(); //CalculationProfiler calculationProfiler = new CalculationProfiler(); var rnd = new Random(1); var nr = new NormalRandom(0, 1, rnd); using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) { calcParameters.Enable(CalcOption.HouseSumProfilesFromDetailedDats); calcParameters.Enable(CalcOption.DeviceProfilesIndividualHouseholds); calcParameters.Enable(CalcOption.TotalsPerDevice); calcParameters.Enable(CalcOption.TotalsPerLoadtype); calcParameters.Enable(CalcOption.DeviceActivations); calcParameters.Enable(CalcOption.DetailedDatFiles); Config.IsInUnitTesting = true; wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new CalcParameterLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new CalcLoadTypeDtoLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new CalcObjectInformationLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new DeviceActivationEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new CalcPersonDtoLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new DeviceTaggingSetLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.Save(calcParameters); using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "hh1", wd.InputDataLogger)) { fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general", HouseholdKeyType.General, "desc", null, null); //fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general", HouseholdKeyType.General,"desc"); //SqlResultLoggingService srls =new SqlResultLoggingService(Path.Combine(wd.WorkingDirectory, "results.sqlite")); var dsc = new DateStampCreator(calcParameters); using (IOnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calcParameters)) { var coi = new CalcObjectInformation(CalcObjectType.ModularHousehold, "objname", wd.WorkingDirectory); wd.InputDataLogger.Save(coi); using (var lf = new LogFile(calcParameters, fft)) { var key = new HouseholdKey("hh1"); fft.RegisterHousehold(key, "hh1 key", HouseholdKeyType.Household, "Description", null, null); var odap = new OnlineDeviceActivationProcessor(old, calcParameters, fft); var clt = new CalcLoadType("lt1", "W", "kWh", 3, true, Guid.NewGuid().ToStrGuid()); var loadTypes = new List <CalcLoadTypeDto> { clt.ConvertToDto() }; wd.InputDataLogger.Save(loadTypes); var cdl = new CalcDeviceLoad("devload1", 10, clt, 666, 0); var deviceLoads = new List <CalcDeviceLoad> { cdl }; var cloc = new CalcLocation("locname", Guid.NewGuid().ToStrGuid()); var devguid = Guid.NewGuid().ToStrGuid(); var devcategoryguid = Guid.NewGuid().ToStrGuid(); var cdto = new CalcDeviceDto("devicename", devcategoryguid, key, OefcDeviceType.Device, "category", "", devguid, cloc.Guid, cloc.Name); var devices = new List <IHouseholdKey> { cdto }; wd.InputDataLogger.SaveList(devices); using (var calcRepo = new CalcRepo(odap, calcParameters: calcParameters, rnd: rnd, normalRandom: nr)) { var device = new CalcDevice(deviceLoads, cloc, cdto, calcRepo); //var devices = new List<CalcDevice> {device}; var cp = new CalcProfile("profile1", Guid.NewGuid().ToStrGuid(), new TimeSpan(0, 1, 0), ProfileType.Absolute, "custom"); cp.AddNewTimepoint(new TimeSpan(0), 0); cp.AddNewTimepoint(new TimeSpan(0, 1, 0), 10); cp.AddNewTimepoint(new TimeSpan(0, 2, 0), 0); cp.ConvertToTimesteps(); //var locations = new List<CalcLocation> {cloc}; var ts = new TimeStep(0, calcParameters); device.SetTimeprofile(cp, ts, clt, "affordanceName", "activatorName", 1, false); device.SetTimeprofile(cp, ts.AddSteps(5), clt, "affordanceName", "activatorName", 1, false); device.SetTimeprofile(cp.CompressExpandDoubleArray(0.5), ts.AddSteps(8), clt, "affordanceName", "activatorName", 1, false); device.SetTimeprofile(cp.CompressExpandDoubleArray(2), ts.AddSteps(10), clt, "affordanceName", "activatorName", 1, false); } for (var i = 0; i < 30; i++) { var ts1 = new TimeStep(i, calcParameters); var filerows = odap.ProcessOneTimestep(ts1); filerows.Count.Should().Be(1); filerows[0].EnergyEntries.Count.Should().Be(1); Logger.Info(filerows[0].EnergyEntries[0].ToString(CultureInfo.CurrentCulture)); foreach (var fileRow in filerows) { fileRow.Save(odap.BinaryOutStreams[fileRow.LoadType]); } } //var autoDevs = new List<CalcAutoDev>(); //var ps = new Postprocessor(lf.FileFactoryAndTracker, calculationProfiler,calcParameters); //var householdKeys = new HashSet<string> {"1"}; //var calcAffordanceTaggingSets = new List<CalcAffordanceTaggingSet>(); var deviceTaggingSetInformation = new DeviceTaggingSetInformation("name"); var taggingsets = new List <DeviceTaggingSetInformation> { deviceTaggingSetInformation }; wd.InputDataLogger.Save(taggingsets); //var householdPlans = new List<CalcHouseholdPlan>(); //var householdNamesByNumber = new Dictionary<string, string> {["HH1"] = "household"}; //var affordanceEnergyUseFile = new AffordanceEnergyUseFile(lf.FileFactoryAndTracker,calcParameters); //lf.Close(null); // needed to free file access //var results = new Dictionary<string, double>(); var persons = new List <CalcPersonDto>(); var dto = new CalcPersonDto("name", Guid.NewGuid().ToStrGuid(), 18, PermittedGender.Female, key, new List <DateSpan>(), new List <DateSpan>(), 1, "tag", "hhname"); persons.Add(dto); wd.InputDataLogger.SaveList(persons.ConvertAll(x => (IHouseholdKey)x)); //var deviceNamesToCategory = new Dictionary<string, string>(); old.FinalSaveToDatabase(); lf.Dispose(); fft.Dispose(); var cprof = new CalculationProfiler(); var ppm = new PostProcessingManager(cprof, fft); ppm.Run(wd.WorkingDirectory); var tel = new TotalsPerLoadtypeEntryLogger(wd.SqlResultLoggingService); var results = tel.Read(key); results[0].Value.Should().Be(150); } } } Logger.Info(wd.WorkingDirectory); wd.CleanUp(); } }
public void PostProcessingTestSingleActivation() { using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) { wd.InputDataLogger.AddSaver(new CalcParameterLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new DeviceActivationEntryLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new CalcLoadTypeDtoLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new CalcPersonDtoLogger(wd.SqlResultLoggingService)); wd.InputDataLogger.AddSaver(new DeviceTaggingSetLogger(wd.SqlResultLoggingService)); var calculationProfiler = new CalculationProfiler(); var startdate = new DateTime(2018, 1, 1); var enddate = startdate.AddMinutes(1000); var calcParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate).EnableShowSettlingPeriod(); var rnd = new Random(1); var nr = new NormalRandom(0, 1, rnd); calcParameters.Enable(CalcOption.HouseSumProfilesFromDetailedDats); calcParameters.Enable(CalcOption.DeviceProfilesIndividualHouseholds); calcParameters.Enable(CalcOption.TotalsPerDevice); calcParameters.Enable(CalcOption.TotalsPerLoadtype); calcParameters.Enable(CalcOption.DetailedDatFiles); calcParameters.Enable(CalcOption.DeviceActivations); var key = new HouseholdKey("hh1"); wd.InputDataLogger.Save(calcParameters); using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "hhname", wd.InputDataLogger)) { //wd.InputDataLogger.AddSaver(new CalcDeviceDtoLogger(wd.SqlResultLoggingService)); var dsc = new DateStampCreator(calcParameters); using (IOnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calcParameters)) { var calcPersonDto = new CalcPersonDto("blub", "personguid".ToStrGuid(), 1, PermittedGender.Male, key, new List <DateSpan>(), new List <DateSpan>(), 1, "traittag", "householdname"); var persons = new List <CalcPersonDto> { calcPersonDto }; wd.InputDataLogger.SaveList(persons.ConvertAll(x => (IHouseholdKey)x)); using (var lf = new LogFile(calcParameters, fft)) { fft.RegisterHousehold(key, "test hh", HouseholdKeyType.Household, "Description", null, null); fft.RegisterHousehold(Constants.GeneralHouseholdKey, "General", HouseholdKeyType.General, "Description", null, null); var odap = new OnlineDeviceActivationProcessor(old, calcParameters, fft); var clt = new CalcLoadType("lt1", "W", "kWh", 3, true, Guid.NewGuid().ToStrGuid()); var loadTypeDtos = new List <CalcLoadTypeDto> { clt.ConvertToDto() }; wd.InputDataLogger.Save(loadTypeDtos); //var loadTypes = new List<CalcLoadType> {clt}; var cdl = new CalcDeviceLoad("devload1", 10, clt, 666, 0); var deviceLoads = new List <CalcDeviceLoad> { cdl }; var cloc = new CalcLocation("locname", Guid.NewGuid().ToStrGuid()); var deviceCategoryGuid = Guid.NewGuid().ToStrGuid(); var calcDeviceDto = new CalcDeviceDto("devicename", deviceCategoryGuid, key, OefcDeviceType.Device, "category", string.Empty, Guid.NewGuid().ToStrGuid(), cloc.Guid, cloc.Name); var calcDeviceDtos = new List <CalcDeviceDto> { calcDeviceDto }; wd.InputDataLogger.SaveList(calcDeviceDtos.ConvertAll(x => (IHouseholdKey)x)); //device tagging set for the post processing var cdts = new List <DeviceTaggingSetInformation>(); var dtsi = new DeviceTaggingSetInformation("myset"); dtsi.AddTag(calcDeviceDto.Name, "testTag "); cdts.Add(dtsi); wd.InputDataLogger.Save(cdts); //device using (var calcRepo = new CalcRepo(calcParameters: calcParameters, odap: odap, rnd: rnd, normalRandom: nr)) { var device = new CalcDevice(deviceLoads, cloc, calcDeviceDto, calcRepo); //var devices = new List<CalcDevice> {device}; double[] resultValues = { 0, 100.0, 0, 0, 0, 0, 0, 0, 0, 0 }; var cp = new CalcProfile("profile1", Guid.NewGuid().ToStrGuid(), new TimeSpan(0, 1, 0), ProfileType.Absolute, "custom"); cp.AddNewTimepoint(new TimeSpan(0), 0); cp.AddNewTimepoint(new TimeSpan(0, 1, 0), 10); cp.AddNewTimepoint(new TimeSpan(0, 2, 0), 0); cp.ConvertToTimesteps(); //var locations = new List<CalcLocation> {cloc}; var ts1 = new TimeStep(0, calcParameters); device.SetTimeprofile(cp, ts1, clt, "affordanceName", "activatorName", 10, false); for (var i = 0; i < 10; i++) { var ts = new TimeStep(i, calcParameters); var filerows = odap.ProcessOneTimestep(ts); filerows.Count.Should().Be(1); filerows[0].EnergyEntries.Count.Should().Be(1); Logger.Info(filerows[0].EnergyEntries[0].ToString(CultureInfo.CurrentCulture)); filerows[0].EnergyEntries[0].Should().Be(resultValues[i]); foreach (var fileRow in filerows) { fileRow.Save(odap.BinaryOutStreams[fileRow.LoadType]); } } } old.FinalSaveToDatabase(); fft.Dispose(); lf.Dispose(); // needed to free the file access //var autoDevs = new List<CalcAutoDev>(); //var ps = new Postprocessor(lf.FileFactoryAndTracker, calculationProfiler,calcParameters ); //var householdKeys = new HashSet<string> {"1"}; //var calcAffordanceTaggingSets = new List<CalcAffordanceTaggingSet>(); //var calcDeviceTaggingSets = new List<CalcDeviceTaggingSet>(); //var calcDeviceTaggingSets = new List<DeviceTaggingSetInformation>(); //var householdPlans = new List<CalcHouseholdPlan>(); //var householdNamesByNumber = new Dictionary<string, string> {["HH1"] = "household"}; //var affordanceEnergyUseFile = new AffordanceEnergyUseFile(lf.FileFactoryAndTracker,calcParameters); //var results = new Dictionary<string, double>(); //BitArray isSick = new BitArray(calcParameters.InternalTimesteps); //BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps); //CalcPersonDto cpd = new CalcPersonDto("personname", Guid.NewGuid().ToStrGuid(),18,PermittedGender.Female,key,new List<DateSpan>(),new List<DateSpan>(),1,"traittag","hhname" ); //var persons = new List<CalcPerson> {new CalcPerson(cpd, new Random(), lf,cloc,calcParameters,isSick,isOnVacation)}; //var deviceNamesToCategory = new Dictionary<string, string>(); //CalcDeviceTaggingSets calcDeviceTaggingSets = new CalcDeviceTaggingSets(); var cpp = new PostProcessingManager(calculationProfiler, fft); cpp.Run(wd.WorkingDirectory); /*ps.EndOfSimulationProcessing(devices, locations, autoDevs, loadTypes, odap.Oefc, householdKeys * ,calcAffordanceTaggingSets, calcDeviceTaggingSets, householdPlans, householdNamesByNumber * ,affordanceEnergyUseFile, results, CalcObjectType.ModularHousehold, persons, deviceNamesToCategory,10);*/ //var dstpath = Path.Combine(wd.WorkingDirectory,DirectoryNames.CalculateTargetdirectory(TargetDirectory.Reports),"DeviceSums." + clt.Name + ".csv"); lf.Dispose(); // needed to free the file access var di = new DirectoryInfo(wd.WorkingDirectory); var fis = di.GetFiles("DeviceSums.*", SearchOption.AllDirectories); if (fis.Length == 0) { throw new LPGException("No Sum File was generated"); } fft.Dispose(); using (var sr = new StreamReader(fis[0].FullName)) { sr.ReadLine(); //header var result = sr.ReadLine(); //0 if (result == null) { throw new LPGException("Result was null"); } var arr = result.Split(';'); Assert.Equal("300", arr[1]); } } } } Logger.Info(wd.WorkingDirectory); wd.CleanUp(); } }