Skip to content

Commit 48dbd95

Browse files
committed
Several changes
1. Fixing typename in indexed_container 2. Fixing logic associated with expansion of var/obj/con components
1 parent c7d5803 commit 48dbd95

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

Diff for: lib/coek/coek/api/indexed_container.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class IndexedComponent {
6161
std::vector<refarg_types> reftmp;
6262

6363
public:
64-
using iterator = typename std::map<IndexVector, TYPE>;
64+
using iterator = typename std::map<IndexVector, TYPE>::iterator;
6565
iterator begin() { return repn->value.begin(); }
6666
iterator end() { return repn->value.end(); }
6767

Diff for: lib/coek/coek/model/compact_model.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,8 @@ void CompactModel::add(ConstraintSequence&& seq) { repn->constraints.push_back(s
247247

248248
// Expand
249249

250-
Model CompactModel::expand()
250+
Model CompactModel::expand_data()
251251
{
252-
// std::cout << "CompactModel::expand()" << std::endl;
253252
Model model;
254253

255254
for (auto& val : repn->data) {
@@ -316,6 +315,14 @@ Model CompactModel::expand()
316315
}
317316
}
318317

318+
return model;
319+
}
320+
321+
Model CompactModel::expand()
322+
{
323+
// std::cout << "CompactModel::expand()" << std::endl;
324+
auto model = expand_data();
325+
319326
// std::cout << "VARIABLES " << repn->variables.size() << std::endl;
320327
for (auto& val : repn->variables) {
321328
if (auto eval = std::get_if<Variable>(&val)) {

Diff for: lib/coek/coek/model/compact_model.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class CompactModel {
154154
void add(ConstraintSequence& seq);
155155
void add(ConstraintSequence&& seq);
156156

157+
Model expand_data();
157158
Model expand();
158159

159160
void write(const std::string& filename);

Diff for: lib/coek/coek/model/writer_lp.cpp

+50-9
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ void print_repn(fmt::ostream& ostr, const QuadraticExpr& repn,
166166

167167
class LPWriter {
168168
public:
169+
#ifdef COEK_WITH_COMPACT_MODEL
170+
Model model_handle;
171+
#endif
169172
bool one_var_constant;
170173
std::unordered_map<size_t, size_t> vid;
171174
std::vector<Variable> variables;
@@ -250,9 +253,8 @@ void LPWriter::print_objectives(StreamType& ostr, CompactModel& model)
250253
print_objective(ostr, obj);
251254
++n_obj;
252255
}
253-
else {
254-
auto& seq = std::get<ObjectiveSequence>(val);
255-
for (auto& jt : seq) {
256+
else if (auto eval = std::get_if<ObjectiveSequence>(&val)) {
257+
for (auto& jt : *eval) {
256258
print_objective(ostr, jt);
257259
++n_obj;
258260
}
@@ -290,14 +292,21 @@ void LPWriter::print_constraints(StreamType& ostr, CompactModel& model)
290292
print_constraint(ostr, c, ctr);
291293
++ctr;
292294
}
293-
else {
294-
auto& seq = std::get<ConstraintSequence>(val);
295-
for (auto& jt : seq) {
295+
else if (auto cval = std::get_if<ConstraintSequence>(&val)) {
296+
for (auto& jt : *cval) {
296297
invconmap[jt.id()] = ctr;
297298
print_constraint(ostr, jt, ctr);
298299
++ctr;
299300
}
300301
}
302+
else if (auto cval = std::get_if<ConstraintMap>(&val)) {
303+
//for (auto jt=cval->begin(); jt != cval->end(); ++jt) {
304+
for (auto& [k,v] : *cval) {
305+
invconmap[v.id()] = ctr;
306+
print_constraint(ostr, v, ctr);
307+
++ctr;
308+
}
309+
}
301310
}
302311
}
303312
#endif
@@ -328,6 +337,7 @@ void LPWriter::collect_variables(Model& model)
328337
#ifdef COEK_WITH_COMPACT_MODEL
329338
void LPWriter::collect_variables(CompactModel& model)
330339
{
340+
model_handle = model.expand_data();
331341
size_t ctr = 0;
332342
for (auto& val : model.repn->variables) {
333343
if (auto eval = std::get_if<Variable>(&val)) {
@@ -351,9 +361,40 @@ void LPWriter::collect_variables(CompactModel& model)
351361
invvarmap[ctr] = variables.size();
352362
++ctr;
353363
}
354-
else {
355-
auto& seq = std::get<VariableSequence>(val);
356-
for (auto& jt : seq) {
364+
else if (auto eval = std::get_if<VariableSequence>(&val)) {
365+
for (auto& jt : *eval) {
366+
if (jt.fixed())
367+
continue;
368+
variables.push_back(jt);
369+
if (jt.is_binary())
370+
bvars[vid[jt.id()]] = jt.repn;
371+
if (jt.is_integer())
372+
ivars[vid[jt.id()]] = jt.repn;
373+
374+
vid[jt.id()] = ctr;
375+
invvarmap[ctr] = variables.size();
376+
++ctr;
377+
}
378+
}
379+
else if (auto eval = std::get_if<VariableMap>(&val)) {
380+
eval->expand();
381+
for (auto& jt : *eval) {
382+
if (jt.fixed())
383+
continue;
384+
variables.push_back(jt);
385+
if (jt.is_binary())
386+
bvars[vid[jt.id()]] = jt.repn;
387+
if (jt.is_integer())
388+
ivars[vid[jt.id()]] = jt.repn;
389+
390+
vid[jt.id()] = ctr;
391+
invvarmap[ctr] = variables.size();
392+
++ctr;
393+
}
394+
}
395+
else if (auto eval = std::get_if<VariableArray>(&val)) {
396+
eval->expand();
397+
for (auto& jt : *eval) {
357398
if (jt.fixed())
358399
continue;
359400
variables.push_back(jt);

0 commit comments

Comments
 (0)