void buildMark() { M = Matrix <double> .Build.Dense(petriNet.Places, 1); Buffers.ForEach(b => b.clearConnected()); //machines and buffers for (int m = 0; m < Machines; m++) { M[firstMachineAddress + m, 0] = 1; M[firstMachineAddress + Machines + m, 0] = Buffers.First(i => i.Number == m + 1).Capacity; Buffers.First(b => b.Number == m + 1).Address = firstMachineAddress + Machines + m; } //inputs int place = 0; foreach (var process in Processes) { var output = getCorrespondingOutput(process.Number); M[place, 0] = Inputs.First(i => i.Number == process.Number).Value; Inputs.First(i => i.Number == process.Number).Address = place; Inputs.First(i => i.Number == process.Number).CorrespondingOutput = output; place += process.Operations.Count * 3 + 2; foreach (var o in process.Operations) { Buffers.First(i => i.Number == o.MachineNumber).Connect(process, o.Number); } } }
public bool bufferCapacityInLimits(int bufferAddress, Matrix <double> M) { var buffer = Buffers.First(i => i.Address == bufferAddress); if (M[bufferAddress, 0] + 1 > buffer.Capacity) { return(false); } return(true); }
public Block RemoveBlock(int stackId) { if (stackId == Production.Id) { return(Production.Blocks.Pop()); } else { return(Buffers.First(b => b.Id == stackId).Blocks.Pop()); } }
public void AddBlock(int stackId, Block block) { if (stackId != Handover.Id && stackId != Production.Id) { Buffers.First(b => b.Id == stackId).Blocks.Push(block); } else { // Production should never be a target // If handover is the target, pretend the Block dissappears immediatly } }
public double CalculateReward(int handovers, long leftoverDueTime) { double reward = 0; List <int> currentBuffer = new List <int>(); foreach (var buffer in Buffers) { currentBuffer.Add(buffer.Blocks.Count); var highestReadyIndex = -1; var distToTop = 0; var bufferList = buffer.Blocks.ToArray(); for (int i = 0; i < buffer.Blocks.Count; i++) { var block = bufferList[i]; if (block.Ready) { highestReadyIndex = i; distToTop = 0; } else { distToTop++; } } if (highestReadyIndex != -1) { reward -= 10 * distToTop; } } var stdDev = currentBuffer.StdDev(); var maxStdDev = new List <int> { 0, Buffers.First().MaxHeight }.StdDev(); var bufferReward = (1 - (stdDev / maxStdDev)) * 10; reward += bufferReward; reward += 10 * (Production.MaxHeight - Production.Blocks.Count); if (Handover.Blocks.Count > 0) { reward += 500 + Handover.Blocks.First().Due.MilliSeconds; } reward += 500 * handovers + leftoverDueTime; return(reward); }
public bool defineBufferCapacity(int buffer, int capacity) { if (buffer > Machines) { return(false); } else { if (Buffers.Any(i => i.Number == buffer)) { Buffers.First(i => i.Number == buffer).Capacity = capacity; } else { Buffers.Add(new Buffer(buffer, capacity)); } return(true); } }
public string printCurrentBufferProcesses(int number) { var buffer = Buffers.First(i => i.Number == number); string result = "B" + number + ": "; if (buffer.ActiveProcesses.Count == 0) { result += $"0 / {buffer.Capacity}"; } else { int sum = 0; string tmp = ""; foreach (var p in buffer.ActiveProcesses) { sum += p.Count; tmp += "P" + p.process.Number + ": " + p.Count + " "; } result += $"{sum} / {buffer.Capacity}, " + tmp; } return(result); }
public int getBufferCapacity(int buffer) { return(Buffers.First(i => i.Number == buffer).Capacity); }
public bool transitionIsSafe(Vector <double> T, int number, Matrix <double> M, Matrix <double> MM, Transition Tr) { int fromBufferNumber = transitionFromBuffer(T); int toBufferNumber = transitionToBuffer(T); updateBuffers(Tr); if (fromBufferNumber > 0) { int processNumber = getProcessNumber(number - 1); var process = getProcess(number - 1); var operation = getOperation(process, number - 1); var currentBuffer = Buffers.First(i => i.Number == fromBufferNumber); var prevBuffer = getOperation(process, number - 2).MachineNumber; //last operation in process if (operation.Number == process.Operations.Count) { revertBuffers(Tr); return(true); } if (bufferNotFull(currentBuffer, MM)) { revertBuffers(Tr); return(true); } List <ProcessConnected> ProcessesToCheck = new List <ProcessConnected>(); List <int> CheckedBuffers = new List <int>(); CheckedBuffers.Add(currentBuffer.Number); ProcessesToCheck.Add(new ProcessConnected(process, operation.Number)); foreach (var ap in currentBuffer.ActiveProcesses) { foreach (var ao in ap.ActiveOperations) { var newcheck = new ProcessConnected(ap.process, ao); if (!ProcessesToCheck.Any(c => c.fromOperationNumber == ao && c.process == ap.process)) { ProcessesToCheck.Add(newcheck); } } } for (int i = 0; i < ProcessesToCheck.Count; i++) { var proc = ProcessesToCheck.ElementAt(i); foreach (var op in proc.process.Operations.Where(j => j.Number > proc.fromOperationNumber).OrderBy(j => j.Number)) { var buffer = Buffers.First(l => l.Number == op.MachineNumber); if (bufferFull(buffer, MM) && !buffer.ActiveProcesses.Any(a => a.process.Number == proc.process.Number)) { if (ProcessesToCheck.Count == i + 1) { revertBuffers(Tr); return(false); } break; } if (CheckedBuffers.Any(b => b == buffer.Number) && ProcessesToCheck.Count == i + 1) { revertBuffers(Tr); return(false); } if (CheckedBuffers.Any(b => b == buffer.Number)) { break; //revertBuffers(Tr); //return false; } if (bufferNotFull(buffer, MM)) { revertBuffers(Tr); return(true); } CheckedBuffers.Add(buffer.Number); foreach (var ap in buffer.ActiveProcesses) { foreach (var ao in ap.ActiveOperations) { var newcheck = new ProcessConnected(ap.process, ao); if (!ProcessesToCheck.Any(c => c.fromOperationNumber == ao && c.process == ap.process)) { ProcessesToCheck.Add(newcheck); } } } } } revertBuffers(Tr); return(false); } revertBuffers(Tr); return(true); }