public void Run(JobShopInstance instance)
        {
            var result = new List <Edge>();
            Dictionary <string, PartialBranchAndBoundResult> selectedMachines = new Dictionary <string, PartialBranchAndBoundResult>();
            var i = 0;

            while (true)
            {
                GenerateParametersForNodes(instance);
                var machines = instance.GetMachines();
                if (selectedMachines.Count == machines.Count)
                {
                    break;                                          //exit
                }
                Dictionary <string, PartialBranchAndBoundResult> minimalLatenessPerMachine = new Dictionary <string, PartialBranchAndBoundResult>();
                machines.RemoveAll(m => selectedMachines.ContainsKey(m));
                foreach (var machine in machines)
                {
                    var jobsPerMachine         = instance.Graph.AllNodes.Where(n => n.Machine == machine).ToList().DeepClone();
                    var partialResults         = new List <PartialBranchAndBoundResult>();
                    var machineMinimumLateness = BranchAndBound(jobsPerMachine.DeepClone(), new List <Node>(), partialResults); // start BranchAndBound
                    minimalLatenessPerMachine.Add(machine, machineMinimumLateness);
                }
                var maxLateness     = minimalLatenessPerMachine.Max(k => k.Value.MinimalPossibleLateness);
                var selectedMachine = minimalLatenessPerMachine.First(m => m.Value.MinimalPossibleLateness == maxLateness);
                selectedMachines.Add(selectedMachine.Key, selectedMachine.Value);
                instance.Graph.AddArcs(selectedMachine.Value.SelectedNodes);
                PrintJobInstance(instance, i++);
            }

            PrintSolutionForMachines(selectedMachines, instance);
        }
        private void GetJobsValues(List <string> jobs, JobShopInstance instance)
        {
            var jobsInFile = new List <List <string> >();

            foreach (var j in jobs)
            {
                jobsInFile.Add(j.Split(' ').ToList());
            }

            foreach (var jobTask in instance.Jobs.SelectMany(j => j.JobTasks))
            {
                try
                {
                    var jobInFile = jobsInFile.FirstOrDefault(jif => jif[0] == jobTask.TaskName);
                    if (jobInFile == null)
                    {
                        throw new ArgumentException("There is no Job definition in file for jobName : " + jobTask.TaskName);
                    }
                    jobTask.MachineName   = jobInFile[2];
                    jobTask.ExecutionTime = int.Parse(jobInFile[1]);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Cannot Parse Values for job task : " + jobTask.TaskName);
                    throw;
                }
            }
        }
 private void PrintJobInstance(JobShopInstance newInstance, int i)
 {
     Console.WriteLine("Graph For iteration : " + i);
     foreach (var edge in newInstance.Graph.Edges)
     {
         Console.WriteLine(edge.StartNode.Name + " => " + edge.Weight + " => " + edge.EndNode.Name);
     }
 }
        public JobShopInstance ParseFileToGraph(string filePath)
        {
            var lines     = File.ReadAllLines(filePath, Encoding.UTF8);
            var instance  = new JobShopInstance();
            var jobAmount = GetJobsAmount(lines[0]);

            instance.Jobs = GetJobs(lines.Skip(1).Take(jobAmount).ToList());
            GetJobsValues(lines.Skip(jobAmount + 1).ToList(), instance);
            instance.GenerateGraph();
            return(instance);
        }
 private void SaveToFile(Dictionary <string, PartialBranchAndBoundResult> selectedMachines,
                         JobShopInstance instance)
 {
     using StreamWriter outputFile = new StreamWriter("Result.txt");
     foreach (var(key, value) in selectedMachines)
     {
         outputFile.WriteLine(key);
         foreach (var line in value.SelectedNodes.Select(s => GetNode(s.Name, instance).Name + " : Release Time -  " + GetNode(s.Name, instance).ReleaseTime + " ; End Time : " + (GetNode(s.Name, instance).ReleaseTime + GetNode(s.Name, instance).ExecutionTime)).ToList())
         {
             outputFile.WriteLine(line);
         }
     }
     outputFile.Flush();
 }
        // used prncipal that it will be always Acyclic Graph
        private void GenerateParametersForNodes(JobShopInstance instance)
        {
            var bellmanFord      = new BellmanFordAlgorithm(instance.Graph);
            var maximumdistances = bellmanFord.BellmanFord(instance.Graph.StartNode);

            instance.Makespan = maximumdistances[instance.Graph.EndNode];

            foreach (var node in instance.Graph.AllNodes)
            {
                node.ReleaseTime = maximumdistances[node];
            }

            foreach (var node in instance.Graph.AllNodes)
            {
                var longestPath = new BellmanFordAlgorithm(new Graph {
                    Edges = instance.Graph.TakeOnlyNextEdges(node), StartNode = node, EndNode = instance.Graph.EndNode
                }).BellmanFord(node).GetValueOrDefault(instance.Graph.EndNode);
                var dueDate = instance.Makespan - longestPath + node.ExecutionTime;
                node.DueDate = dueDate < 0 ? 0 : dueDate;
            }
        }
 private void PrintSolutionForMachines(Dictionary <string, PartialBranchAndBoundResult> selectedMachines, JobShopInstance instance)
 {
     foreach (var(key, value) in selectedMachines)
     {
         Console.WriteLine(key);
         foreach (var line in value.SelectedNodes.Select(s => GetNode(s.Name, instance).Name + " : Release Time -  " + GetNode(s.Name, instance).ReleaseTime + " ; End Time : " + (GetNode(s.Name, instance).ReleaseTime + GetNode(s.Name, instance).ExecutionTime)).ToList())
         {
             Console.WriteLine(line);
         }
     }
     SaveToFile(selectedMachines, instance);
 }
 //To much Deep Copies
 private Node GetNode(string name, JobShopInstance instance)
 {
     return(instance.Graph.AllNodes.First(n => n.Name == name));
 }