Exemple #1
0
 private void MakeSubAffordances([NotNull] DtoCalcLocationDict locations, [NotNull] CalcVariableRepository variableRepository,
                                 [NotNull] CalcAffordanceDto affordancedto, [NotNull] CalcAffordance caff)
 {
     foreach (CalcSubAffordanceDto sdto in affordancedto.SubAffordance)
     {
         CalcLocation      subaffLocation     = locations.GetCalcLocationByGuid(sdto.LocGuid);
         List <CalcDesire> satisfactionValues = MakeCalcDesires(sdto.Satisfactionvalues);
         var varOps = MakeVariableOps(locations, sdto.VariableOps, variableRepository);
         //subaffordances have no time limits
         BitArray          isBusySub = new BitArray(_calcRepo.CalcParameters.InternalTimesteps, false);
         CalcSubAffordance csuf      = new CalcSubAffordance(sdto.Name,
                                                             subaffLocation,
                                                             satisfactionValues,
                                                             sdto.MiniumAge, sdto.MaximumAge,
                                                             sdto.Delaytimesteps,
                                                             sdto.PermittedGender,
                                                             sdto.AffCategory,
                                                             sdto.IsInterruptable,
                                                             sdto.IsInterrupting,
                                                             caff,
                                                             varOps,
                                                             sdto.Weight,
                                                             sdto.SourceTrait,
                                                             sdto.Guid, isBusySub, variableRepository, caff.BodilyActivityLevel, _calcRepo);
         caff.SubAffordances.Add(csuf);
     }
 }
        public void CalcAffordanceVariableTestSet()
        {
            var deviceCategoryGuid = Guid.NewGuid().ToStrGuid();
            //var r = new Random(0);
            //var nr = new NormalRandom(0, 0.1, r);
            const int stepcount = 150;

            Config.IsInUnitTesting = true;
            DateTime startdate = new DateTime(2018, 1, 1);
            DateTime enddate   = startdate.AddMinutes(stepcount);
            //_calcParameters.InitializeTimeSteps(startdate, enddate, new TimeSpan(0, 1, 0), 3, false);
            CalcParameters calcParameters =
                CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate);
            var timeStep = new TimeSpan(0, 1, 0);
            var cp       = new CalcProfile("profile", Guid.NewGuid().ToStrGuid(), timeStep, ProfileType.Absolute, "blub");

            cp.AddNewTimepoint(new TimeSpan(0), 100);
            cp.AddNewTimepoint(new TimeSpan(0, 10, 0), 0);
            cp.ConvertToTimesteps();
            var variables    = new List <CalcAffordanceVariableOp>();
            var variableReqs = new List <VariableRequirement>();
            var loc          = new CalcLocation("loc", Guid.NewGuid().ToStrGuid());
            CalcVariableRepository calcVariableRepository = new CalcVariableRepository();
            var          variableGuid = Guid.NewGuid().ToStrGuid();
            HouseholdKey key          = new HouseholdKey("hh1");
            CalcVariable cv           = new CalcVariable("varname", variableGuid, 0, loc.Name, loc.Guid, key);

            calcVariableRepository.RegisterVariable(cv);
            variables.Add(new CalcAffordanceVariableOp(cv.Name, 1, loc, VariableAction.SetTo,
                                                       VariableExecutionTime.Beginning, variableGuid));
            BitArray     isBusy = new BitArray(100, false);
            Random       rnd    = new Random();
            NormalRandom nr     = new NormalRandom(0, 1, rnd);

            using CalcRepo calcRepo = new CalcRepo(calcParameters: calcParameters, odap: new Mock <IOnlineDeviceActivationProcessor>().Object, normalRandom: nr, rnd: rnd);
            var aff = new CalcAffordance("bla", cp, loc, false, new List <CalcDesire>(), 0, 99,
                                         PermittedGender.All, false, 0.1, new ColorRGB(0, 0, 0), "bla", false, false, variables, variableReqs,
                                         ActionAfterInterruption.GoBackToOld, "bla", 100, false, "", Guid.NewGuid().ToStrGuid(),
                                         calcVariableRepository,
                                         new List <CalcAffordance.DeviceEnergyProfileTuple>(), isBusy, BodilyActivityLevel.Low, calcRepo);
            var lt       = new CalcLoadType("load", "unit1", "unit2", 1, true, Guid.NewGuid().ToStrGuid());
            var cdl      = new CalcDeviceLoad("cdl", 1, lt, 1, 0.1);
            var devloads = new List <CalcDeviceLoad> {
                cdl
            };
            CalcDeviceDto cdd = new CalcDeviceDto("device", deviceCategoryGuid, key,
                                                  OefcDeviceType.Device, "category", string.Empty,
                                                  Guid.NewGuid().ToStrGuid(), loc.Guid, loc.Name);
            var cd = new CalcDevice(devloads, loc,
                                    cdd, calcRepo);

            //loc.Variables.Add("Variable1", 0);
            aff.AddDeviceTuple(cd, cp, lt, 0, timeStep, 10, 1);
            TimeStep ts = new TimeStep(0, 0, false);

            aff.IsBusy(ts, loc, "name");
            //var variableOperator = new VariableOperator();
            aff.Activate(ts, "blub", loc, out var _);
            calcVariableRepository.GetValueByGuid(variableGuid).Should().Be(1);
        }
        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();
            }
        }
Exemple #4
0
        public void SetAllAffordaces([NotNull][ItemNotNull] List <CalcAffordanceDto> affordances,
                                     [NotNull] DtoCalcLocationDict locations,
                                     [NotNull] CalcVariableRepository variableRepository,
                                     [ItemNotNull][NotNull] List <CalcDevice> devices)
        {
            if (affordances.Count == 0)
            {
                throw new LPGException("No Affordances found.");
            }

            foreach (CalcAffordanceDto affordancedto in affordances)
            {
                CalcProfile  personProfile = CalcDeviceFactory.MakeCalcProfile(affordancedto.PersonProfile, _calcRepo.CalcParameters);
                CalcLocation calcLocation  = locations.GetCalcLocationByGuid(affordancedto.CalcLocationGuid);
                var          calcDesires   = MakeCalcDesires(affordancedto.Satisfactionvalues);
                var          color         = new ColorRGB(affordancedto.ColorR, affordancedto.ColorG, affordancedto.ColorB);
                var          variableOps   = MakeVariableOps(locations, affordancedto.VariableOps, variableRepository);
                List <VariableRequirement> requirements = new List <VariableRequirement>();
                foreach (VariableRequirementDto requirementDto in affordancedto.VariableRequirements)
                {
                    VariableRequirement vrq = new VariableRequirement(requirementDto.Name,
                                                                      requirementDto.Value, requirementDto.CalcLocationName, requirementDto.LocationGuid,
                                                                      requirementDto.VariableCondition, variableRepository, requirementDto.VariableGuid);
                    requirements.Add(vrq);
                }

                var deviceEnergyProfiles = MakeDeviceEnergyProfileTuples(devices, affordancedto);

                var            busyarr = _availabilityDtoRepository.GetByGuid(affordancedto.IsBusyArray.Guid);
                CalcAffordance caff    = new CalcAffordance(
                    affordancedto.Name,
                    personProfile, calcLocation,
                    affordancedto.RandomEffect,
                    calcDesires,
                    affordancedto.MiniumAge,
                    affordancedto.MaximumAge,
                    affordancedto.PermittedGender,
                    affordancedto.NeedsLight,
                    affordancedto.TimeStandardDeviation,
                    color,
                    affordancedto.AffCategory,
                    affordancedto.IsInterruptable,
                    affordancedto.IsInterrupting,
                    variableOps,
                    requirements,
                    affordancedto.ActionAfterInterruption,
                    affordancedto.TimeLimitName,
                    affordancedto.Weight,
                    affordancedto.RequireAllDesires,
                    affordancedto.SrcTrait,
                    affordancedto.Guid,
                    variableRepository, deviceEnergyProfiles,
                    busyarr, affordancedto.BodilyActivityLevel, _calcRepo);
                MakeSubAffordances(locations, variableRepository, affordancedto, caff);

                calcLocation.AddAffordance(caff);
            }
        }
        private static void CheckForBusyness([NotNull] CalcLocation loc,
                                             [NotNull] CalcAffordance aff,
                                             [NotNull] CalcDevice cd, [NotNull] CalcLoadType lt)
        {
            Logger.Info("------------");
            TimeStep ts1 = new TimeStep(0, 0, true);

            Logger.Info("aff.isbusy 0: " + aff.IsBusy(ts1, loc, "person", false));
            var prevstate = false;

            for (var i = 0; i < 100; i++)
            {
                TimeStep ts2 = new TimeStep(i, 0, true);
                if (aff.IsBusy(ts2, loc, "person", false) != prevstate)
                {
                    prevstate = aff.IsBusy(ts2, loc, "name", false);
                    Logger.Info("aff.isbusy:" + i + ": " + prevstate);
                }
            }

            Logger.Info("aff.isbusy 100: " + aff.IsBusyArray[100]);

            prevstate = false;
            Logger.Info("aff.isbusyarray 0:   " + aff.IsBusyArray[0]);
            for (var i = 0; i < 100; i++)
            {
                if (aff.IsBusyArray[i] != prevstate)
                {
                    prevstate = aff.IsBusyArray[i];
                    Logger.Info("aff.isbusyarray: " + i + ": " + prevstate);
                }
            }

            Logger.Info("aff.isbusyarray 100: " + aff.IsBusyArray[100]);

            prevstate = false;
            TimeStep ts3 = new TimeStep(0, 0, false);

            Logger.Info("cd.isbusyarray 0:   " + cd.GetIsBusyForTesting(ts3, lt));
            for (var i = 0; i < 100; i++)
            {
                TimeStep ts4 = new TimeStep(i, 0, false);
                if (cd.GetIsBusyForTesting(ts4, lt) != prevstate)
                {
                    prevstate = cd.GetIsBusyForTesting(ts4, lt);
                    Logger.Info("cd.isbusyarray: " + i + ": " + prevstate);
                }
            }
            TimeStep ts5 = new TimeStep(100, 0, false);

            Logger.Info("cd.isbusyarray 100: " + cd.GetIsBusyForTesting(ts5, lt));
        }
        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();
        }
        private static CalcRepo SetupFullWorkingTransportationExample([NotNull] WorkingDir wd, [NotNull] Random rnd, [NotNull] out NormalRandom nr,
                                                                      [NotNull] out CalcLocation srcloc, [NotNull] out CalcLocation dstloc, [NotNull] out CalcSite dstSite,
                                                                      [NotNull] out TransportationHandler transportationHandler, [NotNull] out AffordanceBaseTransportDecorator abt, [NotNull] CalcParameters calcParameters,
                                                                      [NotNull] HouseholdKey key)
        {
            Config.IsInUnitTesting = true;
            CalcAffordance.DoubleCheckBusyArray = true;
            nr = new NormalRandom(0, 0.1, rnd);
            var calcprofilevalues = new List <double> {
                10,
                20,
                30
            };

            var cp = new CalcProfile("calcprofile", Guid.NewGuid().ToStrGuid(), calcprofilevalues, ProfileType.Absolute, "syn");

            srcloc = new CalcLocation("srclocation", Guid.NewGuid().ToStrGuid());
            dstloc = new CalcLocation("dstlocation", Guid.NewGuid().ToStrGuid());
            var calcdesire =
                new CalcDesire("calcdesire", 1, 0.5m, 10, 1, 1, 60, 0.1m, null, "sourcetrait", "desirecat");
            var calcdesires = new List <CalcDesire> {
                calcdesire
            };
            CalcVariableRepository crv = new CalcVariableRepository();
            Mock <IOnlineDeviceActivationProcessor> iodap = new Mock <IOnlineDeviceActivationProcessor>();
            var old = new Mock <IOnlineLoggingData>();

            using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "hh0", wd.InputDataLogger))
            {
                using (var lf = new LogFile(calcParameters, fft, true))
                {
                    var      calcRepo = new CalcRepo(odap: iodap.Object, calcParameters: calcParameters, rnd: rnd, normalRandom: nr, onlineLoggingData: old.Object, lf: lf);
                    BitArray isBusy   = new BitArray(calcParameters.InternalTimesteps, false);
                    var      ca       = new CalcAffordance("calcaffordance", cp, dstloc, false, calcdesires,
                                                           18, 50, PermittedGender.All, false, 0.1, LPGColors.Blue, "affordance category", false,
                                                           false, new List <CalcAffordanceVariableOp>(), new List <VariableRequirement>(),
                                                           ActionAfterInterruption.GoBackToOld, "timelimitname", 1, false,
                                                           "srctrait",
                                                           Guid.NewGuid().ToStrGuid(), crv, new List <CalcAffordance.DeviceEnergyProfileTuple>(),
                                                           isBusy, BodilyActivityLevel.Low, calcRepo);

                    var srcSite = new CalcSite("srcsite", Guid.NewGuid().ToStrGuid(), key);
                    srcSite.Locations.Add(srcloc);
                    dstSite = new CalcSite("dstSite", Guid.NewGuid().ToStrGuid(), key);
                    dstSite.Locations.Add(dstloc);
                    fft.RegisterHousehold(new HouseholdKey("hh0"), "hh0-prettyname", HouseholdKeyType.Household,
                                          "Desc", null, null);
                    transportationHandler = new TransportationHandler();
                    transportationHandler.AddSite(srcSite);
                    abt = new AffordanceBaseTransportDecorator(ca, dstSite, transportationHandler,
                                                               "travel to dstsite", new HouseholdKey("hh0"), Guid.NewGuid().ToStrGuid(), calcRepo);
                    dstloc.AddTransportationAffordance(abt);

                    var ctr = new CalcTravelRoute("myRoute1", srcSite, dstSite,
                                                  transportationHandler.VehicleDepot, transportationHandler.LocationUnlimitedDevices,
                                                  new HouseholdKey("hh0"), Guid.NewGuid().ToStrGuid(), calcRepo);
                    var myCategory = new CalcTransportationDeviceCategory("mycategory", false, Guid.NewGuid().ToStrGuid());
                    ctr.AddTravelRouteStep("driving", myCategory, 1, 36000, Guid.NewGuid().ToStrGuid());
                    transportationHandler.TravelRoutes.Add(ctr);
                    CalcLoadType    chargingloadtype = new CalcLoadType("chargingloadtype", "W", "kwh", 1, true, Guid.NewGuid().ToStrGuid());
                    List <CalcSite> calcSites        = new List <CalcSite>
                    {
                        srcSite,
                        dstSite
                    };
                    var            list = new List <CalcDeviceLoad>();
                    CalcDeviceLoad cdl  = new CalcDeviceLoad("bla", 1, chargingloadtype, 1, 1);
                    list.Add(cdl);
                    CalcDeviceDto cdd = new CalcDeviceDto("bus", myCategory.Guid,
                                                          new HouseholdKey("hh1"), OefcDeviceType.Transportation, myCategory.Name, string.Empty,
                                                          Guid.NewGuid().ToStrGuid(), string.Empty.ToStrGuid(), string.Empty);
                    var transportationDevice =
                        new CalcTransportationDevice(myCategory, 1, list, 100,
                                                     10, 1000, chargingloadtype, calcSites,
                                                     cdd, calcRepo);
                    transportationHandler.LocationUnlimitedDevices.Add(transportationDevice);
                    return(calcRepo);
                }
            }
        }
        public void RunDeviceOffsetTest()
        {
            //var r = new Random(0);
            //var nr = new NormalRandom(0, 0.1, r);
            const int stepcount       = 150;
            var       devCategoryGuid = Guid.NewGuid().ToStrGuid();

            Config.IsInUnitTesting = true;
            DateTime       startdate      = new DateTime(2018, 1, 1);
            DateTime       enddate        = startdate.AddMinutes(stepcount);
            CalcParameters calcParameters =
                CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate);
            var timeStep = new TimeSpan(0, 1, 0);
            var cp       = new CalcProfile("profile", Guid.NewGuid().ToStrGuid(), timeStep, ProfileType.Absolute, "blub");

            cp.AddNewTimepoint(new TimeSpan(0), 100);
            cp.AddNewTimepoint(new TimeSpan(0, 10, 0), 0);
            cp.ConvertToTimesteps();
            var loc = new CalcLocation(Utili.GetCurrentMethodAndClass(), Guid.NewGuid().ToStrGuid());
            CalcVariableRepository crv = new CalcVariableRepository();
            BitArray     isBusy        = new BitArray(calcParameters.InternalTimesteps, false);
            Random       rnd           = new Random();
            NormalRandom nr            = new NormalRandom(0, 1, rnd);

            using CalcRepo calcRepo = new CalcRepo(calcParameters: calcParameters, odap: new Mock <IOnlineDeviceActivationProcessor>().Object,
                                                   rnd: rnd, normalRandom: nr);
            var aff = new CalcAffordance("bla", cp, loc, false, new List <CalcDesire>(), 0, 99, PermittedGender.All,
                                         false, 0, new ColorRGB(0, 0, 0), "bla", 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);
            var lt  = new CalcLoadType("load", "unit1", "unit2", 1, true, Guid.NewGuid().ToStrGuid());
            var cdl = new CalcDeviceLoad("cdl", 1, lt, 1, 0.1);
            //var variableOperator = new VariableOperator();
            var devloads = new List <CalcDeviceLoad> {
                cdl
            };
            HouseholdKey  key = new HouseholdKey("HH1");
            CalcDeviceDto cdd = new CalcDeviceDto("device",
                                                  devCategoryGuid,
                                                  key,
                                                  OefcDeviceType.Device,
                                                  "category",
                                                  string.Empty,
                                                  Guid.NewGuid().ToStrGuid(),
                                                  loc.Guid,
                                                  loc.Name);
            var cd = new CalcDevice(devloads, loc, cdd, calcRepo);

            aff.AddDeviceTuple(cd, cp, lt, 20, timeStep, 10, 1);

            //bool result = aff.IsBusy(0, nr, r, loc);
            //(result).Should().BeFalse();
            CheckForBusyness(loc, aff, cd, lt);
            TimeStep ts = new TimeStep(0, 0, false);

            aff.Activate(ts.AddSteps(10), "blub", loc, out var _);
            CheckForBusyness(loc, aff, cd, lt);
            aff.IsBusy(ts.AddSteps(1), loc, "name", false).Should().BeTrue();
            aff.IsBusy(ts.AddSteps(19), loc, "name", false).Should().BeTrue();
            aff.IsBusy(ts, loc, "name", false).Should().BeFalse();
            aff.IsBusy(ts.AddSteps(20), loc, "name", false).Should().BeFalse();
        }
        private static void SetupProbabilityTest([NotNull] out CalcAffordance aff, [NotNull] out CalcLoadType lt,
                                                 [NotNull] out CalcDevice cd,
                                                 [NotNull] out CalcLocation loc, int stepcount, double probability)
        {
            Config.IsInUnitTesting = true;
            DateTime       startdate      = new DateTime(2018, 1, 1);
            DateTime       enddate        = startdate.AddMinutes(stepcount);
            CalcParameters calcParameters =
                CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate);
            var timeStep = new TimeSpan(0, 1, 0);
            var cp       = new CalcProfile("profile", Guid.NewGuid().ToStrGuid(), timeStep, ProfileType.Absolute, "blub");

            cp.AddNewTimepoint(new TimeSpan(0), 100);
            cp.AddNewTimepoint(new TimeSpan(0, 10, 0), 0);
            cp.ConvertToTimesteps();
            loc = new CalcLocation(Utili.GetCurrentMethodAndClass(), Guid.NewGuid().ToStrGuid());
            CalcVariableRepository cvr = new CalcVariableRepository();
            BitArray isBusy            = new BitArray(100, false);
            var      r = new Random(0);

            var nr = new NormalRandom(0, 0.1, r);
            IMock <IOnlineDeviceActivationProcessor> iodap = new Mock <IOnlineDeviceActivationProcessor>();

            using CalcRepo calcRepo = new CalcRepo(calcParameters: calcParameters, normalRandom: nr, rnd: r, odap: iodap.Object);
            aff = new CalcAffordance("bla",
                                     cp,
                                     loc,
                                     false,
                                     new List <CalcDesire>(),
                                     0,
                                     99,
                                     PermittedGender.All,
                                     false,
                                     0.1,
                                     new ColorRGB(0, 0, 0),
                                     "bla",
                                     false,
                                     false,
                                     new List <CalcAffordanceVariableOp>(),
                                     new List <VariableRequirement>(),
                                     ActionAfterInterruption.GoBackToOld,
                                     "bla",
                                     100,
                                     false,
                                     "",
                                     Guid.NewGuid().ToStrGuid(),
                                     cvr,
                                     new List <CalcAffordance.DeviceEnergyProfileTuple>(),
                                     isBusy,
                                     BodilyActivityLevel.Low,
                                     calcRepo);
            lt = new CalcLoadType("load", "unit1", "unit2", 1, true, Guid.NewGuid().ToStrGuid());
            var cdl      = new CalcDeviceLoad("cdl", 1, lt, 1, 0.1);
            var devloads = new List <CalcDeviceLoad> {
                cdl
            };
            CalcDeviceDto cdd = new CalcDeviceDto("device",
                                                  "devcategoryguid".ToStrGuid(),
                                                  new HouseholdKey("HH1"),
                                                  OefcDeviceType.Device,
                                                  "category",
                                                  string.Empty,
                                                  Guid.NewGuid().ToStrGuid(),
                                                  loc.Guid,
                                                  loc.Name);

            cd = new CalcDevice(devloads, loc, cdd, calcRepo);
            aff.AddDeviceTuple(cd, cp, lt, 0, timeStep, 10, probability);
        }