Add AddHint to cpp and csharp, port hinting samples
This commit is contained in:
@@ -401,6 +401,7 @@ test_cc_sat_samples: \
|
||||
rcc_reified_sample_sat \
|
||||
rcc_search_for_all_solutions_sample_sat \
|
||||
rcc_simple_sat_program \
|
||||
rcc_solution_hinting_sample_sat \
|
||||
rcc_solve_and_print_intermediate_solutions_sample_sat \
|
||||
rcc_solve_with_time_limit_sample_sat \
|
||||
rcc_step_function_sample_sat \
|
||||
|
||||
@@ -520,6 +520,7 @@ test_dotnet_sat_samples:
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/ReifiedSampleSat.cs
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/SearchForAllSolutionsSampleSat.cs
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/SimpleSatProgram.cs
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/SolutionHintingSampleSat.cs
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/SolveAndPrintIntermediateSolutionsSampleSat.cs
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/SolveWithTimeLimitSampleSat.cs
|
||||
$(MAKE) run SOURCE=ortools/sat/samples/StepFunctionSampleSat.cs
|
||||
|
||||
@@ -733,6 +733,11 @@ void CpModelBuilder::AddDecisionStrategy(
|
||||
proto->set_domain_reduction_strategy(domain_strategy);
|
||||
}
|
||||
|
||||
void CpModelBuilder::AddHint(IntVar var, int64 value) {
|
||||
cp_model_.mutable_solution_hint()->add_vars(GetOrCreateIntegerIndex(var.index_));
|
||||
cp_model_.mutable_solution_hint()->add_values(value);
|
||||
}
|
||||
|
||||
int64 SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr) {
|
||||
int64 result = expr.constant();
|
||||
for (int i = 0; i < expr.variables().size(); ++i) {
|
||||
|
||||
@@ -798,6 +798,9 @@ class CpModelBuilder {
|
||||
DecisionStrategyProto::VariableSelectionStrategy var_strategy,
|
||||
DecisionStrategyProto::DomainReductionStrategy domain_strategy);
|
||||
|
||||
/// Adds hinting to a variable.
|
||||
void AddHint(IntVar var, int64 value);
|
||||
|
||||
// TODO(user) : add MapDomain?
|
||||
|
||||
const CpModelProto& Build() const { return Proto(); }
|
||||
|
||||
@@ -635,6 +635,15 @@ namespace Google.OrTools.Sat
|
||||
model_.SearchStrategy.Add(ds);
|
||||
}
|
||||
|
||||
public void AddHint(IntVar var, long value)
|
||||
{
|
||||
if (model_.SolutionHint == null) {
|
||||
model_.SolutionHint = new PartialVariableAssignment();
|
||||
}
|
||||
model_.SolutionHint.Vars.Add(var.GetIndex());
|
||||
model_.SolutionHint.Values.Add(value);
|
||||
}
|
||||
|
||||
// Internal methods.
|
||||
|
||||
void SetObjective(LinearExpr obj, bool minimize)
|
||||
|
||||
91
ortools/sat/samples/SolutionHintingSampleSat.cs
Normal file
91
ortools/sat/samples/SolutionHintingSampleSat.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
// 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]
|
||||
using System;
|
||||
using Google.OrTools.Sat;
|
||||
|
||||
// [START print_solution]
|
||||
public class VarArraySolutionPrinter : CpSolverSolutionCallback
|
||||
{
|
||||
public VarArraySolutionPrinter(IntVar[] variables)
|
||||
{
|
||||
variables_ = variables;
|
||||
}
|
||||
|
||||
public override void OnSolutionCallback()
|
||||
{
|
||||
{
|
||||
Console.WriteLine(String.Format("Solution #{0}: time = {1:F2} s",
|
||||
solution_count_, WallTime()));
|
||||
foreach (IntVar v in variables_)
|
||||
{
|
||||
Console.WriteLine(
|
||||
String.Format(" {0} = {1}", v.ShortString(), Value(v)));
|
||||
}
|
||||
solution_count_++;
|
||||
}
|
||||
}
|
||||
|
||||
public int SolutionCount()
|
||||
{
|
||||
return solution_count_;
|
||||
}
|
||||
|
||||
private int solution_count_;
|
||||
private IntVar[] variables_;
|
||||
}
|
||||
// [END print_solution]
|
||||
|
||||
public class SolutionHintingSampleSat
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
// Creates the model.
|
||||
// [START model]
|
||||
CpModel model = new CpModel();
|
||||
// [END model]
|
||||
|
||||
// Creates the variables.
|
||||
// [START variables]
|
||||
int num_vals = 3;
|
||||
|
||||
IntVar x = model.NewIntVar(0, num_vals - 1, "x");
|
||||
IntVar y = model.NewIntVar(0, num_vals - 1, "y");
|
||||
IntVar z = model.NewIntVar(0, num_vals - 1, "z");
|
||||
// [END variables]
|
||||
|
||||
// Creates the constraints.
|
||||
// [START constraints]
|
||||
model.Add(x != y);
|
||||
// [END constraints]
|
||||
|
||||
// Solution hinting: x <- 1, y <- 2
|
||||
model.AddHint(x, 1);
|
||||
model.AddHint(y, 2);
|
||||
|
||||
// [START objective]
|
||||
model.Maximize(LinearExpr.ScalProd(new IntVar[] {x, y, z}, new int[] {1, 2, 3}));
|
||||
// [END objective]
|
||||
|
||||
// Creates a solver and solves the model.
|
||||
// [START solve]
|
||||
CpSolver solver = new CpSolver();
|
||||
VarArraySolutionPrinter cb =
|
||||
new VarArraySolutionPrinter(new IntVar[] { x, y, z });
|
||||
CpSolverStatus status = solver.SolveWithSolutionCallback(model, cb);
|
||||
// [END solve]
|
||||
|
||||
}
|
||||
}
|
||||
// [END program]
|
||||
22
ortools/sat/samples/SolutionHintingSampleSat.csproj
Normal file
22
ortools/sat/samples/SolutionHintingSampleSat.csproj
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<EnableDefaultItems>false</EnableDefaultItems>
|
||||
<RestoreSources>../../../packages;$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
|
||||
<AssemblyName>Google.OrTools.SolutionHintingSampleSat</AssemblyName>
|
||||
<IsPackable>true</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<GenerateTailCalls>true</GenerateTailCalls>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="SolutionHintingSampleSat.cs" />
|
||||
<PackageReference Include="Google.OrTools" Version="7.4.*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
73
ortools/sat/samples/solution_hinting_sample_sat.cc
Normal file
73
ortools/sat/samples/solution_hinting_sample_sat.cc
Normal file
@@ -0,0 +1,73 @@
|
||||
// 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]
|
||||
#include "ortools/sat/cp_model.h"
|
||||
#include "ortools/sat/model.h"
|
||||
|
||||
namespace operations_research {
|
||||
namespace sat {
|
||||
|
||||
void SolutionHintingSampleSat() {
|
||||
// [START model]
|
||||
CpModelBuilder cp_model;
|
||||
// [END model]
|
||||
|
||||
// [START variables]
|
||||
const Domain domain(0, 2);
|
||||
const IntVar x = cp_model.NewIntVar(domain).WithName("x");
|
||||
const IntVar y = cp_model.NewIntVar(domain).WithName("y");
|
||||
const IntVar z = cp_model.NewIntVar(domain).WithName("z");
|
||||
// [END variables]
|
||||
|
||||
// [START constraints]
|
||||
cp_model.AddNotEqual(x, y);
|
||||
// [END constraints]
|
||||
|
||||
// [START objective]
|
||||
cp_model.Maximize(LinearExpr::ScalProd({x, y, z}, {1, 2, 3}));
|
||||
// [END objective]
|
||||
|
||||
// Solution hinting: x <- 1, y <- 2
|
||||
cp_model.AddHint(x, 1);
|
||||
cp_model.AddHint(y, 2);
|
||||
|
||||
// [START print_solution]
|
||||
Model model;
|
||||
|
||||
int num_solutions = 0;
|
||||
model.Add(NewFeasibleSolutionObserver([&](const CpSolverResponse& r) {
|
||||
LOG(INFO) << "Solution " << num_solutions;
|
||||
LOG(INFO) << " x = " << SolutionIntegerValue(r, x);
|
||||
LOG(INFO) << " y = " << SolutionIntegerValue(r, y);
|
||||
LOG(INFO) << " z = " << SolutionIntegerValue(r, z);
|
||||
num_solutions++;
|
||||
}));
|
||||
// [END print_solution]
|
||||
|
||||
// Solving part.
|
||||
// [START solve]
|
||||
const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);
|
||||
LOG(INFO) << CpSolverResponseStats(response);
|
||||
// [END solve]
|
||||
}
|
||||
|
||||
} // namespace sat
|
||||
} // namespace operations_research
|
||||
|
||||
int main() {
|
||||
operations_research::sat::SolutionHintingSampleSat();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
// [END program]
|
||||
Reference in New Issue
Block a user