From e129e2da77af489c47f39cbb82d58fb203bf82ff Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Wed, 6 Jun 2018 14:32:29 +0200 Subject: [PATCH] fix examples --- examples/cpp/sat_runner.cc | 2 +- examples/csharp/code_samples_sat.cs | 100 ++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/examples/cpp/sat_runner.cc b/examples/cpp/sat_runner.cc index e418941b53..a7cfed9ee5 100644 --- a/examples/cpp/sat_runner.cc +++ b/examples/cpp/sat_runner.cc @@ -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" diff --git a/examples/csharp/code_samples_sat.cs b/examples/csharp/code_samples_sat.cs index 8b1038d62e..9876586e2f 100644 --- a/examples/csharp/code_samples_sat.cs +++ b/examples/csharp/code_samples_sat.cs @@ -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 ---");