Files
ortools-clone/examples/cpp/multi_knapsack_sat.cc

118 lines
4.0 KiB
C++
Raw Normal View History

2022-06-17 08:40:20 +02:00
// Copyright 2010-2022 Google LLC
2019-04-04 23:07:06 +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.
// Solve a scaled constrained two dimensional knapsack problem.
2019-04-05 16:43:23 +02:00
// Each bin must be filled with items with min and max weights, and min and max
// volumes. As is a knapsack, the objective is to maximize total value. It turns
// out that the objective is to maximize weights.
2019-04-04 23:07:06 +02:00
//
2019-04-05 16:43:23 +02:00
// Data is for 1 bin and 10 items. Scaling is done my having m bins and m copies
// of each items.
2019-04-04 23:07:06 +02:00
2021-04-23 14:55:51 +02:00
#include <cstdint>
2023-02-11 04:27:31 -08:00
#include <string>
2019-04-04 23:07:06 +02:00
#include <vector>
#include "absl/flags/flag.h"
2019-04-04 23:07:06 +02:00
#include "ortools/base/commandlineflags.h"
2022-02-25 09:47:52 +01:00
#include "ortools/base/init_google.h"
2019-04-05 16:43:23 +02:00
#include "ortools/base/logging.h"
2019-04-04 23:07:06 +02:00
#include "ortools/sat/cp_model.h"
2020-10-23 11:50:14 +02:00
ABSL_FLAG(int, size, 16, "scaling factor of the model");
ABSL_FLAG(std::string, params, "", "Sat parameters");
2019-04-04 23:07:06 +02:00
namespace operations_research {
namespace sat {
static const int kWeightMin = 16000;
static const int kWeightMax = 22000;
static const int kVolumeMin = 1156;
static const int kVolumeMax = 1600;
2019-04-05 16:43:23 +02:00
// Data for a single bin problem
2020-10-22 23:36:58 +02:00
static const int kItemsWeights[] = {1008, 2087, 5522, 5250, 5720,
4998, 275, 3145, 12580, 382};
static const int kItemsVolumes[] = {281, 307, 206, 111, 275,
79, 23, 65, 261, 40};
2019-04-04 23:07:06 +02:00
static const int kNumItems = 10;
2020-10-29 14:25:39 +01:00
void MultiKnapsackSat(int scaling, const std::string& params) {
2019-04-04 23:07:06 +02:00
CpModelBuilder builder;
const int num_items = scaling * kNumItems;
const int num_bins = scaling;
std::vector<std::vector<BoolVar>> items_in_bins(num_bins);
2019-04-04 23:07:06 +02:00
for (int b = 0; b < num_bins; ++b) {
for (int i = 0; i < num_items; ++i) {
items_in_bins[b].push_back(builder.NewBoolVar());
}
}
std::vector<BoolVar> selected_items(num_items);
for (int i = 0; i < num_items; ++i) {
selected_items[i] = builder.NewBoolVar();
}
// Fill up scaled values, weights, volumes;
2021-04-02 14:58:16 +02:00
std::vector<int64_t> values(num_items);
std::vector<int64_t> weights(num_items);
std::vector<int64_t> volumes(num_items);
2019-04-04 23:07:06 +02:00
for (int i = 0; i < num_items; ++i) {
const int index = i % kNumItems;
weights[i] = kItemsWeights[index];
volumes[i] = kItemsVolumes[index];
}
// Constraints per bins.
std::vector<IntVar> bin_weights;
2019-04-04 23:07:06 +02:00
for (int b = 0; b < num_bins; ++b) {
2020-10-22 23:36:58 +02:00
IntVar bin_weight = builder.NewIntVar({kWeightMin, kWeightMax});
bin_weights.push_back(bin_weight);
2022-01-04 19:35:22 +01:00
builder.AddEquality(LinearExpr::WeightedSum(items_in_bins[b], weights),
2019-04-05 16:43:23 +02:00
bin_weight);
2022-01-04 19:35:22 +01:00
builder.AddLinearConstraint(
LinearExpr::WeightedSum(items_in_bins[b], volumes),
{kVolumeMin, kVolumeMax});
2019-04-04 23:07:06 +02:00
}
2019-04-05 16:43:23 +02:00
// Each item is selected at most one time.
2019-04-04 23:07:06 +02:00
for (int i = 0; i < num_items; ++i) {
2019-04-05 16:43:23 +02:00
std::vector<BoolVar> bin_contain_item(num_bins);
2019-04-04 23:07:06 +02:00
for (int b = 0; b < num_bins; ++b) {
2019-04-05 16:43:23 +02:00
bin_contain_item[b] = items_in_bins[b][i];
2019-04-04 23:07:06 +02:00
}
builder.AddEquality(LinearExpr::Sum(bin_contain_item), selected_items[i]);
2019-04-04 23:07:06 +02:00
}
2019-04-05 16:43:23 +02:00
// Maximize the sums of weights.
builder.Maximize(LinearExpr::Sum(bin_weights));
2019-04-04 23:07:06 +02:00
// And solve.
2019-04-05 16:43:23 +02:00
const CpSolverResponse response =
2019-11-22 15:17:10 +01:00
SolveWithParameters(builder.Build(), params);
LOG(INFO) << CpSolverResponseStats(response);
2019-04-04 23:07:06 +02:00
}
2020-10-22 23:36:58 +02:00
} // namespace sat
} // namespace operations_research
2019-04-04 23:07:06 +02:00
2020-10-29 14:25:39 +01:00
int main(int argc, char** argv) {
2023-02-17 15:17:12 +01:00
absl::SetFlag(&FLAGS_stderrthreshold, 0);
2022-02-25 09:47:52 +01:00
InitGoogle(argv[0], &argc, &argv, true);
operations_research::sat::MultiKnapsackSat(absl::GetFlag(FLAGS_size),
absl::GetFlag(FLAGS_params));
2019-04-04 23:07:06 +02:00
return EXIT_SUCCESS;
}