Skip to content

Commit b573ae4

Browse files
authored
Merge pull request #61 from sandialabs/solver_api
Update to the solver API
2 parents 126a2ba + 06bd99b commit b573ae4

File tree

156 files changed

+22362
-11541
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+22362
-11541
lines changed

Diff for: .clang-format

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
AlignTrailingComments: 'true'
44
AllowAllArgumentsOnNextLine: 'true'
55
AllowAllParametersOfDeclarationOnNextLine: 'true'
6+
AllowShortLoopsOnASingleLine : 'false'
7+
AllowShortIfStatementsOnASingleLine: 'false'
68
AlwaysBreakTemplateDeclarations: 'Yes'
79
BreakBeforeBraces: Stroustrup
810
ColumnLimit: 100

Diff for: .github/workflows/linux.yml

+21-21
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: Linux Build and Tests
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ main, dev, public, private ]
6+
branches: [ main, dev, dev-weh, public, private ]
77
pull_request:
8-
branches: [ main, dev, public, private ]
8+
branches: [ main, dev, dev-weh, public, private ]
99

1010
jobs:
1111

@@ -44,22 +44,22 @@ jobs:
4444
cd build
4545
make test
4646
47-
coek-build-and-test-with-cpp14:
48-
runs-on: ubuntu-latest
49-
steps:
50-
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
51-
- uses: actions/checkout@v3
52-
- name: run cmake
53-
run: |
54-
mkdir build
55-
cd build
56-
cmake -DCMAKE_CXX_STANDARD=14 -Dwith_python=OFF -Dwith_tests=ON -Dwith_debug=ON ..
57-
- name: build
58-
run: |
59-
cd build
60-
make install_tpls
61-
make
62-
- name: test
63-
run: |
64-
cd build
65-
make test
47+
# coek-build-and-test-with-cpp14:
48+
# runs-on: ubuntu-latest
49+
# steps:
50+
# # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
51+
# - uses: actions/checkout@v3
52+
# - name: run cmake
53+
# run: |
54+
# mkdir build
55+
# cd build
56+
# cmake -DCMAKE_CXX_STANDARD=14 -Dwith_python=OFF -Dwith_tests=ON -Dwith_debug=ON ..
57+
# - name: build
58+
# run: |
59+
# cd build
60+
# make install_tpls
61+
# make
62+
# - name: test
63+
# run: |
64+
# cd build
65+
# make test

Diff for: app/demos/demo1.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,29 @@ int main(int argc, char* argv[])
5858
if (solver_name == "gurobi") {
5959
coek::Solver solver;
6060
solver.initialize(solver_name);
61-
if (not solver.available()) {
62-
std::cout << "ERROR - solver '" << solver_name << "' is not available" << std::endl;
63-
std::cout << "MESSAGE - " << solver.error_message() << std::endl;
64-
return 2;
65-
}
6661
//solver.set_option("OutputFlag", debug);
6762
//solver.set_option("TimeLimit", 0.0);
6863

69-
solver.solve(model);
64+
auto res = solver.solve(model);
65+
if (not check_optimal_termination(res)) {
66+
std::cout << "ERROR - failed to execute solver '" << solver_name << "'" << std::endl;
67+
std::cout << "MESSAGE - " << res->error_message << std::endl;
68+
return 2;
69+
}
7070
}
7171
else if (solver_name == "ipopt") {
7272
coek::NLPModel nlpmodel(model, "cppad");
7373
coek::NLPSolver solver;
7474
solver.initialize(solver_name);
75-
if (not solver.available()) {
76-
std::cout << "ERROR - solver '" << solver_name << "' is not available" << std::endl;
77-
std::cout << "MESSAGE - " << solver.error_message() << std::endl;
78-
return 2;
79-
}
8075
//solver.set_option("max_iter", 1);
8176
//solver.set_option("print_level", 1);
8277

83-
solver.solve(nlpmodel);
78+
auto res = solver.solve(nlpmodel);
79+
if (not check_optimal_termination(res)) {
80+
std::cout << "ERROR - failed to execute solver '" << solver_name << "'" << std::endl;
81+
std::cout << "MESSAGE - " << res->error_message << std::endl;
82+
return 2;
83+
}
8484
}
8585

8686
return 0;

Diff for: lib/coek/coek/CMakeLists.txt

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SET(sources
3131
model/writer_lp.cpp
3232
model/writer_nl.cpp
3333
model/reader_jpof.cpp
34+
solvers/solver_results.cpp
3435
solvers/solver.cpp
3536
solvers/solver_repn.cpp
3637
solvers/testsolver.cpp
@@ -66,6 +67,14 @@ if(with_compact)
6667
)
6768
endif()
6869

70+
# Dynamic linking options
71+
list(APPEND sources util/loadlib.cpp)
72+
if(WIN32)
73+
else()
74+
list(APPEND coek_compile_options -DUSING_DL)
75+
list(APPEND coek_link_libraries dl)
76+
endif()
77+
6978
# fmtlib LIBRARY
7079
if(with_fmtlib)
7180
find_package(fmt REQUIRED)
@@ -156,12 +165,6 @@ endif()
156165
#
157166
# IPOPT LIBRARY
158167
#
159-
if(WIN32)
160-
else()
161-
list(APPEND coek_compile_options -DUSING_DL)
162-
list(APPEND coek_link_libraries dl)
163-
endif()
164-
list(APPEND sources solvers/loadlib.cpp)
165168
list(APPEND sources
166169
solvers/ipopt/ipopt_capi.cpp
167170
solvers/ipopt/ipopt_solver.cpp)
@@ -270,6 +273,7 @@ install(FILES
270273
)
271274
install(FILES
272275
solvers/solver.hpp
276+
solvers/solver_results.hpp
273277
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/coek/solvers
274278
)
275279

Diff for: lib/coek/coek/abstract/expr_rule.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class RangeSet : public IndexSet {
9999
bool next()
100100
{
101101
i++;
102-
if (i == N) return false;
102+
if (i == N)
103+
return false;
103104
index[0]->set_value(i);
104105
return true;
105106
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Constraint& Constraint::operator=(const Constraint& expr)
2929
return *this;
3030
}
3131

32-
unsigned int Constraint::id() const { return repn->index; }
32+
size_t Constraint::id() const { return repn->index; }
3333

3434
bool Constraint::is_inequality() const { return repn->is_inequality(); }
3535

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Constraint {
6060
Constraint expand();
6161

6262
/** \returns the unique integer constraint ID */
63-
unsigned int id() const;
63+
size_t id() const;
6464

6565
/** \returns a list representation of the constraint */
6666
std::list<std::string> to_list() const;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ void Model::add_constraint(ConstraintMap& cons)
1919
cons.generate_names();
2020
else if (repn->name_generation_policy == Model::NameGeneration::lazy)
2121
repn->constraint_maps.push_back(cons);
22-
for (auto& con : cons.repn->value) add_constraint(con.second);
22+
for (auto& con : cons.repn->value)
23+
add_constraint(con.second);
2324
}
2425

2526
#ifdef COEK_WITH_COMPACT_MODEL
2627
void CompactModel::add_constraint(ConstraintMap& cons)
2728
{
2829
// TODO - name management here
29-
for (auto& con : cons.repn->value) add_constraint(con.second);
30+
for (auto& con : cons.repn->value)
31+
add_constraint(con.second);
3032
}
3133
#endif
3234

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Variable& Variable::name(const std::string& name)
240240

241241
std::string Variable::name() const { return repn->get_name(); }
242242

243-
unsigned int Variable::id() const { return repn->index; }
243+
size_t Variable::id() const { return repn->index; }
244244

245245
Variable& Variable::within(VariableTypes vtype)
246246
{
@@ -343,7 +343,8 @@ Expression Expression::diff(const Variable& var) const
343343
std::map<std::shared_ptr<VariableTerm>, expr_pointer_t> ans;
344344
symbolic_diff_all(repn, ans);
345345
Expression e;
346-
if (ans.find(var.repn) != ans.end()) e = ans[var.repn];
346+
if (ans.find(var.repn) != ans.end())
347+
e = ans[var.repn];
347348
return e;
348349
}
349350

@@ -1012,14 +1013,16 @@ Expression affine_expression(const std::vector<double>& coef, const std::vector<
10121013
Expression e(offset);
10131014
auto cit = coef.begin();
10141015
auto vit = var.begin();
1015-
for (; vit != var.end(); ++vit, ++cit) e += (*cit) * (*vit);
1016+
for (; vit != var.end(); ++vit, ++cit)
1017+
e += (*cit) * (*vit);
10161018
return e;
10171019
}
10181020

10191021
Expression affine_expression(const std::vector<Variable>& var, double offset)
10201022
{
10211023
Expression e(offset);
1022-
for (auto& v : var) e += v;
1024+
for (auto& v : var)
1025+
e += v;
10231026
return e;
10241027
}
10251028

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class Variable {
261261
bool is_integer() const;
262262

263263
/** \returns the unique integer variable ID */
264-
unsigned int id() const;
264+
size_t id() const;
265265

266266
/** \returns \c false because this is not a constant expression */
267267
bool is_constant() const { return false; }

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

+22-11
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ class IndexedComponentRepn_multiarray : public IndexedComponentRepn<TYPE> {
1717
: IndexedComponentRepn<TYPE>(_shape.size()), shape(_shape)
1818
{
1919
size_t _size = 1;
20-
for (auto n : shape) _size *= n;
20+
for (auto n : shape)
21+
_size *= n;
2122
this->cache.resize((_size + 1) * (_shape.size() + 1));
2223
}
2324

2425
IndexedComponentRepn_multiarray(const std::initializer_list<size_t>& _shape)
2526
: IndexedComponentRepn<TYPE>(_shape.size()), shape(_shape)
2627
{
2728
size_t _size = 1;
28-
for (auto n : shape) _size *= n;
29+
for (auto n : shape)
30+
_size *= n;
2931
this->cache.resize((_size + 1) * (_shape.size() + 1));
3032
}
3133

3234
bool valid_index(const IndexVector& args)
3335
{
3436
for (size_t i = 0; i < this->_dim; ++i) {
3537
size_t tmp = static_cast<size_t>(args[i]);
36-
if ((tmp < 0) or (tmp >= shape[i])) return false;
38+
if ((tmp < 0) or (tmp >= shape[i]))
39+
return false;
3740
}
3841
return true;
3942
}
@@ -48,13 +51,15 @@ void IndexedComponentRepn_multiarray<TYPE>::generate_names()
4851
// then we do not try to generate names. The default/simple
4952
// names will be used.
5053
auto name = this->_name;
51-
if (name.size() == 0) return;
54+
if (name.size() == 0)
55+
return;
5256

5357
for (auto& component : this->value) {
5458
name += "[";
5559

5660
auto shape_size = shape.size();
57-
if (shape_size >= 1) name += std::to_string(component.first[0]);
61+
if (shape_size >= 1)
62+
name += std::to_string(component.first[0]);
5863

5964
if (shape_size > 1) {
6065
for (size_t i = 1; i < shape_size; ++i) {
@@ -89,7 +94,8 @@ class IndexedComponentRepn_setindex : public IndexedComponentRepn<TYPE> {
8994
if (tmp.size() == 1)
9095
return this->concrete_set.contains(args[0]);
9196
else {
92-
for (size_t i = 0; i < tmp.size(); i++) tmp[i] = args[i];
97+
for (size_t i = 0; i < tmp.size(); i++)
98+
tmp[i] = args[i];
9399
return this->concrete_set.contains(tmp);
94100
}
95101
}
@@ -104,13 +110,15 @@ void IndexedComponentRepn_setindex<TYPE>::generate_names()
104110
// then we do not try to generate names. The default/simple
105111
// names will be used.
106112
auto name = this->_name;
107-
if (name == "") return;
113+
if (name == "")
114+
return;
108115

109116
size_t _dim = this->dim();
110117
std::vector<int> x_data(_dim);
111118
IndexVector x(&(x_data[0]), _dim);
112119
for (auto& indices : concrete_set) {
113-
for (size_t j = 0; j < _dim; j++) x[j] = indices[j];
120+
for (size_t j = 0; j < _dim; j++)
121+
x[j] = indices[j];
114122
if (indices.size() == 1) {
115123
auto tmp = indices[0];
116124
this->value[x].name(name + "[" + std::to_string(tmp) + "]");
@@ -132,15 +140,17 @@ void IndexedComponentRepn_setindex<TYPE>::generate_names()
132140
template <class TYPE>
133141
TYPE& IndexedComponent_Map<TYPE>::operator()(int i)
134142
{
135-
if (this->dim() != 1) index_error(1);
143+
if (this->dim() != 1)
144+
index_error(1);
136145
this->tmp[0] = i;
137146
return index(this->tmp);
138147
}
139148

140149
template <class TYPE>
141150
TYPE& IndexedComponent_Map<TYPE>::operator()(int i, int j)
142151
{
143-
if (this->dim() != 2) index_error(2);
152+
if (this->dim() != 2)
153+
index_error(2);
144154
this->tmp[0] = i;
145155
this->tmp[1] = j;
146156
return index(this->tmp);
@@ -184,7 +194,8 @@ TYPE& IndexedComponent_Map<TYPE>::index(const IndexVector& args)
184194
if (!(this->repn->valid_index(this->tmp))) {
185195
std::string err = "Unexpected index value: " + this->repn->_name + "(";
186196
for (size_t i = 0; i < args.size(); i++) {
187-
if (i > 0) err += ",";
197+
if (i > 0)
198+
err += ",";
188199
err += std::to_string(args[i]);
189200
}
190201
err += ")";

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ class IndexedComponent_Map : public IndexedComponent<TYPE> {
110110
TYPE& operator()(const ARGTYPES&... args)
111111
{
112112
const size_t nargs = count_args(args...);
113-
if (this->dim() != nargs) index_error(nargs);
113+
if (this->dim() != nargs)
114+
index_error(nargs);
114115
collect_args(static_cast<size_t>(0), args...);
115116
return index(this->tmp);
116117
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Objective& Objective::operator=(const Objective& expr)
1919
return *this;
2020
}
2121

22-
unsigned int Objective::id() const { return repn->index; }
22+
size_t Objective::id() const { return repn->index; }
2323

2424
double Objective::value() const { return repn->eval(); }
2525

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Objective {
4545
std::string name() const;
4646

4747
/** \returns the unique integer objective ID */
48-
unsigned int id() const;
48+
size_t id() const;
4949

5050
/** \returns a list representation of the objective */
5151
std::list<std::string> to_list() const;

0 commit comments

Comments
 (0)