protected int[,] ComputeCosts(AssignmentDescription assignment) { int[,] costs = new int[assignment.Nodes.Count, assignment.Tasks.Count]; for (int j = 0; j < assignment.Tasks.Count; j++) { costs[0, j] = 500; // UNASSIGNED cost if (assignment.Assignments[0].Contains(j)) // Not assigned yet. { for (int i = 1; i < assignment.Nodes.Count; i++) { costs[i, j] = 0; // No fee for assignment. } } else { for (int i = 1; i < assignment.Nodes.Count; i++) { if (assignment.Assignments[i].Contains(j)) { costs[i, j] = 0; // Don't move. } else { costs[i, j] = 200; // Moving cost. } } } } return(costs); }
// TODO(zhangshuai.ds): Pass in 3 policies. public AssignerCore(Solver solver, AssignmentDescription assignment) { this.solver_ = solver ?? throw new ArgumentNullException(nameof(solver)); this.assignment_ = assignment ?? throw new ArgumentNullException(nameof(assignment)); this.costs_ = this.ComputeCosts(assignment); this.variables_ = this.solver_.MakeBoolVarMatrix(assignment.Nodes.Count, assignment.Tasks.Count); }
static void Main() { using var solver = Solver.CreateSolver( "SimpleMipProgram", "CBC_MIXED_INTEGER_PROGRAMMING"); var assigner = new Assigner(solver); var assignment = new AssignmentDescription(); assignment.Nodes.Add(new NodeDescription { Id = 0, CpuCores = int.MaxValue, MemoryMiB = int.MaxValue, }); for (int i = 1; i <= 3; i++) { assignment.Nodes.Add(new NodeDescription { Id = i, CpuCores = 48, MemoryMiB = 64 << 10, // 64 GiB }); } for (int i = 0; i < 13; i++) { assignment.Tasks.Add(new TaskDescription { Id = i, CpuCores = 8, MemoryMiB = 32 << 10, // 32 GiB, CPU:MEM=1:4 }); } for (int i = 0; i < 17; i++) { assignment.Tasks.Add(new TaskDescription { Id = 13 + i, CpuCores = 16, MemoryMiB = 32 << 10, // 16 GiB, CPU:MEM=1:2 }); } assignment.Assignments[0] = new HashSet <int>(); for (int i = 0; i < assignment.Tasks.Count; i++) { assignment.Assignments[0].Add(i); } for (int i = 1; i < assignment.Nodes.Count; i++) { assignment.Assignments[i] = new HashSet <int>(); } assignment = assigner.Solve(assignment); }
public AssignmentDescription Solve(AssignmentDescription assignment) { if (assignment is null) { throw new ArgumentNullException(nameof(assignment)); } var assignerCore = new AssignerCore(this.solver_, assignment); return(assignerCore.Run()); }
public AssignmentDescription Run() { this.AddObjective(); this.AddConstraints(); Solver.ResultStatus status = this.solver_.Solve(); if (status == Solver.ResultStatus.OPTIMAL) { var result = new AssignmentDescription(); foreach (NodeDescription n in this.assignment_.Nodes) { result.Nodes.Add(n); } foreach (TaskDescription t in this.assignment_.Tasks) { result.Tasks.Add(t); } for (int i = 1; i < this.assignment_.Nodes.Count; i++) { for (int j = 0; j < this.assignment_.Tasks.Count; j++) { if (this.variables_[i, j].SolutionValue() > 0) { if (!result.Assignments.TryGetValue(i, out ISet <int> tasks)) { tasks = new HashSet <int>(); result.Assignments.Add(i, tasks); } tasks.Add(j); Console.WriteLine($"Task {j} assigned to node {i} with cost {this.costs_[i, j]}."); } } } Console.WriteLine($"Total cost is {this.solver_.Objective().Value()}"); return(result); } throw new NotImplementedException($"Not implemented for status: {status}"); }