OR-Tools  9.3
cp_model_search.h
Go to the documentation of this file.
1// Copyright 2010-2021 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_SEARCH_H_
15#define OR_TOOLS_SAT_CP_MODEL_SEARCH_H_
16
17#include <cstdint>
18#include <functional>
19#include <vector>
20
22#include "ortools/sat/cp_model.pb.h"
24#include "ortools/sat/integer.h"
26#include "ortools/sat/model.h"
28#include "ortools/sat/sat_parameters.pb.h"
29
30namespace operations_research {
31namespace sat {
32
33// This class allows to query information about the current bounds of the loaded
34// cp_model.proto variables during the search. It is a "view" of the current
35// solver state using the indices of the proto.
36//
37// TODO(user): For now it uses proto indices of the loaded model. We will need
38// to add a mapping to use proto indices of the non-presolved model to allow for
39// a client custom search with presolve. The main API shouldn't change though
40// and the change will be transparent.
42 public:
43 explicit CpModelView(Model* model);
44
45 // The valid indices for the calls below are in [0, num_variables).
46 int NumVariables() const;
47
48 // Getters about the current domain of the given variable.
49 bool IsFixed(int var) const;
50 int64_t Min(int var) const;
51 int64_t Max(int var) const;
52
53 // If under a given partial assignment, the value of a variable has no impact,
54 // this might returns true, and there is no point trying to branch on this
55 // variable.
56 //
57 // This might for example be the case for the start of an unperformed interval
58 // which will not impact the rest of the problem in any way. Note that it is
59 // still possible to branch on ignored variable, this will just not change
60 // anything.
61 bool IsCurrentlyFree(int var) const;
62
63 // Helpers to generate a decision.
67
68 private:
69 const CpModelMapping& mapping_;
70 const VariablesAssignment& boolean_assignment_;
71 const IntegerTrail& integer_trail_;
72 const IntegerEncoder& integer_encoder_;
73};
74
75// Constructs the search strategy specified in the given CpModelProto. A
76// positive variable ref in the proto is mapped to variable_mapping[ref] in the
77// model. All the variables referred in the search strategy must be correctly
78// mapped, the other entries can be set to kNoIntegerVariable.
80 const CpModelProto& cp_model_proto,
81 const std::vector<IntegerVariable>& variable_mapping,
82 IntegerVariable objective_var, Model* model);
83
84// For debugging fixed-search: display information about the named variables
85// domain before taking each decision. Note that we copy the instrumented
86// stategy so it doesn't have to outlive the returned functions like the other
87// arguments.
89 const CpModelProto& cp_model_proto,
90 const std::vector<IntegerVariable>& variable_mapping,
91 const std::function<BooleanOrIntegerLiteral()>& instrumented_strategy,
92 Model* model);
93
94// Returns up to base_params.num_workers() different parameters.
95// We do not always return num_worker parameters to leave room for strategies
96// like LNS that do not consume a full worker and can always be interleaved.
97std::vector<SatParameters> GetDiverseSetOfParameters(
98 const SatParameters& base_params, const CpModelProto& cp_model);
99
100} // namespace sat
101} // namespace operations_research
102
103#endif // OR_TOOLS_SAT_CP_MODEL_SEARCH_H_
BooleanOrIntegerLiteral GreaterOrEqual(int var, int64_t value) const
BooleanOrIntegerLiteral MedianValue(int var) const
BooleanOrIntegerLiteral LowerOrEqual(int var, int64_t value) const
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
int64_t value
IntVar * var
Definition: expr_array.cc:1874
GRBmodel * model
std::vector< SatParameters > GetDiverseSetOfParameters(const SatParameters &base_params, const CpModelProto &cp_model)
std::function< BooleanOrIntegerLiteral()> ConstructSearchStrategy(const CpModelProto &cp_model_proto, const std::vector< IntegerVariable > &variable_mapping, IntegerVariable objective_var, Model *model)
std::function< BooleanOrIntegerLiteral()> InstrumentSearchStrategy(const CpModelProto &cp_model_proto, const std::vector< IntegerVariable > &variable_mapping, const std::function< BooleanOrIntegerLiteral()> &instrumented_strategy, Model *model)
Collection of objects used to extend the Constraint Solver library.