fix examples
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
#include "ortools/sat/boolean_problem.pb.h"
|
||||
#include "ortools/sat/cp_model.pb.h"
|
||||
#include "ortools/sat/cp_model_solver.h"
|
||||
#include "ortools/sat/drat.h"
|
||||
#include "ortools/sat/drat_proof_handler.h"
|
||||
#include "ortools/sat/lp_utils.h"
|
||||
#include "ortools/sat/model.h"
|
||||
#include "examples/cpp/opb_reader.h"
|
||||
|
||||
@@ -145,6 +145,104 @@ public class CodeSamplesSat
|
||||
}
|
||||
}
|
||||
|
||||
static void BinpackingProblem()
|
||||
{
|
||||
// Data.
|
||||
int bin_capacity = 100;
|
||||
int slack_capacity = 20;
|
||||
int num_bins = 10;
|
||||
|
||||
int[,] items = new int[,] { {20, 12}, {15, 12}, {30, 8}, {45, 5} };
|
||||
int num_items = items.GetLength(0);
|
||||
|
||||
// Model.
|
||||
CpModel model = new CpModel();
|
||||
|
||||
// Main variables.
|
||||
IntVar[,] x = new IntVar[num_items, num_bins];
|
||||
for (int i = 0; i < num_items; ++i)
|
||||
{
|
||||
int num_copies = items[i, 1];
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
x[i, b] = model.NewIntVar(0, num_copies, String.Format("x_{0}_{1}", i, b));
|
||||
}
|
||||
}
|
||||
|
||||
// Load variables.
|
||||
IntVar[] loads = new IntVar[num_bins];
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
loads[b] = model.NewIntVar(0, bin_capacity, String.Format("load_{0}", b));
|
||||
}
|
||||
|
||||
// Slack variables.
|
||||
IntVar[] slacks = new IntVar[num_bins];
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
slacks[b] = model.NewBoolVar(String.Format("slack_{0}", b));
|
||||
}
|
||||
|
||||
// Links load and x.
|
||||
int[] sizes = new int[num_items];
|
||||
for (int i = 0; i < num_items; ++i) {
|
||||
sizes[i] = items[i, 0];
|
||||
}
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
IntVar[] tmp = new IntVar[num_items];
|
||||
for (int i = 0; i < num_items; ++i)
|
||||
{
|
||||
tmp[i] = x[i, b];
|
||||
}
|
||||
model.Add(loads[b] == tmp.ScalProd(sizes));
|
||||
}
|
||||
|
||||
// Place all items.
|
||||
for (int i = 0; i < num_items; ++i)
|
||||
{
|
||||
IntVar[] tmp = new IntVar[num_bins];
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
tmp[b] = x[i, b];
|
||||
}
|
||||
model.Add(tmp.Sum() == items[i, 1]);
|
||||
}
|
||||
|
||||
// Links load and slack.
|
||||
int safe_capacity = bin_capacity - slack_capacity;
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
// slack[b] => loads[b] <= safe_capacity.
|
||||
model.Add(loads[b] <= safe_capacity).OnlyEnforceIf(slacks[b]);
|
||||
// not(slack[b]) => loads[b] > safe_capacity.
|
||||
model.Add(loads[b] > safe_capacity).OnlyEnforceIf(slacks[b].Not());
|
||||
}
|
||||
|
||||
// Maximize sum of slacks.
|
||||
model.Maximize(slacks.Sum());
|
||||
|
||||
// Solves and prints out the solution.
|
||||
CpSolver solver = new CpSolver();
|
||||
CpSolverStatus status = solver.Solve(model);
|
||||
Console.WriteLine(String.Format("Solve status: {0}", status));
|
||||
if (status == CpSolverStatus.Optimal) {
|
||||
Console.WriteLine(String.Format("Optimal objective value: {0}", solver.ObjectiveValue));
|
||||
for (int b = 0; b < num_bins; ++b)
|
||||
{
|
||||
Console.WriteLine(String.Format("load_{0} = {1}", b, solver.Value(loads[b])));
|
||||
for (int i = 0; i < num_items; ++i)
|
||||
{
|
||||
Console.WriteLine(string.Format(" item_{0}_{1} = {2}", i, b, solver.Value(x[i, b])));
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.WriteLine("Statistics");
|
||||
Console.WriteLine(String.Format(" - conflicts : {0}", solver.NumConflicts()));
|
||||
Console.WriteLine(String.Format(" - branches : {0}", solver.NumBranches()));
|
||||
Console.WriteLine(String.Format(" - wall time : {0} s", solver.WallTime()));
|
||||
}
|
||||
|
||||
static void IntervalSample()
|
||||
{
|
||||
CpModel model = new CpModel();
|
||||
@@ -269,6 +367,8 @@ public class CodeSamplesSat
|
||||
ReifiedSample();
|
||||
Console.WriteLine("--- RabbitsAndPheasants ---");
|
||||
RabbitsAndPheasants();
|
||||
Console.WriteLine("--- BinpackingProblem ---");
|
||||
BinpackingProblem();
|
||||
Console.WriteLine("--- IntervalSample ---");
|
||||
IntervalSample();
|
||||
Console.WriteLine("--- MinimalCpSat ---");
|
||||
|
||||
Reference in New Issue
Block a user