cp_model_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_SAT_CP_MODEL_SOLVER_H_
15 #define OR_TOOLS_SAT_CP_MODEL_SOLVER_H_
16 
17 #include <functional>
18 #include <string>
19 #include <vector>
20 
21 #include "ortools/base/integral_types.h"
23 #include "ortools/sat/model.h"
25 
26 namespace operations_research {
27 namespace sat {
28 
29 // Returns a std::string with some statistics on the given CpModelProto.
30 std::string CpModelStats(const CpModelProto& model);
31 
32 // Returns a std::string with some statistics on the solver response.
33 std::string CpSolverResponseStats(const CpSolverResponse& response);
34 
35 // Solves the given CpModelProto.
36 //
37 // Note that the API takes a Model* that will be filled with the in-memory
38 // representation of the given CpModelProto. It is done this way so that it is
39 // easy to set custom parameters or interrupt the solver will calls like:
40 // - model->Add(NewSatParameters(parameters_as_string_or_proto));
41 // - model->GetOrCreate<TimeLimit>()->RegisterExternalBooleanAsLimit(&stop);
42 CpSolverResponse SolveCpModel(const CpModelProto& model_proto, Model* model);
43 
44 // Solves the given cp_model and returns an instance of CpSolverResponse.
45 CpSolverResponse Solve(const CpModelProto& model_proto);
46 
47 // Solves the given cp_model with the give sat parameters, and returns an
48 // instance of CpSolverResponse.
49 CpSolverResponse SolveWithParameters(const CpModelProto& model_proto,
50  const SatParameters& params);
51 
52 #if !defined(__PORTABLE_PLATFORM__)
53 // Solves the given cp_model with the given sat parameters as std::string in
54 // JSon format, and returns an instance of CpSolverResponse.
55 CpSolverResponse SolveWithParameters(const CpModelProto& model_proto,
56  const std::string& params);
57 #endif // !__PORTABLE_PLATFORM__
58 
59 // Allows to register a solution "observer" with the model with
60 // model.Add(NewFeasibleSolutionObserver([](response){...}));
61 // The given function will be called on each "improving" feasible solution found
62 // during the search. For a non-optimization problem, if the option to find all
63 // solution was set, then this will be called on each new solution.
64 std::function<void(Model*)> NewFeasibleSolutionObserver(
65  const std::function<void(const CpSolverResponse& response)>& observer);
66 
67 // If set, the underlying solver will call this function "regularly" in a
68 // deterministic way. It will then wait until this function returns with the
69 // current "best" information about the current problem.
70 //
71 // This is meant to be used in a multi-threaded environment with many parallel
72 // solving process. If the returned current "best" response only uses
73 // informations derived at a lower deterministic time (possibly with offset)
74 // than the deterministic time of the current thread, then the whole process can
75 // be made deterministic.
76 void SetSynchronizationFunction(std::function<CpSolverResponse()> f,
77  Model* model);
78 
79 // Allows to change the default parameters with
80 // model->Add(NewSatParameters(parameters_as_string_or_proto))
81 // before calling SolveCpModel().
82 #if !defined(__PORTABLE_PLATFORM__)
84  const std::string& params);
85 #endif // !__PORTABLE_PLATFORM__
86 std::function<SatParameters(Model*)> NewSatParameters(
87  const SatParameters& parameters);
88 
89 } // namespace sat
90 } // namespace operations_research
91 
92 #endif // OR_TOOLS_SAT_CP_MODEL_SOLVER_H_
std::string CpModelStats(const CpModelProto &model)
Returns a std::string with some statistics on the given CpModelProto.
void SetSynchronizationFunction(std::function< CpSolverResponse()> f, Model *model)
If set, the underlying solver will call this function "regularly" in a deterministic way.
CpSolverResponse SolveCpModel(const CpModelProto &model_proto, Model *model)
Solves the given CpModelProto.
std::string CpSolverResponseStats(const CpSolverResponse &response)
Returns a std::string with some statistics on the solver response.
CpSolverResponse Solve(const CpModelProto &model_proto)
Solves the given cp_model and returns an instance of CpSolverResponse.
std::function< void(Model *)> NewFeasibleSolutionObserver(const std::function< void(const CpSolverResponse &response)> &observer)
Allows to register a solution "observer" with the model with model.Add(NewFeasibleSolutionObserver([]...
Class that owns everything related to a particular optimization model.
Definition: model.h:35
CpSolverResponse SolveWithParameters(const CpModelProto &model_proto, const SatParameters &params)
Solves the given cp_model with the give sat parameters, and returns an instance of CpSolverResponse.
std::function< SatParameters(Model *)> NewSatParameters(const std::string &params)
Allows to change the default parameters with model->Add(NewSatParameters(parameters_as_string_or_prot...
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
Definition: cp_model.h:48