or-tools 0.4.3 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/ext/or-tools/ext.cpp CHANGED
@@ -1,6 +1,6 @@
1
1
  #include <ortools/base/version.h>
2
2
 
3
- #include <rice/Module.hpp>
3
+ #include "ext.h"
4
4
 
5
5
  void init_assignment(Rice::Module& m);
6
6
  void init_bin_packing(Rice::Module& m);
@@ -14,9 +14,9 @@ void Init_ext()
14
14
  {
15
15
  auto m = Rice::define_module("ORTools");
16
16
 
17
- m.define_singleton_method(
17
+ m.define_singleton_function(
18
18
  "lib_version",
19
- *[]() {
19
+ []() {
20
20
  return std::to_string(operations_research::OrToolsMajorVersion()) + "."
21
21
  + std::to_string(operations_research::OrToolsMinorVersion());
22
22
  });
@@ -0,0 +1,4 @@
1
+ #pragma once
2
+
3
+ #include <rice/rice.hpp>
4
+ #include <rice/stl.hpp>
@@ -1,8 +1,6 @@
1
1
  require "mkmf-rice"
2
2
 
3
- raise "Missing stdc++" unless have_library("stdc++")
4
-
5
- $CXXFLAGS << " -std=c++17 -DUSE_CBC"
3
+ $CXXFLAGS << " -std=c++17 $(optflags) -DUSE_CBC"
6
4
 
7
5
  # or-tools warnings
8
6
  $CXXFLAGS << " -Wno-sign-compare -Wno-shorten-64-to-32 -Wno-ignored-qualifiers"
@@ -1,11 +1,6 @@
1
1
  #include <ortools/linear_solver/linear_solver.h>
2
2
 
3
- #include <rice/Array.hpp>
4
- #include <rice/Class.hpp>
5
- #include <rice/Constructor.hpp>
6
- #include <rice/Module.hpp>
7
- #include <rice/String.hpp>
8
- #include <rice/Symbol.hpp>
3
+ #include "ext.h"
9
4
 
10
5
  using operations_research::LinearExpr;
11
6
  using operations_research::LinearRange;
@@ -21,141 +16,135 @@ using Rice::Object;
21
16
  using Rice::String;
22
17
  using Rice::Symbol;
23
18
 
24
- template<>
25
- inline
26
- MPSolver::OptimizationProblemType from_ruby<MPSolver::OptimizationProblemType>(Object x)
19
+ namespace Rice::detail
27
20
  {
28
- std::string s = Symbol(x).str();
29
- if (s == "glop") {
30
- return MPSolver::OptimizationProblemType::GLOP_LINEAR_PROGRAMMING;
31
- } else if (s == "cbc") {
32
- return MPSolver::OptimizationProblemType::CBC_MIXED_INTEGER_PROGRAMMING;
33
- } else {
34
- throw std::runtime_error("Unknown optimization problem type: " + s);
35
- }
36
- }
37
-
38
- Class rb_cMPVariable;
39
- Class rb_cMPConstraint;
40
- Class rb_cMPObjective;
41
-
42
- template<>
43
- inline
44
- Object to_ruby<MPVariable*>(MPVariable* const &x)
45
- {
46
- return Rice::Data_Object<MPVariable>(x, rb_cMPVariable, nullptr, nullptr);
47
- }
21
+ template<>
22
+ struct Type<MPSolver::OptimizationProblemType>
23
+ {
24
+ static bool verify()
25
+ {
26
+ return true;
27
+ }
28
+ };
48
29
 
49
- template<>
50
- inline
51
- Object to_ruby<MPConstraint*>(MPConstraint* const &x)
52
- {
53
- return Rice::Data_Object<MPConstraint>(x, rb_cMPConstraint, nullptr, nullptr);
54
- }
55
-
56
- template<>
57
- inline
58
- Object to_ruby<MPObjective*>(MPObjective* const &x)
59
- {
60
- return Rice::Data_Object<MPObjective>(x, rb_cMPObjective, nullptr, nullptr);
30
+ template<>
31
+ struct From_Ruby<MPSolver::OptimizationProblemType>
32
+ {
33
+ static MPSolver::OptimizationProblemType convert(VALUE x)
34
+ {
35
+ auto s = Symbol(x).str();
36
+ if (s == "glop") {
37
+ return MPSolver::OptimizationProblemType::GLOP_LINEAR_PROGRAMMING;
38
+ } else if (s == "cbc") {
39
+ return MPSolver::OptimizationProblemType::CBC_MIXED_INTEGER_PROGRAMMING;
40
+ } else {
41
+ throw std::runtime_error("Unknown optimization problem type: " + s);
42
+ }
43
+ }
44
+ };
61
45
  }
62
46
 
63
47
  void init_linear(Rice::Module& m) {
64
- rb_cMPVariable = Rice::define_class_under<MPVariable>(m, "MPVariable")
48
+ Rice::define_class_under<LinearRange>(m, "LinearRange");
49
+ auto rb_cLinearExpr = Rice::define_class_under<LinearExpr>(m, "LinearExpr");
50
+
51
+ Rice::define_class_under<MPVariable>(m, "MPVariable")
65
52
  .define_method("name", &MPVariable::name)
66
53
  .define_method("solution_value", &MPVariable::solution_value)
67
54
  .define_method(
68
55
  "+",
69
- *[](MPVariable& self, LinearExpr& other) {
56
+ [](MPVariable& self, LinearExpr& other) {
70
57
  LinearExpr s(&self);
71
58
  return s + other;
72
59
  })
73
60
  .define_method(
74
61
  "-",
75
- *[](MPVariable& self, LinearExpr& other) {
62
+ [](MPVariable& self, LinearExpr& other) {
76
63
  LinearExpr s(&self);
77
64
  return s - other;
78
65
  })
79
66
  .define_method(
80
67
  "*",
81
- *[](MPVariable& self, double other) {
68
+ [](MPVariable& self, double other) {
82
69
  LinearExpr s(&self);
83
70
  return s * other;
84
71
  })
85
72
  .define_method(
86
73
  "inspect",
87
- *[](MPVariable& self) {
74
+ [](MPVariable& self) {
88
75
  return "#<ORTools::MPVariable @name=\"" + self.name() + "\">";
89
76
  });
90
77
 
91
- Rice::define_class_under<LinearExpr>(m, "LinearExpr")
78
+ rb_cLinearExpr
92
79
  .define_constructor(Rice::Constructor<LinearExpr>())
93
80
  .define_method(
94
81
  "_add_linear_expr",
95
- *[](LinearExpr& self, LinearExpr& other) {
82
+ [](LinearExpr& self, LinearExpr& other) {
96
83
  return self + other;
97
84
  })
98
85
  .define_method(
99
86
  "_add_mp_variable",
100
- *[](LinearExpr& self, MPVariable &other) {
87
+ [](LinearExpr& self, MPVariable &other) {
101
88
  LinearExpr o(&other);
102
89
  return self + o;
103
90
  })
104
91
  .define_method(
105
92
  "_gte_double",
106
- *[](LinearExpr& self, double other) {
93
+ [](LinearExpr& self, double other) {
107
94
  LinearExpr o(other);
108
95
  return self >= o;
109
96
  })
110
97
  .define_method(
111
98
  "_gte_linear_expr",
112
- *[](LinearExpr& self, LinearExpr& other) {
99
+ [](LinearExpr& self, LinearExpr& other) {
113
100
  return self >= other;
114
101
  })
115
102
  .define_method(
116
103
  "_lte_double",
117
- *[](LinearExpr& self, double other) {
104
+ [](LinearExpr& self, double other) {
118
105
  LinearExpr o(other);
119
106
  return self <= o;
120
107
  })
121
108
  .define_method(
122
109
  "_lte_linear_expr",
123
- *[](LinearExpr& self, LinearExpr& other) {
110
+ [](LinearExpr& self, LinearExpr& other) {
124
111
  return self <= other;
125
112
  })
126
113
  .define_method(
127
114
  "==",
128
- *[](LinearExpr& self, double other) {
115
+ [](LinearExpr& self, double other) {
129
116
  LinearExpr o(other);
130
117
  return self == o;
131
118
  })
132
119
  .define_method(
133
120
  "to_s",
134
- *[](LinearExpr& self) {
121
+ [](LinearExpr& self) {
135
122
  return self.ToString();
136
123
  })
137
124
  .define_method(
138
125
  "inspect",
139
- *[](LinearExpr& self) {
126
+ [](LinearExpr& self) {
140
127
  return "#<ORTools::LinearExpr \"" + self.ToString() + "\">";
141
128
  });
142
129
 
143
- Rice::define_class_under<LinearRange>(m, "LinearRange");
144
-
145
- rb_cMPConstraint = Rice::define_class_under<MPConstraint>(m, "MPConstraint")
130
+ Rice::define_class_under<MPConstraint>(m, "MPConstraint")
146
131
  .define_method("set_coefficient", &MPConstraint::SetCoefficient);
147
132
 
148
- rb_cMPObjective = Rice::define_class_under<MPObjective>(m, "MPObjective")
133
+ Rice::define_class_under<MPObjective>(m, "MPObjective")
149
134
  .define_method("value", &MPObjective::Value)
150
135
  .define_method("set_coefficient", &MPObjective::SetCoefficient)
151
136
  .define_method("set_maximization", &MPObjective::SetMaximization);
152
137
 
153
138
  Rice::define_class_under<MPSolver>(m, "Solver")
154
139
  .define_constructor(Rice::Constructor<MPSolver, std::string, MPSolver::OptimizationProblemType>())
155
- .define_method("infinity", &MPSolver::infinity)
140
+ .define_method(
141
+ "infinity",
142
+ [](MPSolver& self) {
143
+ return self.infinity();
144
+ })
156
145
  .define_method(
157
146
  "int_var",
158
- *[](MPSolver& self, double min, double max, const std::string& name) {
147
+ [](MPSolver& self, double min, double max, const std::string& name) {
159
148
  return self.MakeIntVar(min, max, name);
160
149
  })
161
150
  .define_method("num_var", &MPSolver::MakeNumVar)
@@ -168,27 +157,27 @@ void init_linear(Rice::Module& m) {
168
157
  .define_method("objective", &MPSolver::MutableObjective)
169
158
  .define_method(
170
159
  "maximize",
171
- *[](MPSolver& self, LinearExpr& expr) {
160
+ [](MPSolver& self, LinearExpr& expr) {
172
161
  return self.MutableObjective()->MaximizeLinearExpr(expr);
173
162
  })
174
163
  .define_method(
175
164
  "minimize",
176
- *[](MPSolver& self, LinearExpr& expr) {
165
+ [](MPSolver& self, LinearExpr& expr) {
177
166
  return self.MutableObjective()->MinimizeLinearExpr(expr);
178
167
  })
179
168
  .define_method(
180
169
  "add",
181
- *[](MPSolver& self, const LinearRange& range) {
170
+ [](MPSolver& self, const LinearRange& range) {
182
171
  return self.MakeRowConstraint(range);
183
172
  })
184
173
  .define_method(
185
174
  "constraint",
186
- *[](MPSolver& self, double lb, double ub) {
175
+ [](MPSolver& self, double lb, double ub) {
187
176
  return self.MakeRowConstraint(lb, ub);
188
177
  })
189
178
  .define_method(
190
179
  "solve",
191
- *[](MPSolver& self) {
180
+ [](MPSolver& self) {
192
181
  auto status = self.Solve();
193
182
 
194
183
  if (status == MPSolver::ResultStatus::OPTIMAL) {
@@ -208,5 +197,23 @@ void init_linear(Rice::Module& m) {
208
197
  } else {
209
198
  throw std::runtime_error("Unknown status");
210
199
  }
200
+ })
201
+ .define_method(
202
+ "export_model_as_lp_format",
203
+ [](MPSolver& self, bool obfuscate) {
204
+ std::string model_str;
205
+ if (!self.ExportModelAsLpFormat(obfuscate, &model_str)) {
206
+ throw std::runtime_error("Export failed");
207
+ }
208
+ return model_str;
209
+ })
210
+ .define_method(
211
+ "export_model_as_mps_format",
212
+ [](MPSolver& self, bool fixed_format, bool obfuscate) {
213
+ std::string model_str;
214
+ if (!self.ExportModelAsMpsFormat(fixed_format, obfuscate, &model_str)) {
215
+ throw std::runtime_error("Export failed");
216
+ }
217
+ return model_str;
211
218
  });
212
219
  }
@@ -1,9 +1,7 @@
1
1
  #include <ortools/graph/max_flow.h>
2
2
  #include <ortools/graph/min_cost_flow.h>
3
3
 
4
- #include <rice/Array.hpp>
5
- #include <rice/Constructor.hpp>
6
- #include <rice/Module.hpp>
4
+ #include "ext.h"
7
5
 
8
6
  using operations_research::NodeIndex;
9
7
  using operations_research::SimpleMaxFlow;
@@ -25,7 +23,7 @@ void init_network_flows(Rice::Module& m) {
25
23
  .define_method("flow", &SimpleMaxFlow::Flow)
26
24
  .define_method(
27
25
  "solve",
28
- *[](SimpleMaxFlow& self, NodeIndex source, NodeIndex sink) {
26
+ [](SimpleMaxFlow& self, NodeIndex source, NodeIndex sink) {
29
27
  auto status = self.Solve(source, sink);
30
28
 
31
29
  if (status == SimpleMaxFlow::Status::OPTIMAL) {
@@ -42,24 +40,24 @@ void init_network_flows(Rice::Module& m) {
42
40
  })
43
41
  .define_method(
44
42
  "source_side_min_cut",
45
- *[](SimpleMaxFlow& self) {
43
+ [](SimpleMaxFlow& self) {
46
44
  std::vector<NodeIndex> result;
47
45
  self.GetSourceSideMinCut(&result);
48
46
 
49
47
  Array ret;
50
- for(auto const& it: result) {
48
+ for (const auto& it : result) {
51
49
  ret.push(it);
52
50
  }
53
51
  return ret;
54
52
  })
55
53
  .define_method(
56
54
  "sink_side_min_cut",
57
- *[](SimpleMaxFlow& self) {
55
+ [](SimpleMaxFlow& self) {
58
56
  std::vector<NodeIndex> result;
59
57
  self.GetSinkSideMinCut(&result);
60
58
 
61
59
  Array ret;
62
- for(auto const& it: result) {
60
+ for (const auto& it : result) {
63
61
  ret.push(it);
64
62
  }
65
63
  return ret;
@@ -81,7 +79,7 @@ void init_network_flows(Rice::Module& m) {
81
79
  .define_method("unit_cost", &SimpleMinCostFlow::UnitCost)
82
80
  .define_method(
83
81
  "solve",
84
- *[](SimpleMinCostFlow& self) {
82
+ [](SimpleMinCostFlow& self) {
85
83
  auto status = self.Solve();
86
84
 
87
85
  if (status == SimpleMinCostFlow::Status::NOT_SOLVED) {
@@ -1,19 +1,17 @@
1
1
  #include <ortools/constraint_solver/routing.h>
2
2
  #include <ortools/constraint_solver/routing_parameters.h>
3
3
 
4
- #include <rice/Array.hpp>
5
- #include <rice/Class.hpp>
6
- #include <rice/Constructor.hpp>
7
- #include <rice/Module.hpp>
8
- #include <rice/String.hpp>
9
- #include <rice/Symbol.hpp>
4
+ #include "ext.h"
10
5
 
6
+ using operations_research::Assignment;
7
+ using operations_research::ConstraintSolverParameters;
11
8
  using operations_research::DefaultRoutingSearchParameters;
12
9
  using operations_research::FirstSolutionStrategy;
13
10
  using operations_research::LocalSearchMetaheuristic;
14
11
  using operations_research::RoutingDimension;
15
12
  using operations_research::RoutingIndexManager;
16
13
  using operations_research::RoutingModel;
14
+ using operations_research::RoutingModelParameters;
17
15
  using operations_research::RoutingNodeIndex;
18
16
  using operations_research::RoutingSearchParameters;
19
17
 
@@ -24,99 +22,50 @@ using Rice::Object;
24
22
  using Rice::String;
25
23
  using Rice::Symbol;
26
24
 
27
- template<>
28
- inline
29
- RoutingNodeIndex from_ruby<RoutingNodeIndex>(Object x)
25
+ namespace Rice::detail
30
26
  {
31
- const RoutingNodeIndex index{from_ruby<int>(x)};
32
- return index;
33
- }
34
-
35
- template<>
36
- inline
37
- Object to_ruby<RoutingNodeIndex>(RoutingNodeIndex const &x)
38
- {
39
- return to_ruby<int>(x.value());
40
- }
41
-
42
- std::vector<RoutingNodeIndex> nodeIndexVector(Array x) {
43
- std::vector<RoutingNodeIndex> res;
44
- for (auto const& v : x) {
45
- res.push_back(from_ruby<RoutingNodeIndex>(v));
46
- }
47
- return res;
48
- }
27
+ template<>
28
+ struct Type<RoutingNodeIndex>
29
+ {
30
+ static bool verify()
31
+ {
32
+ return true;
33
+ }
34
+ };
49
35
 
50
- // need a wrapper class due to const
51
- class Assignment {
52
- const operations_research::Assignment* self;
36
+ template<>
37
+ class From_Ruby<RoutingNodeIndex>
38
+ {
53
39
  public:
54
- Assignment(const operations_research::Assignment* v) {
55
- self = v;
56
- }
57
- int64 ObjectiveValue() {
58
- return self->ObjectiveValue();
59
- }
60
- int64 Value(const operations_research::IntVar* const var) const {
61
- return self->Value(var);
62
- }
63
- int64 Min(const operations_research::IntVar* const var) const {
64
- return self->Min(var);
65
- }
66
- int64 Max(const operations_research::IntVar* const var) const {
67
- return self->Max(var);
40
+ RoutingNodeIndex convert(VALUE x)
41
+ {
42
+ const RoutingNodeIndex index{From_Ruby<int>().convert(x)};
43
+ return index;
68
44
  }
69
- };
70
-
71
- Class rb_cIntVar;
72
- Class rb_cIntervalVar;
73
- Class rb_cRoutingDimension;
74
- Class rb_cConstraint;
75
- Class rb_cSolver2;
76
-
77
- template<>
78
- inline
79
- Object to_ruby<operations_research::IntVar*>(operations_research::IntVar* const &x)
80
- {
81
- return Rice::Data_Object<operations_research::IntVar>(x, rb_cIntVar, nullptr, nullptr);
82
- }
83
-
84
- template<>
85
- inline
86
- Object to_ruby<operations_research::IntervalVar*>(operations_research::IntervalVar* const &x)
87
- {
88
- return Rice::Data_Object<operations_research::IntervalVar>(x, rb_cIntervalVar, nullptr, nullptr);
89
- }
90
-
91
- template<>
92
- inline
93
- Object to_ruby<RoutingDimension*>(RoutingDimension* const &x)
94
- {
95
- return Rice::Data_Object<RoutingDimension>(x, rb_cRoutingDimension, nullptr, nullptr);
96
- }
45
+ };
97
46
 
98
- template<>
99
- inline
100
- Object to_ruby<operations_research::Constraint*>(operations_research::Constraint* const &x)
101
- {
102
- return Rice::Data_Object<operations_research::Constraint>(x, rb_cConstraint, nullptr, nullptr);
103
- }
104
-
105
- template<>
106
- inline
107
- Object to_ruby<operations_research::Solver*>(operations_research::Solver* const &x)
108
- {
109
- return Rice::Data_Object<operations_research::Solver>(x, rb_cSolver2, nullptr, nullptr);
47
+ template<>
48
+ class To_Ruby<RoutingNodeIndex>
49
+ {
50
+ public:
51
+ VALUE convert(RoutingNodeIndex const & x)
52
+ {
53
+ return To_Ruby<int>().convert(x.value());
54
+ }
55
+ };
110
56
  }
111
57
 
112
58
  void init_routing(Rice::Module& m) {
113
- m.define_singleton_method("default_routing_search_parameters", &DefaultRoutingSearchParameters);
59
+ auto rb_cRoutingSearchParameters = Rice::define_class_under<RoutingSearchParameters>(m, "RoutingSearchParameters");
60
+ auto rb_cIntVar = Rice::define_class_under<operations_research::IntVar>(m, "IntVar");
114
61
 
115
- Rice::define_class_under<RoutingSearchParameters>(m, "RoutingSearchParameters")
62
+ m.define_singleton_function("default_routing_search_parameters", &DefaultRoutingSearchParameters);
63
+
64
+ rb_cRoutingSearchParameters
116
65
  .define_method(
117
66
  "first_solution_strategy=",
118
- *[](RoutingSearchParameters& self, Symbol value) {
119
- std::string s = Symbol(value).str();
67
+ [](RoutingSearchParameters& self, Symbol value) {
68
+ auto s = Symbol(value).str();
120
69
 
121
70
  FirstSolutionStrategy::Value v;
122
71
  if (s == "path_cheapest_arc") {
@@ -155,8 +104,8 @@ void init_routing(Rice::Module& m) {
155
104
  })
156
105
  .define_method(
157
106
  "local_search_metaheuristic=",
158
- *[](RoutingSearchParameters& self, Symbol value) {
159
- std::string s = Symbol(value).str();
107
+ [](RoutingSearchParameters& self, Symbol value) {
108
+ auto s = Symbol(value).str();
160
109
 
161
110
  LocalSearchMetaheuristic::Value v;
162
111
  if (s == "guided_local_search") {
@@ -175,35 +124,35 @@ void init_routing(Rice::Module& m) {
175
124
  })
176
125
  .define_method(
177
126
  "log_search=",
178
- *[](RoutingSearchParameters& self, bool value) {
127
+ [](RoutingSearchParameters& self, bool value) {
179
128
  self.set_log_search(value);
180
129
  })
181
130
  .define_method(
182
131
  "solution_limit=",
183
- *[](RoutingSearchParameters& self, int64 value) {
132
+ [](RoutingSearchParameters& self, int64_t value) {
184
133
  self.set_solution_limit(value);
185
134
  })
186
135
  .define_method(
187
136
  "time_limit=",
188
- *[](RoutingSearchParameters& self, int64 value) {
137
+ [](RoutingSearchParameters& self, int64_t value) {
189
138
  self.mutable_time_limit()->set_seconds(value);
190
139
  })
191
140
  .define_method(
192
141
  "lns_time_limit=",
193
- *[](RoutingSearchParameters& self, int64 value) {
142
+ [](RoutingSearchParameters& self, int64_t value) {
194
143
  self.mutable_lns_time_limit()->set_seconds(value);
195
144
  });
196
145
 
197
146
  Rice::define_class_under<RoutingIndexManager>(m, "RoutingIndexManager")
198
- .define_singleton_method(
147
+ .define_singleton_function(
199
148
  "_new_depot",
200
- *[](int num_nodes, int num_vehicles, RoutingNodeIndex depot) {
149
+ [](int num_nodes, int num_vehicles, RoutingNodeIndex depot) {
201
150
  return RoutingIndexManager(num_nodes, num_vehicles, depot);
202
151
  })
203
- .define_singleton_method(
152
+ .define_singleton_function(
204
153
  "_new_starts_ends",
205
- *[](int num_nodes, int num_vehicles, Array starts, Array ends) {
206
- return RoutingIndexManager(num_nodes, num_vehicles, nodeIndexVector(starts), nodeIndexVector(ends));
154
+ [](int num_nodes, int num_vehicles, std::vector<RoutingNodeIndex> starts, std::vector<RoutingNodeIndex> ends) {
155
+ return RoutingIndexManager(num_nodes, num_vehicles, starts, ends);
207
156
  })
208
157
  .define_method("index_to_node", &RoutingIndexManager::IndexToNode)
209
158
  .define_method("node_to_index", &RoutingIndexManager::NodeToIndex);
@@ -215,29 +164,55 @@ void init_routing(Rice::Module& m) {
215
164
  .define_method("max", &Assignment::Max);
216
165
 
217
166
  // not to be confused with operations_research::sat::IntVar
218
- rb_cIntVar = Rice::define_class_under<operations_research::IntVar>(m, "IntVar")
167
+ rb_cIntVar
168
+ .define_method("var?", &operations_research::IntVar::IsVar)
169
+ .define_method("value", &operations_research::IntVar::Value)
170
+ .define_method("remove_value", &operations_research::IntVar::RemoveValue)
171
+ .define_method("remove_interval", &operations_research::IntVar::RemoveInterval)
172
+ .define_method("remove_values", &operations_research::IntVar::RemoveValues)
173
+ .define_method("set_values", &operations_research::IntVar::SetValues)
174
+ .define_method("size", &operations_research::IntVar::Size)
175
+ .define_method("contains", &operations_research::IntVar::Contains)
176
+ .define_method("old_min", &operations_research::IntVar::OldMin)
177
+ .define_method("old_max", &operations_research::IntVar::OldMax)
219
178
  .define_method(
220
179
  "set_range",
221
- *[](operations_research::IntVar& self, int64 new_min, int64 new_max) {
180
+ [](operations_research::IntVar& self, int64_t new_min, int64_t new_max) {
222
181
  self.SetRange(new_min, new_max);
223
182
  });
224
183
 
225
- rb_cIntervalVar = Rice::define_class_under<operations_research::IntervalVar>(m, "IntervalVar");
184
+ Rice::define_class_under<operations_research::IntervalVar>(m, "IntervalVar")
185
+ .define_method("start_min", &operations_research::IntervalVar::StartMin)
186
+ .define_method("start_max", &operations_research::IntervalVar::StartMax)
187
+ .define_method("set_start_min", &operations_research::IntervalVar::SetStartMin)
188
+ .define_method("set_start_max", &operations_research::IntervalVar::SetStartMax)
189
+ .define_method("set_start_range", &operations_research::IntervalVar::SetStartRange)
190
+ .define_method("old_start_min", &operations_research::IntervalVar::OldStartMin)
191
+ .define_method("old_start_max", &operations_research::IntervalVar::OldStartMax)
192
+ .define_method("end_min", &operations_research::IntervalVar::EndMin)
193
+ .define_method("end_max", &operations_research::IntervalVar::EndMax)
194
+ .define_method("set_end_min", &operations_research::IntervalVar::SetEndMin)
195
+ .define_method("set_end_max", &operations_research::IntervalVar::SetEndMax)
196
+ .define_method("set_end_range", &operations_research::IntervalVar::SetEndRange)
197
+ .define_method("old_end_min", &operations_research::IntervalVar::OldEndMin)
198
+ .define_method("old_end_max", &operations_research::IntervalVar::OldEndMax);
226
199
 
227
- rb_cRoutingDimension = Rice::define_class_under<RoutingDimension>(m, "RoutingDimension")
200
+ Rice::define_class_under<RoutingDimension>(m, "RoutingDimension")
228
201
  .define_method("global_span_cost_coefficient=", &RoutingDimension::SetGlobalSpanCostCoefficient)
229
202
  .define_method("cumul_var", &RoutingDimension::CumulVar);
230
203
 
231
- rb_cConstraint = Rice::define_class_under<operations_research::Constraint>(m, "Constraint");
204
+ Rice::define_class_under<operations_research::Constraint>(m, "Constraint")
205
+ .define_method("post", &operations_research::Constraint::Post)
206
+ .define_method("debug_string", &operations_research::Constraint::DebugString);
232
207
 
233
- rb_cSolver2 = Rice::define_class_under<operations_research::Solver>(m, "Solver2")
208
+ Rice::define_class_under<operations_research::Solver>(m, "Solver2")
234
209
  .define_method(
235
210
  "add",
236
- *[](operations_research::Solver& self, Object o) {
211
+ [](operations_research::Solver& self, Object o) {
237
212
  operations_research::Constraint* constraint;
238
213
  if (o.respond_to("left")) {
239
- operations_research::IntExpr* left(from_ruby<operations_research::IntVar*>(o.call("left")));
240
- operations_research::IntExpr* right(from_ruby<operations_research::IntVar*>(o.call("right")));
214
+ operations_research::IntExpr* left(Rice::detail::From_Ruby<operations_research::IntVar*>().convert(o.call("left")));
215
+ operations_research::IntExpr* right(Rice::detail::From_Ruby<operations_research::IntVar*>().convert(o.call("right")));
241
216
  auto op = o.call("operator").to_s().str();
242
217
  if (op == "==") {
243
218
  constraint = self.MakeEquality(left, right);
@@ -247,54 +222,69 @@ void init_routing(Rice::Module& m) {
247
222
  throw std::runtime_error("Unknown operator");
248
223
  }
249
224
  } else {
250
- constraint = from_ruby<operations_research::Constraint*>(o);
225
+ constraint = Rice::detail::From_Ruby<operations_research::Constraint*>().convert(o);
251
226
  }
252
227
  self.AddConstraint(constraint);
253
228
  })
254
229
  .define_method(
255
230
  "fixed_duration_interval_var",
256
- *[](operations_research::Solver& self, operations_research::IntVar* const start_variable, int64 duration, const std::string& name) {
231
+ [](operations_research::Solver& self, operations_research::IntVar* const start_variable, int64_t duration, const std::string& name) {
257
232
  return self.MakeFixedDurationIntervalVar(start_variable, duration, name);
258
233
  })
259
234
  .define_method(
260
235
  "cumulative",
261
- *[](operations_research::Solver& self, Array rb_intervals, Array rb_demands, int64 capacity, const std::string& name) {
262
- std::vector<operations_research::IntervalVar*> intervals;
263
- for (std::size_t i = 0; i < rb_intervals.size(); ++i) {
264
- intervals.push_back(from_ruby<operations_research::IntervalVar*>(rb_intervals[i]));
265
- }
236
+ [](operations_research::Solver& self, std::vector<operations_research::IntervalVar*> intervals, std::vector<int64_t> demands, int64_t capacity, const std::string& name) {
237
+ return self.MakeCumulative(intervals, demands, capacity, name);
238
+ });
266
239
 
267
- std::vector<int64> demands;
268
- for (std::size_t i = 0; i < rb_demands.size(); ++i) {
269
- demands.push_back(from_ruby<int64>(rb_demands[i]));
270
- }
240
+ Rice::define_class_under<ConstraintSolverParameters>(m, "ConstraintSolverParameters")
241
+ .define_method(
242
+ "trace_propagation=",
243
+ [](ConstraintSolverParameters& self, bool value) {
244
+ self.set_trace_propagation(value);
245
+ })
246
+ .define_method(
247
+ "trace_search=",
248
+ [](ConstraintSolverParameters& self, bool value) {
249
+ self.set_trace_search(value);
250
+ });
271
251
 
272
- return self.MakeCumulative(intervals, demands, capacity, name);
252
+ Rice::define_class_under<RoutingModelParameters>(m, "RoutingModelParameters")
253
+ .define_method(
254
+ "solver_parameters",
255
+ [](RoutingModelParameters& self) {
256
+ return self.mutable_solver_parameters();
273
257
  });
274
258
 
259
+ m.define_singleton_function(
260
+ "default_routing_model_parameters",
261
+ []() {
262
+ return operations_research::DefaultRoutingModelParameters();
263
+ });
264
+
275
265
  Rice::define_class_under<RoutingModel>(m, "RoutingModel")
276
- .define_constructor(Rice::Constructor<RoutingModel, RoutingIndexManager>())
266
+ .define_constructor(Rice::Constructor<RoutingModel, RoutingIndexManager, RoutingModelParameters>(), Rice::Arg("index_manager"), Rice::Arg("parameters") = operations_research::DefaultRoutingModelParameters())
277
267
  .define_method(
278
268
  "register_transit_callback",
279
- *[](RoutingModel& self, Object callback) {
269
+ [](RoutingModel& self, Object callback) {
280
270
  return self.RegisterTransitCallback(
281
- [callback](int64 from_index, int64 to_index) -> int64 {
282
- return from_ruby<int64>(callback.call("call", from_index, to_index));
271
+ [callback](int64_t from_index, int64_t to_index) -> int64_t {
272
+ return Rice::detail::From_Ruby<int64_t>().convert(callback.call("call", from_index, to_index));
283
273
  }
284
274
  );
285
275
  })
286
276
  .define_method(
287
277
  "register_unary_transit_callback",
288
- *[](RoutingModel& self, Object callback) {
278
+ [](RoutingModel& self, Object callback) {
289
279
  return self.RegisterUnaryTransitCallback(
290
- [callback](int64 from_index) -> int64 {
291
- return from_ruby<int64>(callback.call("call", from_index));
280
+ [callback](int64_t from_index) -> int64_t {
281
+ return Rice::detail::From_Ruby<int64_t>().convert(callback.call("call", from_index));
292
282
  }
293
283
  );
294
284
  })
295
285
  .define_method("depot", &RoutingModel::GetDepot)
296
286
  .define_method("size", &RoutingModel::Size)
297
- .define_method("status", *[](RoutingModel& self) {
287
+ .define_method("status", [](RoutingModel& self) {
298
288
  auto status = self.status();
299
289
 
300
290
  if (status == RoutingModel::ROUTING_NOT_SOLVED) {
@@ -320,29 +310,17 @@ void init_routing(Rice::Module& m) {
320
310
  .define_method("add_dimension", &RoutingModel::AddDimension)
321
311
  .define_method(
322
312
  "add_dimension_with_vehicle_capacity",
323
- *[](RoutingModel& self, int evaluator_index, int64 slack_max, Array vc, bool fix_start_cumul_to_zero, const std::string& name) {
324
- std::vector<int64> vehicle_capacities;
325
- for (std::size_t i = 0; i < vc.size(); ++i) {
326
- vehicle_capacities.push_back(from_ruby<int64>(vc[i]));
327
- }
313
+ [](RoutingModel& self, int evaluator_index, int64_t slack_max, std::vector<int64_t> vehicle_capacities, bool fix_start_cumul_to_zero, const std::string& name) {
328
314
  self.AddDimensionWithVehicleCapacity(evaluator_index, slack_max, vehicle_capacities, fix_start_cumul_to_zero, name);
329
315
  })
330
316
  .define_method(
331
317
  "add_dimension_with_vehicle_transits",
332
- *[](RoutingModel& self, Array rb_indices, int64 slack_max, int64 capacity, bool fix_start_cumul_to_zero, const std::string& name) {
333
- std::vector<int> evaluator_indices;
334
- for (std::size_t i = 0; i < rb_indices.size(); ++i) {
335
- evaluator_indices.push_back(from_ruby<int>(rb_indices[i]));
336
- }
318
+ [](RoutingModel& self, std::vector<int> evaluator_indices, int64_t slack_max, int64_t capacity, bool fix_start_cumul_to_zero, const std::string& name) {
337
319
  self.AddDimensionWithVehicleTransits(evaluator_indices, slack_max, capacity, fix_start_cumul_to_zero, name);
338
320
  })
339
321
  .define_method(
340
322
  "add_disjunction",
341
- *[](RoutingModel& self, Array rb_indices, int64 penalty) {
342
- std::vector<int64> indices;
343
- for (std::size_t i = 0; i < rb_indices.size(); ++i) {
344
- indices.push_back(from_ruby<int64>(rb_indices[i]));
345
- }
323
+ [](RoutingModel& self, std::vector<int64_t> indices, int64_t penalty) {
346
324
  self.AddDisjunction(indices, penalty);
347
325
  })
348
326
  .define_method("add_pickup_and_delivery", &RoutingModel::AddPickupAndDelivery)
@@ -360,9 +338,7 @@ void init_routing(Rice::Module& m) {
360
338
  .define_method("add_variable_minimized_by_finalizer", &RoutingModel::AddVariableMinimizedByFinalizer)
361
339
  .define_method(
362
340
  "solve_with_parameters",
363
- *[](RoutingModel& self, const RoutingSearchParameters& search_parameters) {
364
- auto assignment = self.SolveWithParameters(search_parameters);
365
- // std::cout << assignment->DebugString();
366
- return (Assignment) assignment;
341
+ [](RoutingModel& self, const RoutingSearchParameters& search_parameters) {
342
+ return self.SolveWithParameters(search_parameters);
367
343
  });
368
344
  }