Skip to content

Commit

Permalink
Merge pull request #2 from sandialabs/dev
Browse files Browse the repository at this point in the history
Merging the dev branch
  • Loading branch information
whart222 authored Sep 20, 2022
2 parents 344a0c7 + 0fa5445 commit daf64a1
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 102 deletions.
26 changes: 24 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Linux Build and Tests
on:
workflow_dispatch:
push:
branches: [ main ]
branches: [ main, dev ]
pull_request:
branches: [ main ]
branches: [ main, dev ]

jobs:

Expand Down Expand Up @@ -43,3 +43,25 @@ jobs:
run: |
cd build
make test
pycoek_pybind11:
runs-on: ubuntu-latest
needs: coek-build-and-test-with-tpls
steps:
- uses: actions/checkout@v2
- name: run cmake
run: |
mkdir build
cd build
cmake -Dbuild_all=ON ../lib/coek
- name: build
run: |
cd build
make tpls
make
- name: build pycoek_pybind11
run: |
cd build
cmake -Dwith_pybind11=ON ../lib/coek
make
8 changes: 8 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ChangeLog

Here we list changes of Coek. More detailed information about incremental changes can be found in the
[commit history](https://github.com/sandialabs/coek/commits).

## 1.0

Initial public release.
8 changes: 4 additions & 4 deletions lib/coek/coek/model/compact_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ VariableMap& CompactModel::add_variable(VariableMap& vars)
{
repn->variables.insert(repn->variables.end(),
vars.begin(), vars.end());
repn->variable_names.insert(repn->variable_names.end(), varray.variables.size(), "");
repn->variable_names.insert(repn->variable_names.end(), vars.size(), "");
/*
auto end = vars.end();
for (auto it=vars.begin(); it != end; ++it) {
Expand All @@ -88,7 +88,7 @@ VariableMap& CompactModel::add_variable(VariableMap&& vars)
{
repn->variables.insert(repn->variables.end(),
vars.begin(), vars.end());
repn->variable_names.insert(repn->variable_names.end(), varray.variables.size(), "");
repn->variable_names.insert(repn->variable_names.end(), vars.size(), "");
/*
auto end = vars.end();
for (auto it=vars.begin(); it != end; ++it) {
Expand All @@ -103,7 +103,7 @@ VariableArray& CompactModel::add_variable(VariableArray& vars)
{
repn->variables.insert(repn->variables.end(),
vars.begin(), vars.end());
repn->variable_names.insert(repn->variable_names.end(), varray.variables.size(), "");
repn->variable_names.insert(repn->variable_names.end(), vars.size(), "");
/*
auto end = vars.end();
for (auto it=vars.begin(); it != end; ++it) {
Expand All @@ -118,7 +118,7 @@ VariableArray& CompactModel::add_variable(VariableArray&& vars)
{
repn->variables.insert(repn->variables.end(),
vars.begin(), vars.end());
repn->variable_names.insert(repn->variable_names.end(), varray.variables.size(), "");
repn->variable_names.insert(repn->variable_names.end(), vars.size(), "");
/*
auto end = vars.end();
for (auto it=vars.begin(); it != end; ++it) {
Expand Down
124 changes: 57 additions & 67 deletions lib/coek/coek/solvers/gurobi/gurobi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,31 @@ gmodel = new GRBModel(*env);
assert(_model->objectives.size() == 1);

// Add Gurobi variables
for (std::vector<coek::Variable>::iterator it=_model->variables.begin(); it != _model->variables.end(); ++it) {
coek::VariableTerm* v = it->repn;
if (v->fixed) {
;
}
else {
double lb = v->lb->eval();
double ub = v->ub->eval();
if (v->binary)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_BINARY);
else if (v->integer)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_INTEGER);
else {
if (ub >= 1e19) {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, GRB_INFINITY, 0, GRB_CONTINUOUS);
}
else {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, ub, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_CONTINUOUS);
for (auto& var : _model->variables) {
coek::VariableTerm* v = var->repn;
if (not v->fixed) {
double lb = v->lb->eval();
double ub = v->ub->eval();
if (v->binary)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_BINARY);
else if (v->integer)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_INTEGER);
else {
if (ub >= 1e19) {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, GRB_INFINITY, 0, GRB_CONTINUOUS);
}
else {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, ub, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_CONTINUOUS);
}
}
}
}
}

gmodel->update();

Expand Down Expand Up @@ -175,8 +172,6 @@ if (nobj > 1) {
try {
coek::QuadraticExpr repn;
for (auto& con : model.repn->constraints) {
//crepn[i].collect_terms(_model->constraints[i]);
//Constraint c = cval->expand();
add_gurobi_constraint(gmodel, con, x, repn);
}
}
Expand All @@ -200,11 +195,11 @@ try {
// TODO: Is there a string description of the solver status?

// Collect values of Gurobi variables
for (std::vector<coek::Variable>::iterator it=_model->variables.begin(); it != _model->variables.end(); ++it) {
for (auto& var : _model->variables) {
coek::VariableTerm* v = it->repn;
if (!(v->fixed)) {
v->set_value( x[v->index].get(GRB_DoubleAttr_X) );
}
if (not v->fixed) {
v->set_value( x[v->index].get(GRB_DoubleAttr_X) );
}
}
}
}
Expand Down Expand Up @@ -236,8 +231,8 @@ assert(model.objectives.size() == 1);
std::cout << "BUILDING GUROBI MODEL" << std::endl << std::flush;

// Add Gurobi variables
for (std::vector<coek::Variable>::iterator it=model.variables.begin(); it != model.variables.end(); ++it) {
coek::VariableTerm* v = it->repn;
for (auto& var : model->variables) {
coek::VariableTerm* v = var->repn;
double lb = v->lb->eval();
double ub = v->ub->eval();
if (v->binary)
Expand Down Expand Up @@ -363,33 +358,30 @@ if (initial_solve()) {
assert(_model->objectives.size() == 1);

// Add Gurobi variables
for (std::vector<coek::Variable>::iterator it=_model->variables.begin(); it != _model->variables.end(); ++it) {
coek::VariableTerm* v = it->repn;
if (v->fixed) {
;
}
else {
double lb = v->lb->eval();
double ub = v->ub->eval();
if (v->binary)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_BINARY);
else if (v->integer)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_INTEGER);
else {
if (ub >= 1e19) {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, GRB_INFINITY, 0, GRB_CONTINUOUS);
}
else {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, ub, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_CONTINUOUS);
}
}
}
for (auto& var : _model->variables) {
coek::VariableTerm* v = var->repn;
if (not v->fixed) {
double lb = v->lb->eval();
double ub = v->ub->eval();
if (v->binary)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_BINARY);
else if (v->integer)
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_INTEGER);
else {
if (ub >= 1e19) {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, GRB_INFINITY, 0, GRB_CONTINUOUS);
}
else {
if (lb <= -1e19)
x[ v->index ] = gmodel->addVar(-GRB_INFINITY, ub, 0, GRB_CONTINUOUS);
else
x[ v->index ] = gmodel->addVar(lb, ub, 0, GRB_CONTINUOUS);
}
}
}
}

gmodel->update();
Expand All @@ -398,9 +390,9 @@ if (initial_solve()) {
int nobj=0;
try {
coek::QuadraticExpr orepn;
for (auto it=model.repn->objectives.begin(); it != model.repn->objectives.end(); ++it) {
Expression tmp = it->expr();
add_gurobi_objective(gmodel, tmp, it->sense(), x, orepn);
for (auto& obj : model.repn->objectives) {
Expression tmp = obj->expr();
add_gurobi_objective(gmodel, tmp, obj->sense(), x, orepn);
nobj++;
}
}
Expand All @@ -419,10 +411,8 @@ if (initial_solve()) {
// Add Gurobi constraints
try {
coek::QuadraticExpr repn;
for (auto it=_model->constraints.begin(); it != _model->constraints.end(); ++it) {
//crepn[i].collect_terms(_model->constraints[i]);
//Constraint c = cval->expand();
add_gurobi_constraint(gmodel, *it, x, repn);
for (auto& con : model->constraints) {
add_gurobi_constraint(gmodel, con, x, repn);
}
}
catch (GRBException e) {
Expand Down
14 changes: 8 additions & 6 deletions lib/coek/coek/solvers/ipopt/ipopt_capi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <memory>
#include <cassert>

#include "coek/util/sequence.hpp"
#include "coek/api/expression.hpp"
#include "coek/api/objective.hpp"
#include "coek/api/constraint.hpp"
Expand Down Expand Up @@ -429,8 +430,8 @@ if (start_from_last_x) {
this);
}
else {
for (size_t i=0; i<model.num_variables(); i++) {
auto v = model.get_variable(i);
for (size_t i : coek::range(model.num_variables())) {
const auto& v = model.get_variable(i);
last_x[i] = v.value();
}
status = (*IpoptSolve_func_ptr)(
Expand All @@ -445,11 +446,12 @@ else {
}

if (status == Solve_Succeeded) {
//std::cout << std::endl << std::endl << "*** The problem solved in " << last_iter_count << " iterations!" << std::endl;
#ifdef DEBUG
std::cout << std::endl << std::endl << "*** The problem solved in " << last_iter_count << " iterations!" << std::endl;
std::cout << std::endl << std::endl << "*** The final value of the objective function is " << last_objval << '.' << std::endl;
#endif

//std::cout << std::endl << std::endl << "*** The final value of the objective function is " << last_objval << '.' << std::endl;

for (size_t i=0; i<model.num_variables(); i++) {
for (size_t i : coek::range(model.num_variables())) {
auto v = model.get_variable(i);
v.value( last_x[i] );
}
Expand Down
6 changes: 5 additions & 1 deletion lib/coek/coek/solvers/ipopt/ipopt_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ return repn->perform_solve();

int IpoptSolver::resolve()
{
#ifdef DEBUG
auto start = std::chrono::high_resolution_clock::now();
#endif

if (not initial_solve())
model->reset();
Expand All @@ -36,9 +38,11 @@ else
repn->set_start_from_last_x(false);
int status = repn->perform_solve();

#ifdef DEBUG
auto curr = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = curr-start;
//std::cout << "Time to solve: " << diff.count() << " s\n";
std::cout << "Time to solve: " << diff.count() << " s\n";
#endif

return status;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/coek/examples/invquad_array_resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ void invquad_array_resolve()
coek::Model m;

size_t N=5;
std::vector<coek::Parameter> p(N);
auto p = coek::parameter("p", N);
for (auto& param : p)
param.value(0.5);

// Initialize variables and add them to the model
auto x = coek::variable("x", p.size())
auto x = coek::variable("x", N)
.bounds(-10,10).value(0.0);
m.add(x);

// Create objective and add it to the model
auto e = coek::expression();
for (size_t i : coek::range(p.size()))
e -= (x(i)-p[i])*(x(i)-p[i]);
for (size_t i : coek::range(N))
e -= (x(i)-p(i))*(x(i)-p(i));
m.add_objective( e );

// Optimize the model
Expand All @@ -30,13 +30,13 @@ solver.load(nlp);

solver.resolve();
// x^*_i = -10
for (size_t i=0; i<p.size(); i++)
for (size_t i : coek::range(N))
std::cout << "Value of " << x(i).name() << ": " << x(i).value() << std::endl;

for (auto& param : p)
param.value(-0.5);
solver.resolve();
// x^*_i = -10
for (size_t i=0; i<p.size(); i++)
for (size_t i : coek::range(N))
std::cout << "Value of " << x(i).name() << ": " << x(i).value() << std::endl;
}
8 changes: 4 additions & 4 deletions lib/coek/examples/invquad_array_solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void invquad_array_solve()
coek::Model m;

size_t N=10;
std::vector<coek::Parameter> p(10);
auto p = coek::parameter("p", N);
for (auto& param : p)
param.value(0.5);

Expand All @@ -18,8 +18,8 @@ m.add(x);

// Create objective and add it to the model
auto e = coek::expression();
for (size_t i : coek::range(p.size()))
e -= (x(i)-p[i])*(x(i)-p[i]);
for (size_t i : coek::range(N))
e -= (x(i)-p(i))*(x(i)-p(i));
m.add_objective( e );

// Optimize the model
Expand All @@ -29,6 +29,6 @@ solver.set_option("print_level", 0);
solver.solve(nlp);

// x^*_i = -10
for (size_t i=0; i<N; i++)
for (size_t i : coek::range(N))
std::cout << "Value of " << x(i).name() << ": " << x(i).value() << std::endl;
}
Loading

0 comments on commit daf64a1

Please sign in to comment.