Skip to content

Commit b0cf288

Browse files
committed
Various changes
1. Fixing initialization of parameter values 2. Fixing bugs in compact LP writer
1 parent 22edeec commit b0cf288

11 files changed

+71
-47
lines changed

Diff for: lib/coek/coek/api/data_assoc_array_repn.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ void DataAssocArrayRepn::value(const Expression& expr)
3636

3737
void DataAssocArrayRepn::name(const std::string& name) { value_template.name(name); }
3838

39+
std::string DataAssocArrayRepn::name() { return value_template.name(); }
40+
3941
} // namespace coek

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ class DataAssocArrayRepn : public AssocArrayBase<ConstantTerm> {
2020

2121
virtual void expand();
2222

23-
/** Set the initial variable value. */
23+
/** Set the initial data value. */
2424
void value(double value);
25-
/** Set the initial variable value. */
25+
/** Set the initial data value. */
2626
void value(const Expression& value);
2727

28-
/** Set the name of the variable. */
28+
/** Set the name of the data. */
2929
void name(const std::string& name);
3030

31+
/** Get the name of the data. */
32+
std::string name();
33+
3134
#ifdef COEK_WITH_COMPACT_MODEL
3235
expr_pointer_t create_ref(const std::vector<refarg_types>& indices);
3336
#endif

Diff for: lib/coek/coek/api/parameter_assoc_array_repn.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ void ParameterAssocArrayRepn::name(const std::string& name)
5252
}
5353
}
5454

55+
std::string ParameterAssocArrayRepn::name() { return value_template.name(); }
56+
5557
} // namespace coek

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ class ParameterAssocArrayRepn : public AssocArrayBase<ParameterTerm> {
1717

1818
virtual void expand();
1919

20-
/** Set the initial variable value. */
20+
/** Set the initial parameter value. */
2121
void value(double value);
22-
/** Set the initial variable value. */
22+
/** Set the initial parameter value. */
2323
void value(const Expression& value);
2424

25-
/** Set the name of the variable. */
25+
/** Set the name of the parameter. */
2626
void name(const std::string& name);
2727

28+
/** Get the name of the parameter. */
29+
std::string name();
30+
2831
#ifdef COEK_WITH_COMPACT_MODEL
2932
expr_pointer_t create_ref(const std::vector<refarg_types>& indices);
3033
#endif

Diff for: lib/coek/coek/compact/data_map.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ DataMap& DataMap::name(const std::string& name)
149149
return *this;
150150
}
151151

152+
std::string DataMap::name() { return repn->name(); }
153+
152154
//
153155
// OTHER
154156
//

Diff for: lib/coek/coek/compact/data_map.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ class DataMap : public DataAssocArray {
137137

138138
/** Set the name of the data. \returns the data object */
139139
DataMap& name(const std::string& name);
140+
141+
/** Get the name of the data. \returns a string. */
142+
std::string name();
140143
};
141144

142145
template <typename KeyType, typename ValueType>

Diff for: lib/coek/coek/compact/parameter_map.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,20 @@ std::shared_ptr<ParameterTerm> ParameterMapRepn::index(const IndexVector& args)
146146
// std::cerr << "HERE " << index_map.size() << std::endl;
147147
// std::cerr << "HERE " << args << std::endl;
148148
// for (auto& [k,v]:index_map)
149-
// std::cerr << "HERE " << k << " " << v << std::endl;
149+
// std::cerr << "HERE " << k << " " << v << std::endl;
150150

151151
auto curr = index_map.find(args);
152-
if (curr == index_map.end()) {
153-
std::string err = "Unknown index value: " + value_template.name() + "[";
154-
for (size_t i = 0; i < args.size(); i++) {
155-
if (i > 0)
156-
err += ",";
157-
err += std::to_string(args[i]);
158-
}
159-
err += "]";
160-
throw std::runtime_error(err);
152+
if (curr != index_map.end())
153+
return values[curr->second].repn;
154+
155+
std::string err = "Unknown index value: " + value_template.name() + "[";
156+
for (size_t i = 0; i < args.size(); i++) {
157+
if (i > 0)
158+
err += ",";
159+
err += std::to_string(args[i]);
161160
}
162-
return values[curr->second].repn;
161+
err += "]";
162+
throw std::runtime_error(err);
163163
}
164164

165165
void ParameterMap::index_error(size_t i)
@@ -195,6 +195,8 @@ ParameterMap& ParameterMap::name(const std::string& name)
195195
return *this;
196196
}
197197

198+
std::string ParameterMap::name() { return repn->name(); }
199+
198200
//
199201
// OTHER
200202
//

Diff for: lib/coek/coek/compact/parameter_map.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ class ParameterMap : public ParameterAssocArray {
138138
template <typename KeyType, typename ValueType>
139139
ParameterMap& value(const std::map<KeyType, ValueType>& values_);
140140

141-
/** Set the name of the parameter. \returns the parameter object */
141+
/** Set the name of the parameter. \returns the parameter object. */
142142
ParameterMap& name(const std::string& name);
143+
144+
/** Get the name of the parameter. \returns a string. */
145+
std::string name();
143146
};
144147

145148
template <typename KeyType, typename ValueType>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ Model CompactModel::expand_data()
294294
eval->expand();
295295
}
296296
else if (auto eval = std::get_if<ParameterMap>(&val)) {
297-
// std::cout << "HERE x" << std::endl;
298297
eval->expand();
298+
// std::cout << "HERE param " << eval->name() << " " << eval->size() << std::endl;
299299
for (auto param : *eval) {
300300
// NOTE: Are we changing the values of these maps in place?
301301
Expression value = param.value_expression().expand();

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

+24-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "../ast/value_terms.hpp"
2121
#include "../ast/visitor_fns.hpp"
22+
#include "coek/util/io_utils.hpp"
2223
#include "coek/api/expression.hpp"
2324
#include "coek/api/constraint.hpp"
2425
#include "coek/api/expression_visitor.hpp"
@@ -250,6 +251,7 @@ void LPWriter::print_objectives(StreamType& ostr, CompactModel& model)
250251
for (auto& val : model.repn->objectives) {
251252
if (auto eval = std::get_if<Objective>(&val)) {
252253
auto obj = objective().expr(eval->expr().expand()).sense(eval->sense());
254+
// std::cout << "HERE obj " << obj.to_list() << std::endl;
253255
print_objective(ostr, obj);
254256
++n_obj;
255257
}
@@ -300,8 +302,8 @@ void LPWriter::print_constraints(StreamType& ostr, CompactModel& model)
300302
}
301303
}
302304
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+
// for (auto jt=cval->begin(); jt != cval->end(); ++jt) {
306+
for (auto& [k, v] : *cval) {
305307
invconmap[v.id()] = ctr;
306308
print_constraint(ostr, v, ctr);
307309
++ctr;
@@ -320,6 +322,7 @@ void LPWriter::collect_variables(Model& model)
320322
size_t ctr = 0;
321323
for (auto& it : model.repn->variables) {
322324
vid[it.id()] = ctr;
325+
// invvarmap[ctr] = ctr; WEH - Is this the right value?
323326
invvarmap[ctr] = it.id();
324327
++ctr;
325328

@@ -352,28 +355,29 @@ void LPWriter::collect_variables(CompactModel& model)
352355
.value(value.value())
353356
.within(eval->within());
354357
variables.push_back(tmp);
358+
vid[tmp.id()] = ctr;
359+
// vid[eval->id()] = ctr;
360+
invvarmap[ctr] = variables.size();
361+
++ctr;
362+
355363
if (tmp.is_binary())
356364
bvars[vid[tmp.id()]] = tmp.repn;
357365
if (tmp.is_integer())
358366
ivars[vid[tmp.id()]] = tmp.repn;
359-
360-
vid[eval->id()] = ctr;
361-
invvarmap[ctr] = variables.size();
362-
++ctr;
363367
}
364368
else if (auto eval = std::get_if<VariableSequence>(&val)) {
365369
for (auto& jt : *eval) {
366370
if (jt.fixed())
367371
continue;
368372
variables.push_back(jt);
373+
vid[jt.id()] = ctr;
374+
invvarmap[ctr] = variables.size();
375+
++ctr;
376+
369377
if (jt.is_binary())
370378
bvars[vid[jt.id()]] = jt.repn;
371379
if (jt.is_integer())
372380
ivars[vid[jt.id()]] = jt.repn;
373-
374-
vid[jt.id()] = ctr;
375-
invvarmap[ctr] = variables.size();
376-
++ctr;
377381
}
378382
}
379383
else if (auto eval = std::get_if<VariableMap>(&val)) {
@@ -382,14 +386,16 @@ void LPWriter::collect_variables(CompactModel& model)
382386
if (jt.fixed())
383387
continue;
384388
variables.push_back(jt);
389+
vid[jt.id()] = ctr;
390+
invvarmap[ctr] = variables.size();
391+
++ctr;
392+
// std::cout << "HELP " << jt.name() << " " << jt.is_binary() << " " <<
393+
// jt.is_integer() << std::endl;
394+
385395
if (jt.is_binary())
386396
bvars[vid[jt.id()]] = jt.repn;
387397
if (jt.is_integer())
388398
ivars[vid[jt.id()]] = jt.repn;
389-
390-
vid[jt.id()] = ctr;
391-
invvarmap[ctr] = variables.size();
392-
++ctr;
393399
}
394400
}
395401
else if (auto eval = std::get_if<VariableArray>(&val)) {
@@ -398,14 +404,14 @@ void LPWriter::collect_variables(CompactModel& model)
398404
if (jt.fixed())
399405
continue;
400406
variables.push_back(jt);
407+
vid[jt.id()] = ctr;
408+
invvarmap[ctr] = variables.size();
409+
++ctr;
410+
401411
if (jt.is_binary())
402412
bvars[vid[jt.id()]] = jt.repn;
403413
if (jt.is_integer())
404414
ivars[vid[jt.id()]] = jt.repn;
405-
406-
vid[jt.id()] = ctr;
407-
invvarmap[ctr] = variables.size();
408-
++ctr;
409415
}
410416
}
411417
}

Diff for: lib/pycoek/pybind11/pycoek_pybind11.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,6 @@ using tuple_of = typename tuple_n<I, T>::template type<>;
657657
return; \
658658
}
659659

660-
// params.value(values);
661-
662660
template <typename TYPE>
663661
void initialize(TYPE& params, coek::DataPortal& dp, const std::string& name)
664662
{
@@ -684,6 +682,14 @@ template <class TYPE>
684682
void set_kwargs_parammap(TYPE& param, py::kwargs kwargs)
685683
{
686684
try {
685+
if (kwargs.contains("name")) {
686+
auto _name = kwargs["name"];
687+
if (not _name.is_none()) {
688+
auto name = _name.cast<py::str>();
689+
param.name(name);
690+
}
691+
}
692+
687693
if (kwargs.contains("data_portal")) {
688694
auto dp_ = kwargs["data_portal"];
689695
auto dp = dp_.cast<coek::DataPortal>();
@@ -694,14 +700,6 @@ void set_kwargs_parammap(TYPE& param, py::kwargs kwargs)
694700
auto value = parse_varargs(kwargs, "value", NAN);
695701
param.value(value);
696702
}
697-
698-
if (kwargs.contains("name")) {
699-
auto _name = kwargs["name"];
700-
if (not _name.is_none()) {
701-
auto name = _name.cast<py::str>();
702-
param.name(name);
703-
}
704-
}
705703
}
706704
catch (std::exception& err) {
707705
throw;

0 commit comments

Comments
 (0)