static void Main() { // [START solver] LinearSumAssignment assignment = new LinearSumAssignment(); // [END solver] // [START data] int[,] costs = { { 90, 76, 75, 70 }, { 35, 85, 55, 65 }, { 125, 95, 90, 105 }, { 45, 110, 95, 115 }, }; int numWorkers = 4; int[] allWorkers = Enumerable.Range(0, numWorkers).ToArray(); int numTasks = 4; int[] allTasks = Enumerable.Range(0, numTasks).ToArray(); // [END data] // [START constraints] // Add each arc. foreach (int w in allWorkers) { foreach (int t in allTasks) { if (costs[w, t] != 0) { assignment.AddArcWithCost(w, t, costs[w, t]); } } } // [END constraints] // [START solve] LinearSumAssignment.Status status = assignment.Solve(); // [END solve] // [START print_solution] if (status == LinearSumAssignment.Status.OPTIMAL) { Console.WriteLine($"Total cost: {assignment.OptimalCost()}."); foreach (int worker in allWorkers) { Console.WriteLine($"Worker {worker} assigned to task {assignment.RightMate(worker)}. " + $"Cost: {assignment.AssignmentCost(worker)}."); } } else { Console.WriteLine("Solving the linear assignment problem failed."); Console.WriteLine($"Solver status: {status}."); } // [END print_solution] }
private static void Run(int[][] costs) { var botsCount = costs.Length; var tasksCount = costs[0].Length; Console.WriteLine($"*** Start with Bots = {botsCount} , tasks = {tasksCount} ***"); // Solver usage var assignment = new LinearSumAssignment(); for (var bot = 0; bot < botsCount; bot++) { for (var task = 0; task < tasksCount; task++) { assignment.AddArcWithCost(bot, task, costs[bot][task]); } } var solveStatus = assignment.Solve(); // Check the result if (solveStatus == LinearSumAssignment.Status.OPTIMAL) { Console.WriteLine($" Optimal Cost = {assignment.OptimalCost()}"); for (int i = 0; i < assignment.NumNodes(); i++) { Console.WriteLine($"Task {assignment.RightMate(i)} assigned to Bot {i}, distance {assignment.AssignmentCost(i)}"); } } else if (solveStatus == LinearSumAssignment.Status.INFEASIBLE) { Console.WriteLine("No assignment is possible."); } else if (solveStatus == LinearSumAssignment.Status.POSSIBLE_OVERFLOW) { Console.WriteLine("Some input costs are too large and may cause an integer overflow."); } }