public string WriteSolution(Solution solution) { var costSolver = new CostSolver(); StringBuilder sb = new StringBuilder(); sb.AppendLine("Solution:"); foreach (var slot in solution.Problem.Slots) { var mappings = solution.Mappings.Where(m => m.Slot == slot); sb.Append(String.Format("'{0}' -> ", slot.Identifier)); if (mappings.Count() == 1) { sb.AppendLine(String.Format("'{0}'", mappings.First().Resource.Identifier)); } else { sb.AppendLine("Invalid"); } } foreach (var resource in solution.Problem.Resources) { var mappings = solution.Mappings.Where(m => m.Resource == resource); var cost = costSolver.CalculateResourceCost(solution, resource); sb.AppendLine(String.Format("Resource '{0}' -> {1} mappings, {2} cost", resource.Identifier, mappings.Count(), cost)); } return(sb.ToString()); }
public Solution Solve() { var costSolver = new CostSolver(); var solutionWriter = new SolutionWriter(); var csvWriter = new CsvWriter(); var csvFile = new FileInfo("solution.csv"); double minCost = 0; Solution bestSolution = null; while (true) { var solution = this.CreateRandomSolution(); var solutionCost = costSolver.CalculateCost(solution); if (bestSolution == null || solutionCost < minCost) { minCost = solutionCost; bestSolution = solution; Console.WriteLine("new min cost: " + minCost); Console.WriteLine(solutionWriter.WriteSolution(bestSolution)); File.WriteAllText(csvFile.FullName, csvWriter.WriteSolution(bestSolution)); } } return(null); }
public string WriteSolution(Solution solution) { var costSolver = new CostSolver(); StringBuilder sb = new StringBuilder(); sb.AppendLine("KW,Datum,Montag,Dienstag,Mittwoch,Donnerstag,Freitag"); foreach (var group in solution.Problem.Groups) { Mapping[] groupMappings = new Mapping[5]; foreach (var slot in group.Slots) { var mapping = solution.Mappings.Single(m => m.Slot == slot); var slotDate = DateTime.Parse(slot.Identifier); int slotIndex = (int)slotDate.DayOfWeek - 1; if (slotIndex < 0 || slotIndex > 4) { throw new Exception("xxx"); } groupMappings[slotIndex] = mapping; } var values = new List <string>(); values.Add(group.Identifier); var minDate = groupMappings.Where(m => m != null).Min(m => DateTime.Parse(m.Slot.Identifier)); var maxDate = groupMappings.Where(m => m != null).Max(m => DateTime.Parse(m.Slot.Identifier)); values.Add($"{minDate.ToString("dd.MM.yyyy")}-{maxDate.ToString("dd.MM.yyyy")}"); for (int i = 0; i < 5; i++) { if (groupMappings[i] != null) { values.Add(groupMappings[i].Resource.Identifier); } else { values.Add("-"); } } sb.AppendLine(String.Join(",", values)); } //sb.AppendLine("Solution:"); //foreach (var slot in solution.Problem.Slots) //{ // var mappings = solution.Mappings.Where(m => m.Slot == slot); // sb.Append(String.Format("'{0}' -> ", slot.Identifier)); // if (mappings.Count() == 1) // { // sb.AppendLine(String.Format("'{0}'", mappings.First().Resource.Identifier)); // } // else // { // sb.AppendLine("Invalid"); // } //} //foreach (var resource in solution.Problem.Resources) //{ // var mappings = solution.Mappings.Where(m => m.Resource == resource); // var cost = costSolver.CalculateResourceCost(solution, resource); // sb.AppendLine(String.Format("Resource '{0}' -> {1} mappings, {2} cost", resource.Identifier, mappings.Count(), cost)); //} return(sb.ToString()); }