private Prosumer MakeFlatProfile([NotNull] CarDistanceEntry carDistance, [NotNull] Hausanschluss ha, [NotNull] Household household) { var slpprofile = Profile.MakeConstantProfile(carDistance.EnergyEstimate, carDistance.Name, Profile.ProfileResolution.QuarterHour); if (slpprofile == null) { throw new FlaException("No profile? Why?"); } var prosumer = new Prosumer(household.HouseGuid, carDistance.Name, carDistance.HouseComponentType, household.SourceGuid, household.FinalIsn, household.HausAnschlussGuid, ha.ObjectID, GenerationOrLoad.Load, ha.Trafokreis, Name + " - Flat Substitute Profile", "Flat Substitute Profile due to missing LPG profile") { Profile = slpprofile }; return(prosumer); }
private Prosumer ProvideLPGProfile([NotNull] ProviderParameterDto parameters, [NotNull] Hausanschluss ha, [NotNull] Household household, [NotNull] CarDistanceEntry carDistanceEntry) { string prosumerName = household.Name; string sourceGuid = household.SourceGuid; string householdKey = household.HouseholdKey; long isn = household.FinalIsn; try { var prosumer = _lpgProfileLoader.LoadProsumer(parameters, ha, prosumerName, carDistanceEntry.HouseComponentType, LoadtypetoSearchFor, _saveableEntry, sourceGuid, householdKey, household.HouseGuid, isn, Services.RunningConfig); if (prosumer == null) { return(null); } if (prosumer.Profile == null) { throw new FlaException("Profile was null"); } parameters.HouseComponentResultObject.ActualDrivingDistance = prosumer.Profile.EnergySum() / 15.0 * 100; return(prosumer); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { #pragma warning restore CA1031 // Do not catch general exception types Error(ex.Message); parameters.HouseComponentResultObject.ErrorMessage = ex.Message; } return(null); }
public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters) { CarDistanceEntry cde = (CarDistanceEntry)parameters.HouseComponent; Car myCar = _dbDto.Cars.Single(x => x.Guid == cde.CarGuid); parameters.HouseComponentResultObject.CarStatus = "Gasoline, not analyzed further"; if (myCar.RequiresProfile == CarProfileRequirement.NoProfile) { return(true); } Hausanschluss ha = _dbDto.Hausanschlusse.Single(x => x.Guid == cde.HausAnschlussGuid); parameters.HouseComponentResultObject.CarStatus = "Electric"; parameters.HouseComponentResultObject.CommutingDistance = cde.CommutingDistance; parameters.HouseComponentResultObject.OtherDrivingDistance = cde.FreizeitDistance; Household flaHousehold = _dbDto.Households.FirstOrDefault(x => x.Guid == cde.HouseholdGuid); if (flaHousehold == null) { parameters.HouseComponentResultObject.CarStatus += ", no household"; throw new FlaException("no household found"); } var house = _dbDto.Houses.FirstOrDefault(x => x.HouseGuid == cde.HouseGuid); if (house == null) { throw new FlaException("no house found"); } HouseholdData householdData = null; if (_housesToBeCreated.Count > 0) { var houseJob = _housesToBeCreated.FirstOrDefault(x => x.House.HouseGuid == ha.HouseGuid); if (houseJob == null) { parameters.HouseComponentResultObject.CarStatus += ", no house"; throw new FlaException("no house found"); } householdData = houseJob.House.Households.FirstOrDefault(x => x.HouseholdGuid == flaHousehold.HouseholdKey); if (householdData == null) { parameters.HouseComponentResultObject.CarStatus += ", no household guid"; throw new FlaException("no household for household guid " + cde.HouseholdGuid); } //set lpg parameters householdData.UseElectricCar = ElectricCarUse.UseElectricCar; //find number of cars var householdCars = _dbDto.Cars.Where(x => x.HouseholdGuid == cde.HouseholdGuid).ToList(); switch (householdCars.Count) { case 1: //use lpg profile for a single car householdData.TransportationDeviceSet = TransportationDevicesTwoCar; break; case 2: //use lpg profile for a single car householdData.TransportationDeviceSet = TransportationDevicesTwoCar; break; case 3: // use lpg profile for a single car // todo: fix this and put in the right transportation device set householdData.TransportationDeviceSet = TransportationDevicesTwoCar; break; case 4: //use lpg profile for a single car //todo: fix this and put in the right transportation device set householdData.TransportationDeviceSet = TransportationDevicesTwoCar; break; default: throw new FlaException("Household with " + householdCars.Count + " cars is missing"); } householdData.TravelRouteSet = TravelRouteSet; householdData.ChargingStationSet = ChargingStationSet; if (householdData.TransportationDistanceModifiers == null) { householdData.TransportationDistanceModifiers = new List <TransportationDistanceModifier>(); } householdData.TransportationDistanceModifiers.Add(new TransportationDistanceModifier("Work", "Car", cde.CommutingDistance * 1000)); householdData.TransportationDistanceModifiers.Add(new TransportationDistanceModifier("Entertainment", "Car", cde.FreizeitDistance * 1000)); parameters.HouseComponentResultObject.CarStatus += ", asking for repare with distances commuting: " + cde.CommutingDistance + ", free time: " + cde.FreizeitDistance + " km"; } if (Services.RunningConfig.LpgPrepareMode == LpgPrepareMode.PrepareWithFullLpgLoad) { Profile lpgProfile = null; try { lpgProfile = _lpgProfileLoader.LoadLPGProfile(parameters, ha.Trafokreis, LoadtypetoSearchFor, _saveableEntry, flaHousehold.HouseholdKey, out _, house.ComplexName, Services.RunningConfig); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { #pragma warning restore CA1031 // Do not catch general exception types Error("trying to load lpg profile: " + ex.Message); parameters.HouseComponentResultObject.LPGErrors = "trying to load lpg profile failed: " + ex.Message; } // ReSharper disable PossibleNullReferenceException if (_housesToBeCreated.Count > 0) { if (lpgProfile != null) { householdData.IsCarProfileCalculated = true; parameters.HouseComponentResultObject.ProcessingStatus = "Found LPG profile on full check"; } else { householdData.IsCarProfileCalculated = false; parameters.HouseComponentResultObject.ProcessingStatus = "Missing LPG profile on full check"; } } // ReSharper restore PossibleNullReferenceException } else if (Services.RunningConfig.LpgPrepareMode == LpgPrepareMode.PrepareWithOnlyNamecheck) { // ReSharper disable PossibleNullReferenceException if (_housesToBeCreated.Count > 0) { if (_saveableEntry.CheckForName(flaHousehold.HouseholdKey, Services.Logger)) { householdData.IsHouseholdProfileCalculated = true; parameters.HouseComponentResultObject.ProcessingStatus = "Found LPG profile on name check"; } else { householdData.IsHouseholdProfileCalculated = false; parameters.HouseComponentResultObject.ProcessingStatus = "Miissing LPG profile on name check"; } } // ReSharper restore PossibleNullReferenceException } else { throw new FlaException("Unknown lpg prepare mode"); } return(true); }
public void RunTest() { CompositeResolver.RegisterAndSetAsDefault(NativeDateTimeResolver.Instance, StandardResolver.Instance); PrepareUnitTest(); Config.Directories.ResultStorageDirectory = WorkingDirectory.Dir; Config.Directories.CalcServerLpgDirectory = WorkingDirectory.Dir; // ReSharper disable twice AssignNullToNotNullAttribute HouseCreationAndCalculationJob hcj = new HouseCreationAndCalculationJob(Scenario.Present().ToString(), "2017", "trafokreis"); HouseData hd = new HouseData("houseguid", "HT01", 1000, 1000, "houseName"); HouseholdData hhd = new HouseholdData("householdguid", 2000, ElectricCarUse.UseElectricCar, "householdname", ElectricCarProvider.ChargingStationSet, ElectricCarProvider.TransportationDevicesOneCar, ElectricCarProvider.TravelRouteSet, new List <TransportationDistanceModifier>(), HouseholdDataSpecifictionType.ByPersons); hd.Households.Add(hhd); hhd.UseElectricCar = ElectricCarUse.UseElectricCar; hhd.TransportationDeviceSet = ElectricCarProvider.TransportationDevicesOneCar; hhd.TravelRouteSet = ElectricCarProvider.TravelRouteSet; hhd.ChargingStationSet = ElectricCarProvider.ChargingStationSet; hhd.HouseholdDataPersonSpecification = new HouseholdDataPersonSpecification(new List <PersonData> { new PersonData(30, Gender.Male) }); hcj.House = hd; List <HouseCreationAndCalculationJob> houseJobs = new List <HouseCreationAndCalculationJob>(); houseJobs.Add(hcj); FileHelpers.CopyRec(Config.Directories.LPGReleaseDirectory, WorkingDirectory.Dir, Logger, true); var endTime = new DateTime(Constants.PresentSlice.DstYear, 1, 10); ProfileGenerationRo pgro = new ProfileGenerationRo(); HouseProcessor.WriteDistrictsForLPG(houseJobs, WorkingDirectory.DirDi, Logger, Constants.PresentSlice, endTime, pgro); string districtsDir = WorkingDirectory.Combine("Districts"); var districtsDi = new DirectoryInfo(districtsDir); var files = districtsDi.GetFiles("*.json"); void RunOneFile(FileInfo myfi) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = WorkingDirectory.Combine("simulationengine.exe"); psi.UseShellExecute = true; psi.WorkingDirectory = WorkingDirectory.Dir; psi.Arguments = "ProcessHouseJob -j \"" + myfi.FullName + "\""; Info("running " + psi.FileName + " " + psi.Arguments); using (Process p = new Process()) { p.StartInfo = psi; p.Start(); p.WaitForExit(); } } foreach (var housejob in files) { RunOneFile(housejob); } DBDto dbDto = new DBDto(new List <House>(), new List <Hausanschluss>(), new List <Car>(), new List <Household>(), new List <RlmProfile>()); CachingLPGProfileLoader ca = new CachingLPGProfileLoader(Logger, dbDto); List <int> isns = new List <int>(); isns.Add(10); CarDistanceEntry cde = new CarDistanceEntry("houseguid", "householdguid", "carguid", 20, 20, isns, 10, "haguid", "sourceguid", "cdename", CarType.Electric); HouseComponentRo hcro = new HouseComponentRo("housecomponent", "componeenttype", 1000, 200, "processingstatus", "isns", "standort", 0); ProviderParameterDto ppd = new ProviderParameterDto(cde, WorkingDirectory.Dir, hcro); SqlConnectionPreparer scp = new SqlConnectionPreparer(Config); MyDb db = scp.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice); SaveableEntry <Profile> sa = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.LPGProfile, Logger); sa.MakeTableForListOfFieldsIfNotExists(true); string dstDir = Path.Combine(WorkingDirectory.Dir, hcj.Trafokreis, hcj.House.Name); FileHelpers.CopyRec(WorkingDirectory.Combine("Results"), dstDir, Logger, true); //normal electricity test and cache test Info("================== "); Info("electricity"); Info("================== "); var profElec1 = ca.LoadLPGProfile(ppd, hcj.Trafokreis, "Electricity", sa, hhd.HouseholdGuid, out var profsource, hcj.House.Name, Config, true); Info("Source: " + profsource); Assert.NotNull(profElec1); Assert.NotNull(profsource); var profElecCache = ca.LoadLPGProfile(ppd, hcj.Trafokreis, "Electricity", sa, hhd.HouseholdGuid, out var profsourceCache, hcj.House.Name, Config, true); Info("Source 2: " + profsourceCache); Assert.NotNull(profsourceCache); Assert.NotNull(profsource); profElec1.Should().BeEquivalentTo(profElecCache, options => options.Excluding(ctx => ctx.SelectedMemberPath.EndsWith("BinaryProfile"))); //Car Charging Electricity electricity test and cache test Info("================== "); Info("Car Charging Electricity electricity"); Info("================== "); SaveableEntry <Profile> sa2 = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.EvProfile, Logger); sa2.MakeCleanTableForListOfFields(true); var prof2 = ca.LoadLPGProfile(ppd, hcj.Trafokreis, "Car Charging Electricity", sa2, hhd.HouseholdGuid, out var profsource2, hcj.House.Name, Config, true); Info("Source Wp 1: " + profsource2); Assert.NotNull(prof2); Assert.NotNull(profsource2); var prof3 = ca.LoadLPGProfile(ppd, hcj.Trafokreis, "Car Charging Electricity", sa2, hhd.HouseholdGuid, out var profsource3, hcj.House.Name, Config, true); Info("Source Wp 2: " + profsource3); Assert.NotNull(prof3); Assert.NotNull(profsource3); prof2.Should().BeEquivalentTo(prof3, options => options.Excluding(ctx => ctx.SelectedMemberPath.EndsWith("BinaryProfile"))); }
protected override void RunActualProcess() { var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice); var dbHousesPersistence = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice, DatabaseCode.Persistence); dbHousesPersistence.CreateTableIfNotExists <PersistentOutgoingCommuterEntry>(); dbHouses.RecreateTable <OutgoingCommuterEntry>(); dbHouses.RecreateTable <CarDistanceEntry>(); var dbRaw = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice); var outGoings = dbRaw.Fetch <OutgoingCommuterSummary>(); var households = dbHouses.Fetch <Household>(); var residents = households.SelectMany(x => x.Occupants).ToList().AsReadOnly(); if (residents.Count == 0) { throw new Exception("No occupants found"); } var persistentOutgoingCommuters = dbHousesPersistence.Fetch <PersistentOutgoingCommuterEntry>(); var cars = dbHouses.Fetch <Car>(); var householdGuids = households.Select(x => x.Guid).ToList(); foreach (var car in cars) { if (!householdGuids.Contains(car.HouseholdGuid)) { throw new FlaException("car with invalid household guid"); } } if (cars.Count < 1000) { throw new FlaException("Not a single car was loaded"); } Debug("total planned commuters before persistence: " + outGoings.Sum(x => x.Erwerbstätige)); int idx = 0; int deletecount = 0; dbHouses.BeginTransaction(); dbHousesPersistence.BeginTransaction(); foreach (var persistentOutgoingCommuterEntry in persistentOutgoingCommuters) { var household = households.FirstOrDefault(x => x.HouseholdKey == persistentOutgoingCommuterEntry.HouseholdKey); if (household == null) { dbHousesPersistence.Delete(persistentOutgoingCommuterEntry); continue; } Car car = cars.FirstOrDefault(x => x.HouseholdGuid == household.Guid); if (car == null && persistentOutgoingCommuterEntry.CommuntingMethod == CommuntingMethod.Car) { Debug("deleted inconsistent commuter " + deletecount++); dbHousesPersistence.Delete(persistentOutgoingCommuterEntry); continue; } OutgoingCommuterEntry ogce = new OutgoingCommuterEntry(Guid.NewGuid().ToString(), household.Guid, persistentOutgoingCommuterEntry.DistanceInKm, persistentOutgoingCommuterEntry.CommuntingMethod, persistentOutgoingCommuterEntry.WorkCity, persistentOutgoingCommuterEntry.WorkKanton, household.HouseGuid); dbHouses.Save(ogce); var outgoing = outGoings.Single(x => x.Arbeitsgemeinde == persistentOutgoingCommuterEntry.WorkCity && x.Arbeitskanton == persistentOutgoingCommuterEntry.WorkKanton); outgoing.Erwerbstätige--; if (outgoing.Erwerbstätige < 0) { throw new FlaException("Negative workers"); } if (car != null) { //bus & bahn cars.Remove(car); CarDistanceEntry cde = new CarDistanceEntry(household.HouseGuid, household.Guid, car.Guid, persistentOutgoingCommuterEntry.DistanceInKm, 27.4, household.OriginalISNs, household.FinalIsn, household.HausAnschlussGuid, Guid.NewGuid().ToString(), "Car " + (idx++), car.CarType); dbHouses.Save(cde); } } dbHouses.CompleteTransaction(); dbHousesPersistence.CompleteTransaction(); Debug("total planned commuters after persistence: " + outGoings.Sum(x => x.Erwerbstätige)); var outgoingCommuters = new List <OutgoingCommuterEntry>(); foreach (var commuter in outGoings) { for (var i = 0; i < commuter.Erwerbstätige; i++) { var oce = new OutgoingCommuterEntry { CommuterGuid = Guid.NewGuid().ToString() }; var method = Services.Rnd.NextDouble(); if (method < 0.52) { oce.CommuntingMethod = CommuntingMethod.Car; } else { oce.CommuntingMethod = CommuntingMethod.PublicTransport; } oce.DistanceInKm = commuter.Entfernung; oce.WorkCity = commuter.Arbeitsgemeinde; oce.WorkKanton = commuter.Arbeitskanton; outgoingCommuters.Add(oce); } } //freizeit km nach mikromobilitätszensus, 12.05 auto-km/tag/person, 0.44 autos pro person = 27.39 km/tag var potentialResidents = residents.Where(x => x.Age > 18 && x.Age < 65).ToList(); var householdGuidsWithCar = cars.Select(x => x.HouseholdGuid).ToList(); var potentialResidentsWithCar = potentialResidents.Where(x => householdGuidsWithCar.Contains(x.HouseholdGuid)).ToList(); var potentialResidentsWithoutCar = potentialResidents.Where(x => !potentialResidentsWithCar.Contains(x)).ToList(); List <Car> availableCars = cars.ToList(); //assign the commuters List <OutgoingCommuterEntry> processedOutgoingCommuters = new List <OutgoingCommuterEntry>(); dbHouses.BeginTransaction(); while (outgoingCommuters.Count > 0) { OutgoingCommuterEntry ogce = outgoingCommuters[0]; processedOutgoingCommuters.Add(ogce); outgoingCommuters.RemoveAt(0); Occupant occupant = null; if (ogce.CommuntingMethod == CommuntingMethod.Car) { Car myCar = null; while (myCar == null) { occupant = potentialResidentsWithCar[Services.Rnd.Next(potentialResidentsWithCar.Count)]; potentialResidentsWithCar.Remove(occupant); myCar = availableCars.FirstOrDefault(x => x.HouseholdGuid == occupant.HouseholdGuid); } availableCars.Remove(myCar); Household hh = households.FirstOrDefault(x => x.Guid == occupant.HouseholdGuid); if (hh == null) { throw new FlaException("No household for " + ogce.HouseholdGuid); } CarDistanceEntry cd = new CarDistanceEntry(occupant.HouseGuid, occupant.HouseholdGuid, myCar.Guid, ogce.DistanceInKm, 27.4, hh.OriginalISNs, hh.FinalIsn, hh.HausAnschlussGuid, Guid.NewGuid().ToString(), (idx++).ToString(), myCar.CarType); dbHouses.Save(cd); } else { if (potentialResidentsWithoutCar.Count > 0) { occupant = potentialResidentsWithoutCar[Services.Rnd.Next(potentialResidentsWithoutCar.Count)]; potentialResidentsWithoutCar.Remove(occupant); } else { occupant = potentialResidentsWithCar[Services.Rnd.Next(potentialResidentsWithCar.Count)]; potentialResidentsWithCar.Remove(occupant); } } ogce.HouseholdGuid = occupant.HouseholdGuid; ogce.HouseGuid = occupant.HouseGuid; ogce.CommuterGuid = Guid.NewGuid().ToString(); } //the other cars are only used for freizeit / shopping / etc foreach (Car car in availableCars) { Household hh = households.Single(x => x.Guid == car.HouseholdGuid); CarDistanceEntry cd = new CarDistanceEntry(car.HouseGuid, car.HouseholdGuid, car.Guid, 0, 27.4, hh.OriginalISNs, hh.FinalIsn, hh.HausAnschlussGuid, Guid.NewGuid().ToString(), "Car " + (idx++), car.CarType); dbHouses.Save(cd); } foreach (var entry in processedOutgoingCommuters) { if (string.IsNullOrWhiteSpace(entry.HouseholdGuid)) { throw new FlaException("Household guid was empty"); } var household = households.Single(x => x.Guid == entry.HouseholdGuid); PersistentOutgoingCommuterEntry poce = new PersistentOutgoingCommuterEntry(household.HouseholdKey, entry.DistanceInKm, entry.CommuntingMethod, entry.WorkCity, entry.WorkKanton); dbHousesPersistence.Save(poce); dbHouses.Save(entry); } dbHousesPersistence.CompleteTransaction(); dbHouses.CompleteTransaction(); }
protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice) { var dbSrcHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice.PreviousSliceNotNull); var dbDstHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice); var households = dbDstHouses.FetchAsRepo <Household>(); dbDstHouses.RecreateTable <OutgoingCommuterEntry>(); dbDstHouses.RecreateTable <CarDistanceEntry>(); var srcCarDistanceEntries = dbSrcHouses.FetchAsRepo <CarDistanceEntry>(); var cars = dbDstHouses.FetchAsRepo <Car>(); var srcCars = dbSrcHouses.FetchAsRepo <Car>(); srcCarDistanceEntries.Count.Should().Be(srcCars.Count); if (srcCarDistanceEntries.Count == 0) { throw new Exception("No car distances were found"); } var carGuids = cars.ToGuidHashset(); //remove old car distance entries List <CarDistanceEntry> toDelete = new List <CarDistanceEntry>(); foreach (var entry in srcCarDistanceEntries) { if (!carGuids.Contains(entry.CarGuid)) { toDelete.Add(entry); } } foreach (var cde in toDelete) { srcCarDistanceEntries.Remove(cde); } //add entries for new cars var usedCarGuids = srcCarDistanceEntries.ToReferenceGuidHashset(x => x.CarGuid); var carsWithoutCde = new List <Car>(); foreach (var car in cars) { if (!usedCarGuids.Contains(car.Guid)) { carsWithoutCde.Add(car); } } double avgCommuningDistance = srcCarDistanceEntries.Average(x => x.CommutingDistance); double avgFreizeitDistance = srcCarDistanceEntries.Average(x => x.FreizeitDistance); int newCarcount = 1; foreach (var car in carsWithoutCde) { var household = households.GetByGuid(car.HouseholdGuid); string name = "Car " + newCarcount + " " + slice; CarDistanceEntry cde = new CarDistanceEntry(car.HouseGuid, car.HouseholdGuid, car.Guid, avgCommuningDistance, avgFreizeitDistance, new List <int>(), household.FinalIsn, household.HausAnschlussGuid, Guid.NewGuid().ToString(), name, car.CarType); newCarcount++; srcCarDistanceEntries.Add(cde); } //change car types foreach (var carDistance in srcCarDistanceEntries) { var car = cars.GetByGuid(carDistance.CarGuid); carDistance.HouseholdGuid = car.HouseholdGuid; carDistance.CarType = car.CarType; } srcCarDistanceEntries.Count.Should().Be(cars.Count); srcCarDistanceEntries.SaveAll(dbDstHouses, true, true); }