private List <ScheduledTask> Run() { Project project = CreateProject(); if (project == null) { MessageBox.Show("There are no Tasks to be processed."); return(new List <ScheduledTask>()); } SchedulingModel m = new SchedulingModel(true); m.Initialize(project); IDictionary <int, double> schedule = m.Solve(); //ProjectUtilities.PrintProjectSchedule(project, schedule); var results = new List <ScheduledTask>(); foreach (var taskSchedule in schedule) { ProjectScheduling.Task task = project.Tasks[taskSchedule.Key]; double start = Math.Round(taskSchedule.Value, 3); results.Add(new ScheduledTask(task, start, start + task.Duration)); } Dictionary <ProjectScheduling.Task, List <ScheduledTask> > bakn = new Dictionary <ProjectScheduling.Task, List <ScheduledTask> >(); foreach (var key in results) { if (bakn.ContainsKey(key.task)) { bakn[key.task].Add(key); } else { bakn.Add(key.task, new List <ScheduledTask>()); bakn[key.task].Add(key); } } //Console.WriteLine(ProjectUtilities.ToCsv(project, schedule)); return(results); }
public static Project CreateProject() { var context = new MedirmDBEntities(); var todasEncomendas = context.Encomenda.Where(x => x.Estado.ToLower() != "terminado"); var Tasks = new List <ProjectScheduling.Task>(); var Moldes = context.Maquina.Select(x => x.Molde).ToArray(); List <Resource> resources = new List <Resource>(); List <TaskDependency> links = new List <TaskDependency>(); foreach (var encomenda in todasEncomendas) { var todosArtigosdAsEncomendas = new List <Artigo>(); todosArtigosdAsEncomendas.AddRange(context.Artigo.Where(x => x.Nome == encomenda.Artigo)); foreach (var compoartigos in todosArtigosdAsEncomendas) { var todosComponents = new List <ComponentesDosArtigos>(); todosComponents.AddRange(context.ComponentesDosArtigos.Where(x => x.Artigo == "1")); foreach (var components in todosComponents) { var maquina1 = context.Maquina.FirstOrDefault(x => x.Nome == components.Maquina1); var maquina2 = context.Maquina.FirstOrDefault(x => x.Nome == components.Maquina2); var maquina3 = context.Maquina.FirstOrDefault(x => x.Nome == components.Maquina3); var maquina4 = context.Maquina.FirstOrDefault(x => x.Nome == components.Maquina4); var maquina5 = context.Maquina.FirstOrDefault(x => x.Nome == components.Maquina5); if (resources.FirstOrDefault(x => x.Name == maquina1.Tipo) == null) { resources.Add(new Resource(maquina1.Tipo, 1)); } if (maquina2 != null && resources.FirstOrDefault(x => x.Name == maquina2.Tipo) == null) { resources.Add(new Resource(maquina2.Tipo, 1)); } if (maquina3 != null && resources.FirstOrDefault(x => x.Name == maquina3.Tipo) == null) { resources.Add(new Resource(maquina3.Tipo, 1)); } if (maquina4 != null && resources.FirstOrDefault(x => x.Name == maquina4.Tipo) == null) { resources.Add(new Resource(maquina4.Tipo, 1)); } if (maquina5 != null && resources.FirstOrDefault(x => x.Name == maquina5.Tipo) == null) { resources.Add(new Resource(maquina5.Tipo, 1)); } // ProjectScheduling.Task lastTask = null; if (components.Maquina1 != null) { var artigo = context.Artigo.FirstOrDefault(x => x.ID.ToString() == components.Artigo.ToString()); var duration = 2; var task = new ProjectScheduling.Task(artigo.Nome, duration, encomenda.DataLimite, new Assignment[] { new Assignment(resources.FirstOrDefault(x => x.Name == maquina1.Tipo), 1.0) }); task.componenteDosArtigos = components; task.Encomenda = encomenda; Tasks.Add(task); lastTask = task; task.Molde = Array.IndexOf(Moldes, maquina1.Molde); //Tasks.Add(new ProjectScheduling.Task("ASd", 1, DateTime.Now.AddMonths(6), new Assignment[] { new Assignment(resources[0], 1.0) })); //break; } if (components.Maquina2 != null) { var artigo = context.Artigo.FirstOrDefault(x => x.ID.ToString() == components.Artigo.ToString()); var duration = 2; var task = new ProjectScheduling.Task(artigo.Nome, duration, encomenda.DataLimite, new Assignment[] { new Assignment(resources.FirstOrDefault(x => x.Name == maquina2.Tipo), 1.0) }); task.componenteDosArtigos = components; task.Encomenda = encomenda; Tasks.Add(task); links.Add(new TaskDependency() { Source = lastTask, Destination = task }); lastTask = task; task.Molde = Array.IndexOf(Moldes, maquina2.Molde); } if (components.Maquina3 != null) { var artigo = context.Artigo.FirstOrDefault(x => x.ID.ToString() == components.Artigo.ToString()); var duration = 2; var task = new ProjectScheduling.Task(artigo.Nome, duration, encomenda.DataLimite, new Assignment[] { new Assignment(resources.FirstOrDefault(x => x.Name == maquina3.Tipo), 1.0) }); task.componenteDosArtigos = components; task.Encomenda = encomenda; Tasks.Add(task); links.Add(new TaskDependency() { Source = lastTask, Destination = task }); lastTask = task; lastTask = task; task.Molde = Array.IndexOf(Moldes, maquina3.Molde); } if (components.Maquina4 != null) { var artigo = context.Artigo.FirstOrDefault(x => x.ID.ToString() == components.Artigo.ToString()); var duration = 2; var task = new ProjectScheduling.Task(artigo.Nome, duration, encomenda.DataLimite, new Assignment[] { new Assignment(resources.FirstOrDefault(x => x.Name == maquina4.Tipo), 1.0) }) { componenteDosArtigos = components }; Tasks.Add(task); task.Encomenda = encomenda; links.Add(new TaskDependency() { Source = lastTask, Destination = task }); lastTask = task; lastTask = task; task.Molde = Array.IndexOf(Moldes, maquina4.Molde); } } } } if (Tasks.Count == 0) { return(null); } return(new Project(Tasks, resources, links)); }