public async Task <List <AgentStatistic> > RunSim(int simulationId, int simulationNumber) { AgentStatistic.Log = new List <string>(); Debug.WriteLine("Simulation Starts"); _messageHub.SendToAllClients("Simulation starts..."); using (var context = new SimulationContext(isDefaultContextForProcess: true)) { // initialise the model var system = await CreateModel(context : context, simulationId : simulationId, simNr : simulationNumber); // instantate a new simulator var simulator = new Simulator(); // run the simulation await simulator.SimulateAsync(0); // Debug Debuglog(simulationContext: context, productionDomainContextContext: _productionDomainContext, simNr: simulationNumber, simId: simulationId); var simulationNr = _productionDomainContext.GetSimulationNumber(simulationId, SimulationType.Decentral); Statistics.UpdateSimulationId(simulationId, SimulationType.Decentral, simulationNumber); _productionDomainContext.SimulationWorkschedules.AddRange(SimulationWorkschedules); _productionDomainContext.SaveChanges(); SaveStockExchanges(simulationId, simulationNr, context); //UpdateStockExchanges(simulationId, simulationNr); } return(Agent.AgentStatistics); }
/// <summary> /// Call this method for a complete central simulation. /// </summary> /// <param name="simulationId"></param> /// <returns></returns> public async Task Simulate(int simulationId) { await Task.Run(async() => { var simNumber = _context.GetSimulationNumber(simulationId, SimulationType.Central); var simConfig = _context.SimulationConfigurations.Single(a => a.Id == simulationId); simConfig.Time = 0; var timeTable = InitializeSimulation(simulationId, simNumber, simConfig).Result; //create list of items waiting to be produced var waitingItems = _context.ProductionOrderWorkSchedules.Where(a => a.ProducingState == ProducingState.Created || a.ProducingState == ProducingState.Waiting).ToList(); //timeTable = CreateInjectionOrders(timeTable); var itemCounter = 0; //start the actual simulation while (timeTable.Timer < simConfig.SimulationEndTime) { timeTable = await ProcessTimeline(timeTable, waitingItems, simulationId, simNumber); if (itemCounter == timeTable.Items.Count) { continue; } itemCounter = timeTable.Items.Count; _messageHub.SendToAllClients(itemCounter + "/" + (timeTable.Items.Count + waitingItems.Count) + " items processed. Time: " + timeTable.Timer); _processMrp.UpdateDemandsAndOrders(simulationId); } FinishSimulation(timeTable, simulationId, simNumber, simConfig); }); }
public async Task AgentSimulatioAsync(int simulationConfigurationId) { // In-memory database only exists while the connection is open var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionStringBuilder.ToString()); // create OptionsBuilder with InMemmory Context var builder = new DbContextOptionsBuilder <MasterDBContext>(); builder.UseSqlite(connection); var simNumber = _evaluationContext.GetSimulationNumber(simulationConfigurationId, SimulationType.Decentral); using (var c = new InMemoryContext(builder.Options)) { c.Database.OpenConnection(); c.Database.EnsureCreated(); InMemoryContext.LoadData(_evaluationContext, c); var sim = new AgentSimulation(c, _messageHub); await sim.RunSim(simulationConfigurationId, simNumber); CopyResults.Copy(c, _evaluationContext, simulationConfigurationId, simNumber, SimulationType.Decentral); CalculateKpis.MachineSattleTime(_evaluationContext, simulationConfigurationId, SimulationType.Decentral, simNumber); CalculateKpis.CalculateAllKpis(_evaluationContext, simulationConfigurationId, SimulationType.Decentral, simNumber, true); } connection.Close(); _messageHub.EndSimulation("Simulation with Id:" + simulationConfigurationId + " Completed." , simulationConfigurationId.ToString() , simNumber.ToString()); }
private void SaveContext(ProductionDomainContext _evaluationContext) { foreach (var stockexchange in _context.StockExchanges) { stockexchange.SimulationConfigurationId = 1; stockexchange.SimulationNumber = _context.GetSimulationNumber(stockexchange.SimulationConfigurationId, SimulationType.Central); stockexchange.SimulationType = SimulationType.Central; var name = _context.Stocks.Single(b => b.Id == stockexchange.StockId).Name; stockexchange.StockId = _evaluationContext.Stocks.Single(a => a.Name.Equals(name)).Id; var exchange = new StockExchange(); stockexchange.CopyDbPropertiesTo(exchange); _evaluationContext.StockExchanges.Add(exchange); } _evaluationContext.SaveChanges(); }
/// <summary> /// Call this method for a single run without simulation. /// </summary> /// <param name="task"></param> /// <param name="simulationId"></param> /// <returns></returns> public async Task InitializeMrp(MrpTask task, int simulationId) { await Task.Run(async() => { _messageHub.SendToAllClients("Prepare InMemory Tables...", MessageType.info); rebuildNets = new RebuildNets(_context); capacityScheduling = new CapacityScheduling(_context); _context = InMemoryContext.CreateInMemoryContext(); InMemoryContext.LoadData(_evaluationContext, _context); await PrepareSimulationContext(); var simConfig = _context.SimulationConfigurations.Single(x => x.Id == simulationId); await OrderGenerator.GenerateOrders(_context, simConfig, simulationId); var simulationNumber = _context.GetSimulationNumber(simulationId, SimulationType.Central); //call initial central MRP-run await _processMrp.CreateAndProcessOrderDemand(task, _context, simulationId, _evaluationContext); if (task == MrpTask.Backward || task == MrpTask.Forward || task == MrpTask.GifflerThompson) { var list = _context.ProductionOrderWorkSchedules.Select(schedule => new PowsSimulationItem(_context) { End = (task == MrpTask.Backward) ? schedule.EndBackward : (task == MrpTask.Forward) ? schedule.EndForward : schedule.End, Start = (task == MrpTask.Backward) ? schedule.StartBackward : (task == MrpTask.Forward) ? schedule.StartForward : schedule.Start, ProductionOrderId = schedule.ProductionOrderId, ProductionOrderWorkScheduleId = schedule.Id, SimulationId = simulationId }) .ToList(); _evaluationContext.SaveChanges(); FillSimulationWorkSchedules(list, simulationId, simulationNumber, task); } SimulationType simType = SimulationType.Central; switch (task) { case MrpTask.Forward: simType = SimulationType.ForwardPlanning; break; case MrpTask.Backward: simType = SimulationType.BackwardPlanning; break; } CopyResults.ExtractSimulationOrders(_context, _evaluationContext, simulationId, simulationNumber, simType); _messageHub.EndScheduler(); }); }