or-tools 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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) {
@@ -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,13 +1,9 @@
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;
11
7
  using operations_research::DefaultRoutingSearchParameters;
12
8
  using operations_research::FirstSolutionStrategy;
13
9
  using operations_research::LocalSearchMetaheuristic;
@@ -24,99 +20,50 @@ using Rice::Object;
24
20
  using Rice::String;
25
21
  using Rice::Symbol;
26
22
 
27
- template<>
28
- inline
29
- RoutingNodeIndex from_ruby<RoutingNodeIndex>(Object x)
23
+ namespace Rice::detail
30
24
  {
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
- }
25
+ template<>
26
+ struct Type<RoutingNodeIndex>
27
+ {
28
+ static bool verify()
29
+ {
30
+ return true;
31
+ }
32
+ };
49
33
 
50
- // need a wrapper class due to const
51
- class Assignment {
52
- const operations_research::Assignment* self;
34
+ template<>
35
+ class From_Ruby<RoutingNodeIndex>
36
+ {
53
37
  public:
54
- Assignment(const operations_research::Assignment* v) {
55
- self = v;
56
- }
57
- int64_t ObjectiveValue() {
58
- return self->ObjectiveValue();
59
- }
60
- int64_t Value(const operations_research::IntVar* const var) const {
61
- return self->Value(var);
38
+ RoutingNodeIndex convert(VALUE x)
39
+ {
40
+ const RoutingNodeIndex index{From_Ruby<int>().convert(x)};
41
+ return index;
62
42
  }
63
- int64_t Min(const operations_research::IntVar* const var) const {
64
- return self->Min(var);
65
- }
66
- int64_t Max(const operations_research::IntVar* const var) const {
67
- return self->Max(var);
68
- }
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
- }
43
+ };
90
44
 
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
- }
97
-
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);
45
+ template<>
46
+ class To_Ruby<RoutingNodeIndex>
47
+ {
48
+ public:
49
+ VALUE convert(RoutingNodeIndex const & x)
50
+ {
51
+ return To_Ruby<int>().convert(x.value());
52
+ }
53
+ };
110
54
  }
111
55
 
112
56
  void init_routing(Rice::Module& m) {
113
- m.define_singleton_method("default_routing_search_parameters", &DefaultRoutingSearchParameters);
57
+ auto rb_cRoutingSearchParameters = Rice::define_class_under<RoutingSearchParameters>(m, "RoutingSearchParameters");
58
+ auto rb_cIntVar = Rice::define_class_under<operations_research::IntVar>(m, "IntVar");
114
59
 
115
- Rice::define_class_under<RoutingSearchParameters>(m, "RoutingSearchParameters")
60
+ m.define_singleton_function("default_routing_search_parameters", &DefaultRoutingSearchParameters);
61
+
62
+ rb_cRoutingSearchParameters
116
63
  .define_method(
117
64
  "first_solution_strategy=",
118
- *[](RoutingSearchParameters& self, Symbol value) {
119
- std::string s = Symbol(value).str();
65
+ [](RoutingSearchParameters& self, Symbol value) {
66
+ auto s = Symbol(value).str();
120
67
 
121
68
  FirstSolutionStrategy::Value v;
122
69
  if (s == "path_cheapest_arc") {
@@ -155,8 +102,8 @@ void init_routing(Rice::Module& m) {
155
102
  })
156
103
  .define_method(
157
104
  "local_search_metaheuristic=",
158
- *[](RoutingSearchParameters& self, Symbol value) {
159
- std::string s = Symbol(value).str();
105
+ [](RoutingSearchParameters& self, Symbol value) {
106
+ auto s = Symbol(value).str();
160
107
 
161
108
  LocalSearchMetaheuristic::Value v;
162
109
  if (s == "guided_local_search") {
@@ -175,35 +122,35 @@ void init_routing(Rice::Module& m) {
175
122
  })
176
123
  .define_method(
177
124
  "log_search=",
178
- *[](RoutingSearchParameters& self, bool value) {
125
+ [](RoutingSearchParameters& self, bool value) {
179
126
  self.set_log_search(value);
180
127
  })
181
128
  .define_method(
182
129
  "solution_limit=",
183
- *[](RoutingSearchParameters& self, int64_t value) {
130
+ [](RoutingSearchParameters& self, int64_t value) {
184
131
  self.set_solution_limit(value);
185
132
  })
186
133
  .define_method(
187
134
  "time_limit=",
188
- *[](RoutingSearchParameters& self, int64_t value) {
135
+ [](RoutingSearchParameters& self, int64_t value) {
189
136
  self.mutable_time_limit()->set_seconds(value);
190
137
  })
191
138
  .define_method(
192
139
  "lns_time_limit=",
193
- *[](RoutingSearchParameters& self, int64_t value) {
140
+ [](RoutingSearchParameters& self, int64_t value) {
194
141
  self.mutable_lns_time_limit()->set_seconds(value);
195
142
  });
196
143
 
197
144
  Rice::define_class_under<RoutingIndexManager>(m, "RoutingIndexManager")
198
- .define_singleton_method(
145
+ .define_singleton_function(
199
146
  "_new_depot",
200
- *[](int num_nodes, int num_vehicles, RoutingNodeIndex depot) {
147
+ [](int num_nodes, int num_vehicles, RoutingNodeIndex depot) {
201
148
  return RoutingIndexManager(num_nodes, num_vehicles, depot);
202
149
  })
203
- .define_singleton_method(
150
+ .define_singleton_function(
204
151
  "_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));
152
+ [](int num_nodes, int num_vehicles, std::vector<RoutingNodeIndex> starts, std::vector<RoutingNodeIndex> ends) {
153
+ return RoutingIndexManager(num_nodes, num_vehicles, starts, ends);
207
154
  })
208
155
  .define_method("index_to_node", &RoutingIndexManager::IndexToNode)
209
156
  .define_method("node_to_index", &RoutingIndexManager::NodeToIndex);
@@ -215,29 +162,29 @@ void init_routing(Rice::Module& m) {
215
162
  .define_method("max", &Assignment::Max);
216
163
 
217
164
  // not to be confused with operations_research::sat::IntVar
218
- rb_cIntVar = Rice::define_class_under<operations_research::IntVar>(m, "IntVar")
165
+ rb_cIntVar
219
166
  .define_method(
220
167
  "set_range",
221
- *[](operations_research::IntVar& self, int64_t new_min, int64_t new_max) {
168
+ [](operations_research::IntVar& self, int64_t new_min, int64_t new_max) {
222
169
  self.SetRange(new_min, new_max);
223
170
  });
224
171
 
225
- rb_cIntervalVar = Rice::define_class_under<operations_research::IntervalVar>(m, "IntervalVar");
172
+ Rice::define_class_under<operations_research::IntervalVar>(m, "IntervalVar");
226
173
 
227
- rb_cRoutingDimension = Rice::define_class_under<RoutingDimension>(m, "RoutingDimension")
174
+ Rice::define_class_under<RoutingDimension>(m, "RoutingDimension")
228
175
  .define_method("global_span_cost_coefficient=", &RoutingDimension::SetGlobalSpanCostCoefficient)
229
176
  .define_method("cumul_var", &RoutingDimension::CumulVar);
230
177
 
231
- rb_cConstraint = Rice::define_class_under<operations_research::Constraint>(m, "Constraint");
178
+ Rice::define_class_under<operations_research::Constraint>(m, "Constraint");
232
179
 
233
- rb_cSolver2 = Rice::define_class_under<operations_research::Solver>(m, "Solver2")
180
+ Rice::define_class_under<operations_research::Solver>(m, "Solver2")
234
181
  .define_method(
235
182
  "add",
236
- *[](operations_research::Solver& self, Object o) {
183
+ [](operations_research::Solver& self, Object o) {
237
184
  operations_research::Constraint* constraint;
238
185
  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")));
186
+ operations_research::IntExpr* left(Rice::detail::From_Ruby<operations_research::IntVar*>().convert(o.call("left")));
187
+ operations_research::IntExpr* right(Rice::detail::From_Ruby<operations_research::IntVar*>().convert(o.call("right")));
241
188
  auto op = o.call("operator").to_s().str();
242
189
  if (op == "==") {
243
190
  constraint = self.MakeEquality(left, right);
@@ -247,28 +194,18 @@ void init_routing(Rice::Module& m) {
247
194
  throw std::runtime_error("Unknown operator");
248
195
  }
249
196
  } else {
250
- constraint = from_ruby<operations_research::Constraint*>(o);
197
+ constraint = Rice::detail::From_Ruby<operations_research::Constraint*>().convert(o);
251
198
  }
252
199
  self.AddConstraint(constraint);
253
200
  })
254
201
  .define_method(
255
202
  "fixed_duration_interval_var",
256
- *[](operations_research::Solver& self, operations_research::IntVar* const start_variable, int64_t duration, const std::string& name) {
203
+ [](operations_research::Solver& self, operations_research::IntVar* const start_variable, int64_t duration, const std::string& name) {
257
204
  return self.MakeFixedDurationIntervalVar(start_variable, duration, name);
258
205
  })
259
206
  .define_method(
260
207
  "cumulative",
261
- *[](operations_research::Solver& self, Array rb_intervals, Array rb_demands, int64_t 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
- }
266
-
267
- std::vector<int64_t> demands;
268
- for (std::size_t i = 0; i < rb_demands.size(); ++i) {
269
- demands.push_back(from_ruby<int64_t>(rb_demands[i]));
270
- }
271
-
208
+ [](operations_research::Solver& self, std::vector<operations_research::IntervalVar*> intervals, std::vector<int64_t> demands, int64_t capacity, const std::string& name) {
272
209
  return self.MakeCumulative(intervals, demands, capacity, name);
273
210
  });
274
211
 
@@ -276,25 +213,25 @@ void init_routing(Rice::Module& m) {
276
213
  .define_constructor(Rice::Constructor<RoutingModel, RoutingIndexManager>())
277
214
  .define_method(
278
215
  "register_transit_callback",
279
- *[](RoutingModel& self, Object callback) {
216
+ [](RoutingModel& self, Object callback) {
280
217
  return self.RegisterTransitCallback(
281
218
  [callback](int64_t from_index, int64_t to_index) -> int64_t {
282
- return from_ruby<int64_t>(callback.call("call", from_index, to_index));
219
+ return Rice::detail::From_Ruby<int64_t>().convert(callback.call("call", from_index, to_index));
283
220
  }
284
221
  );
285
222
  })
286
223
  .define_method(
287
224
  "register_unary_transit_callback",
288
- *[](RoutingModel& self, Object callback) {
225
+ [](RoutingModel& self, Object callback) {
289
226
  return self.RegisterUnaryTransitCallback(
290
227
  [callback](int64_t from_index) -> int64_t {
291
- return from_ruby<int64_t>(callback.call("call", from_index));
228
+ return Rice::detail::From_Ruby<int64_t>().convert(callback.call("call", from_index));
292
229
  }
293
230
  );
294
231
  })
295
232
  .define_method("depot", &RoutingModel::GetDepot)
296
233
  .define_method("size", &RoutingModel::Size)
297
- .define_method("status", *[](RoutingModel& self) {
234
+ .define_method("status", [](RoutingModel& self) {
298
235
  auto status = self.status();
299
236
 
300
237
  if (status == RoutingModel::ROUTING_NOT_SOLVED) {
@@ -320,29 +257,17 @@ void init_routing(Rice::Module& m) {
320
257
  .define_method("add_dimension", &RoutingModel::AddDimension)
321
258
  .define_method(
322
259
  "add_dimension_with_vehicle_capacity",
323
- *[](RoutingModel& self, int evaluator_index, int64_t slack_max, Array vc, bool fix_start_cumul_to_zero, const std::string& name) {
324
- std::vector<int64_t> vehicle_capacities;
325
- for (std::size_t i = 0; i < vc.size(); ++i) {
326
- vehicle_capacities.push_back(from_ruby<int64_t>(vc[i]));
327
- }
260
+ [](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
261
  self.AddDimensionWithVehicleCapacity(evaluator_index, slack_max, vehicle_capacities, fix_start_cumul_to_zero, name);
329
262
  })
330
263
  .define_method(
331
264
  "add_dimension_with_vehicle_transits",
332
- *[](RoutingModel& self, Array rb_indices, int64_t slack_max, int64_t 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
- }
265
+ [](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
266
  self.AddDimensionWithVehicleTransits(evaluator_indices, slack_max, capacity, fix_start_cumul_to_zero, name);
338
267
  })
339
268
  .define_method(
340
269
  "add_disjunction",
341
- *[](RoutingModel& self, Array rb_indices, int64_t penalty) {
342
- std::vector<int64_t> indices;
343
- for (std::size_t i = 0; i < rb_indices.size(); ++i) {
344
- indices.push_back(from_ruby<int64_t>(rb_indices[i]));
345
- }
270
+ [](RoutingModel& self, std::vector<int64_t> indices, int64_t penalty) {
346
271
  self.AddDisjunction(indices, penalty);
347
272
  })
348
273
  .define_method("add_pickup_and_delivery", &RoutingModel::AddPickupAndDelivery)
@@ -360,9 +285,7 @@ void init_routing(Rice::Module& m) {
360
285
  .define_method("add_variable_minimized_by_finalizer", &RoutingModel::AddVariableMinimizedByFinalizer)
361
286
  .define_method(
362
287
  "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;
288
+ [](RoutingModel& self, const RoutingSearchParameters& search_parameters) {
289
+ return self.SolveWithParameters(search_parameters);
367
290
  });
368
291
  }