public async Task RunAkkaSimulation(Configuration configuration) { _configuration = configuration; _messageHub.SendToAllClients(msg: "Prepare in Memory model from DB for Simulation: " + _configuration.GetOption <SimulationId>().Value); _messageHub.SendToAllClients(msg: "Prepare Simulation"); _agentSimulation = new AgentSimulation(DBContext: _context , messageHub: _messageHub); // Defines the status output var simulation = await _agentSimulation.InitializeSimulation(configuration : _configuration); SimulationContext = simulation.SimulationContext; if (simulation.IsReady()) { _messageHub.StartSimulation(simId: _configuration.GetOption <SimulationId>().Value.ToString() , simNumber: _configuration.GetOption <SimulationNumber>().Value.ToString()); // Start simulation var sim = simulation.RunAsync(); _agentSimulation.StateManager.ContinueExecution(simulation); await sim; } _messageHub.EndSimulation(msg: "Simulation Completed." , simId: _configuration.GetOption <SimulationId>().Value.ToString() , simNumber: _configuration.GetOption <SimulationNumber>().Value.ToString()); return; }
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); }
public void AggregateResults(int simulationId, PerformContext consoleContext) { _messageHub = new ProcessingHub(consoleContext); var aggregator = new ResultAggregator(_resultContext); aggregator.BuildResults(simulationId); _messageHub.SendToAllClients("AggretationFinished for " + simulationId); }
public async Task RunAkkaSimulation(Configuration configuration) { _configuration = configuration; _messageHub.SendToAllClients(msg: "Prepare in Memory model from DB for Simulation: " + _configuration.GetOption <SimulationId>().Value.ToString() , msgType: MessageType.info); //In-memory database only exists while the connection is open //var _inMemory = InMemoryContext.CreateInMemoryContext(); // InMemoryContext.LoadData(source: _context, target: _inMemory); //MasterDBInitializerSimple.DbInitialize(_inMemory); _messageHub.SendToAllClients(msg: "Prepare Simulation", msgType: MessageType.info); _agentSimulation = new AgentSimulation(DBContext: _context , messageHub: _messageHub); // Defines the status output var simulation = _agentSimulation.InitializeSimulation(configuration: _configuration).Result; SimulationContext = simulation.SimulationContext; if (simulation.IsReady()) { _messageHub.SendToAllClients(msg: "Start Simulation ...", msgType: MessageType.info); // Start simulation var sim = simulation.RunAsync(); AgentSimulation.Continuation(inbox: _agentSimulation.SimulationConfig.Inbox , sim: simulation , collectors: new List <IActorRef> { _agentSimulation.StorageCollector , _agentSimulation.WorkCollector , _agentSimulation.ContractCollector }); await sim; } _messageHub.EndScheduler(); _messageHub.EndSimulation(msg: "Simulation Completed." , simId: _configuration.GetOption <SimulationId>().Value.ToString() , simNumber: _configuration.GetOption <SimulationNumber>().Value.ToString()); return; }
/// <summary> /// Plans all unplanned Orders with MRP I + II /// </summary> /// <param name="task"></param> /// <returns></returns> public async Task CreateAndProcessOrderDemand(MrpTask task, ProductionDomainContext context, int simulationId, ProductionDomainContext evaluationContext) { await Task.Run(() => { if (context != null) { _context = context; } var newOrdersAdded = false; _messageHub.SendToAllClients("Start full MRP cycle...", MessageType.info); //get all unplanned orderparts and iterate through them for MRP var simConfig = _context.SimulationConfigurations.Single(a => a.Id == simulationId); var maxAllowedTime = simConfig.Time + simConfig.MaxCalculationTime; //Todo: put together if problem is solved var orderParts3 = _context.OrderParts.Include(a => a.Order).Where(a => a.IsPlanned == false); var orderParts2 = orderParts3.Where(a => a.Order.CreationTime <= simConfig.Time); var orderParts = orderParts2.Where(a => a.Order.DueTime < maxAllowedTime).Include(a => a.Article).ToList(); if (orderParts.Any()) { newOrdersAdded = true; } foreach (var orderPart in orderParts.ToList()) { var demand = GetDemand(orderPart); _messageHub.SendToAllClients("running requirements for orderpart " + orderPart.Id); //run the requirements planning and backward/forward termination algorithm RunRequirementsAndTermination(demand, task, simulationId); } if (task == MrpTask.All || task == MrpTask.GifflerThompson || task == MrpTask.Capacity) { //run the capacity algorithm PlanCapacities(task, newOrdersAdded, simulationId, evaluationContext); _messageHub.SendToAllClients("Capacities are planned"); } //set all orderparts to be planned foreach (var orderPart in orderParts) { if (task == MrpTask.All || task == MrpTask.GifflerThompson) { orderPart.IsPlanned = true; } } _context.SaveChanges(); _messageHub.SendToAllClients("End of the latest calculated order: " + _context.ProductionOrderWorkSchedules?.Max(a => a.End)); }); }
public async Task PrepareAgents(SimulationConfiguration simConfig, int simNr) { await Tools.Simulation.OrderGenerator.GenerateOrders(_productionDomainContext, simConfig, simNr); foreach (var orderpart in _productionDomainContext.OrderParts .Include(x => x.Article) .Include(x => x.Order) .AsNoTracking()) { if (orderpart.Order.CreationTime == 0) { this.InstructionQueue.Enqueue(new InstructionSet { MethodName = SystemAgent.InstuctionsMethods.CreateContractAgent.ToString(), ObjectToProcess = orderpart, ObjectType = orderpart.GetType(), SourceAgent = this }); } else { long l = orderpart.Order.DueTime - (10 * 60); // 540 if (l < 0) { l = 0; } this.CreateAndEnqueueInstuction( methodName: SystemAgent.InstuctionsMethods.CreateContractAgent.ToString(), objectToProcess: orderpart, targetAgent: this, waitFor: l ); } } _messageHub.SendToAllClients("Agent-System ready for Work"); }
public async Task RunSimulation(int simulationId, int simulationNumber) { await _agentSimulation.RunSim(simulationId, simulationNumber); _messageHub.SendToAllClients("Number of involved Agents: " + Agent.AgentCounter.Count, MessageType.success); _messageHub.SendToAllClients("Number of instructions send: " + Agent.InstructionCounter, MessageType.success); var itemlist = from val in Agent.AgentStatistics group val by new { val.AgentType } into grouped select new { Agent = grouped.First().AgentType, ProcessingTime = grouped.Sum(x => x.ProcessingTime), Count = grouped.Count().ToString() }; foreach (var item in itemlist) { _messageHub.SendToAllClients(" Agent (" + Agent.AgentCounter.Count(x => x == item.Agent) + "): " + item.Agent + " -> Runtime: " + item.ProcessingTime + " Milliseconds with " + item.Count + " Instructions Processed", MessageType.warning); } //var jobs = itemlist.Count(x => x.) var jobs = AgentStatistic.Log.Count(x => x.Contains("Finished Work with")); _messageHub.SendToAllClients(jobs + " Jobs processed in " + (Agent.AgentStatistics.Max(x => x.Time) - 1) + " minutes", MessageType.success); _messageHub.SendToAllClients("Simulation Finished"); _messageHub.EndScheduler(); }
/// <summary> /// Collector Agent for Life data Aquesition /// </summary> /// <param name="actorPaths"></param> /// <param name="time">Current time span</param> /// <param name="debug">Parameter to activate Debug Messages on Agent level</param> public Collector(ActorPaths paths , ICollectorBehaviour collectorBehaviour , IMessageHub msgHub , Configuration configuration , long time , List <Type> streamTypes) : base(time: time, channels: streamTypes) { collectorBehaviour.Collector = this; Behaviour = collectorBehaviour; messageHub = msgHub; Config = configuration; actorPaths = paths; simulationId = Config.GetOption <SimulationId>(); simulationNumber = Config.GetOption <SimulationNumber>(); simulationKind = Config.GetOption <SimulationKind>(); saveToDB = Config.GetOption <SaveToDB>(); maxTime = Config.GetOption <SimulationEnd>().Value; messageHub.SendToAllClients(msg: "Collector initialized: " + Self.Path.ToStringWithAddress()); }
/// <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(); }); }