or-tools 0.5.0 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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) {