Files
ortools-clone/ortools/linear_solver/samples/BinPackingMip.cs

112 lines
3.6 KiB
C#
Raw Normal View History

2020-05-26 09:30:42 +02:00
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
// [START import]
using System;
using Google.OrTools.LinearSolver;
// [END import]
// [START program_part1]
2020-10-26 18:36:17 +01:00
public class BinPackingMip {
2020-05-26 09:30:42 +02:00
// [START data_model]
2020-10-26 18:36:17 +01:00
class DataModel {
2020-10-28 09:43:50 +01:00
public static double[] Weights = { 48, 30, 19, 36, 36, 27, 42, 42, 36, 24, 30 };
2020-05-26 09:30:42 +02:00
public int NumItems = Weights.Length;
public int NumBins = Weights.Length;
2020-05-26 17:39:25 +02:00
public double BinCapacity = 100.0;
2020-05-26 09:30:42 +02:00
}
// [END data_model]
2020-10-26 18:36:17 +01:00
public static void Main() {
2020-05-26 09:30:42 +02:00
// [START data]
DataModel data = new DataModel();
// [END data]
// [END program_part1]
// [START solver]
// Create the linear solver with the SCIP backend.
Solver solver = Solver.CreateSolver("SCIP");
2020-05-26 09:30:42 +02:00
// [END solver]
// [START program_part2]
// [START variables]
2020-10-28 09:43:50 +01:00
Variable[,] x = new Variable[data.NumItems, data.NumBins];
2020-10-26 18:36:17 +01:00
for (int i = 0; i < data.NumItems; i++) {
for (int j = 0; j < data.NumBins; j++) {
2020-05-26 17:39:25 +02:00
x[i, j] = solver.MakeIntVar(0, 1, $"x_{i}_{j}");
2020-05-26 09:30:42 +02:00
}
}
2020-05-26 17:39:25 +02:00
Variable[] y = new Variable[data.NumBins];
2020-10-26 18:36:17 +01:00
for (int j = 0; j < data.NumBins; j++) {
y[j] = solver.MakeIntVar(0, 1, $"y_{j}");
}
2020-05-26 09:30:42 +02:00
// [END variables]
// [START constraints]
for (int i = 0; i < data.NumItems; ++i) {
2020-05-26 17:39:25 +02:00
Constraint constraint = solver.MakeConstraint(1, 1, "");
2020-05-26 09:30:42 +02:00
for (int j = 0; j < data.NumBins; ++j) {
2020-05-26 17:39:25 +02:00
constraint.SetCoefficient(x[i, j], 1);
2020-05-26 09:30:42 +02:00
}
}
2020-05-26 17:39:25 +02:00
2020-10-26 18:36:17 +01:00
for (int j = 0; j < data.NumBins; ++j) {
2020-10-28 09:43:50 +01:00
Constraint constraint = solver.MakeConstraint(0, Double.PositiveInfinity, "");
2020-05-26 17:39:25 +02:00
constraint.SetCoefficient(y[j], data.BinCapacity);
2020-10-26 18:36:17 +01:00
for (int i = 0; i < data.NumItems; ++i) {
2020-05-26 17:39:25 +02:00
constraint.SetCoefficient(x[i, j], -DataModel.Weights[i]);
2020-05-26 09:30:42 +02:00
}
}
// [END constraints]
// [START objective]
2020-05-26 17:39:25 +02:00
Objective objective = solver.Objective();
2020-10-26 18:36:17 +01:00
for (int j = 0; j < data.NumBins; ++j) {
2020-05-26 17:39:25 +02:00
objective.SetCoefficient(y[j], 1);
2020-05-26 09:30:42 +02:00
}
2020-05-26 17:39:25 +02:00
objective.SetMinimization();
2020-05-26 09:30:42 +02:00
// [END objective]
// [START solve]
Solver.ResultStatus resultStatus = solver.Solve();
// [END solve]
// [START print_solution]
// Check that the problem has an optimal solution.
2020-10-26 18:36:17 +01:00
if (resultStatus != Solver.ResultStatus.OPTIMAL) {
2020-05-26 09:30:42 +02:00
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
2020-05-26 17:39:25 +02:00
Console.WriteLine($"Number of bins used: {solver.Objective().Value()}");
double TotalWeight = 0.0;
2020-10-26 18:36:17 +01:00
for (int j = 0; j < data.NumBins; ++j) {
2020-05-26 17:39:25 +02:00
double BinWeight = 0.0;
2020-10-28 09:43:50 +01:00
if (y[j].SolutionValue() == 1) {
2020-05-26 17:39:25 +02:00
Console.WriteLine($"Bin {j}");
2020-10-26 18:36:17 +01:00
for (int i = 0; i < data.NumItems; ++i) {
2020-10-28 09:43:50 +01:00
if (x[i, j].SolutionValue() == 1) {
2020-05-26 17:39:25 +02:00
Console.WriteLine($"Item {i} weight: {DataModel.Weights[i]}");
BinWeight += DataModel.Weights[i];
2020-05-26 09:30:42 +02:00
}
}
2020-05-26 17:39:25 +02:00
Console.WriteLine($"Packed bin weight: {BinWeight}");
2020-05-26 09:30:42 +02:00
TotalWeight += BinWeight;
}
}
2020-05-26 17:39:25 +02:00
Console.WriteLine($"Total packed weight: {TotalWeight}");
// [END print_solution]
2020-05-26 09:30:42 +02:00
}
}
// [END program_part2]
// [END program]