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

134 lines
4.3 KiB
C#
Raw Normal View History

2024-01-04 13:43:15 +01:00
// Copyright 2010-2024 Google LLC
2020-05-26 09:30:42 +02:00
// 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]
2021-12-01 23:50:49 +01:00
// Solve a multiple knapsack problem using a MIP solver.
2020-05-26 09:30:42 +02:00
// [START import]
using System;
2021-12-01 23:50:49 +01:00
using System.Collections.Generic;
using System.Linq;
2020-05-26 09:30:42 +02:00
using Google.OrTools.LinearSolver;
// [END import]
2020-11-03 10:04:19 +01:00
public class MultipleKnapsackMip
{
public static void Main()
{
2021-12-01 23:50:49 +01:00
// Instantiate the data problem.
2020-11-03 10:04:19 +01:00
// [START data]
2021-12-01 23:50:49 +01:00
double[] Weights = { 48, 30, 42, 36, 36, 48, 42, 42, 36, 24, 30, 30, 42, 36, 36 };
double[] Values = { 10, 30, 25, 50, 35, 30, 15, 40, 30, 35, 45, 10, 20, 30, 25 };
int NumItems = Weights.Length;
int[] allItems = Enumerable.Range(0, NumItems).ToArray();
double[] BinCapacities = { 100, 100, 100, 100, 100 };
int NumBins = BinCapacities.Length;
int[] allBins = Enumerable.Range(0, NumBins).ToArray();
2020-11-03 10:04:19 +01:00
// [END data]
2020-05-26 09:30:42 +02:00
2020-11-03 10:04:19 +01:00
// [START solver]
// Create the linear solver with the SCIP backend.
Solver solver = Solver.CreateSolver("SCIP");
if (solver is null)
{
return;
}
2020-11-03 10:04:19 +01:00
// [END solver]
2020-05-26 09:30:42 +02:00
2021-12-01 23:50:49 +01:00
// Variables.
2020-11-03 10:04:19 +01:00
// [START variables]
2021-12-01 23:50:49 +01:00
Variable[,] x = new Variable[NumItems, NumBins];
foreach (int i in allItems)
2020-11-03 10:04:19 +01:00
{
2021-12-01 23:50:49 +01:00
foreach (int b in allBins)
2020-11-03 10:04:19 +01:00
{
x[i, b] = solver.MakeBoolVar($"x_{i}_{b}");
2020-11-03 10:04:19 +01:00
}
}
// [END variables]
2020-05-26 09:30:42 +02:00
2021-12-01 23:50:49 +01:00
// Constraints.
2020-11-03 10:04:19 +01:00
// [START constraints]
// Each item is assigned to at most one bin.
2021-12-01 23:50:49 +01:00
foreach (int i in allItems)
2020-11-03 10:04:19 +01:00
{
Constraint constraint = solver.MakeConstraint(0, 1, "");
2021-12-01 23:50:49 +01:00
foreach (int b in allBins)
2020-11-03 10:04:19 +01:00
{
constraint.SetCoefficient(x[i, b], 1);
2020-11-03 10:04:19 +01:00
}
}
2020-05-26 09:30:42 +02:00
// The amount packed in each bin cannot exceed its capacity.
2021-12-01 23:50:49 +01:00
foreach (int b in allBins)
2020-11-03 10:04:19 +01:00
{
2021-12-01 23:50:49 +01:00
Constraint constraint = solver.MakeConstraint(0, BinCapacities[b], "");
foreach (int i in allItems)
2020-11-03 10:04:19 +01:00
{
2021-12-01 23:50:49 +01:00
constraint.SetCoefficient(x[i, b], Weights[i]);
2020-11-03 10:04:19 +01:00
}
}
// [END constraints]
2020-05-26 09:30:42 +02:00
2021-12-01 23:50:49 +01:00
// Objective.
2020-11-03 10:04:19 +01:00
// [START objective]
Objective objective = solver.Objective();
2021-12-01 23:50:49 +01:00
foreach (int i in allItems)
2020-11-03 10:04:19 +01:00
{
2021-12-01 23:50:49 +01:00
foreach (int b in allBins)
2020-11-03 10:04:19 +01:00
{
2021-12-01 23:50:49 +01:00
objective.SetCoefficient(x[i, b], Values[i]);
2020-11-03 10:04:19 +01:00
}
}
objective.SetMaximization();
// [END objective]
2020-05-26 09:30:42 +02:00
2020-11-03 10:04:19 +01:00
// [START solve]
Solver.ResultStatus resultStatus = solver.Solve();
// [END solve]
2020-05-26 09:30:42 +02:00
2020-11-03 10:04:19 +01:00
// [START print_solution]
// Check that the problem has an optimal solution.
if (resultStatus == Solver.ResultStatus.OPTIMAL)
2020-11-03 10:04:19 +01:00
{
Console.WriteLine($"Total packed value: {solver.Objective().Value()}");
double TotalWeight = 0.0;
2021-12-01 23:50:49 +01:00
foreach (int b in allBins)
2020-11-03 10:04:19 +01:00
{
double BinWeight = 0.0;
double BinValue = 0.0;
Console.WriteLine("Bin " + b);
2021-12-01 23:50:49 +01:00
foreach (int i in allItems)
2020-11-03 10:04:19 +01:00
{
if (x[i, b].SolutionValue() == 1)
{
2021-12-01 23:50:49 +01:00
Console.WriteLine($"Item {i} weight: {Weights[i]} values: {Values[i]}");
BinWeight += Weights[i];
BinValue += Values[i];
}
2020-11-03 10:04:19 +01:00
}
Console.WriteLine("Packed bin weight: " + BinWeight);
Console.WriteLine("Packed bin value: " + BinValue);
TotalWeight += BinWeight;
2020-11-03 10:04:19 +01:00
}
Console.WriteLine("Total packed weight: " + TotalWeight);
}
else
{
Console.WriteLine("The problem does not have an optimal solution!");
2020-05-26 09:30:42 +02:00
}
2020-11-03 10:04:19 +01:00
// [END print_solution]
2020-05-26 09:30:42 +02:00
}
}
// [END program]