Skip to content

Commit

Permalink
Merge pull request #71 from sandialabs/highs
Browse files Browse the repository at this point in the history
Preliminary interface to the HIGHS solver
  • Loading branch information
whart222 authored May 6, 2024
2 parents b2ec377 + 6fae3ba commit 8de953e
Show file tree
Hide file tree
Showing 24 changed files with 954 additions and 68 deletions.
4 changes: 2 additions & 2 deletions build_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ else
. ${SPACK_HOME}/share/spack/setup-env.sh
spack env create coekenv
spack env activate coekenv
spack add asl cppad fmt rapidjson catch2
spack add asl cppad fmt rapidjson catch2 highs
spack install
spack env deactivate
fi
Expand All @@ -67,5 +67,5 @@ echo ""
\rm -Rf build
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=${SPACK_HOME}/var/spack/environments/coekenv/.spack-env/view -Dwith_python=${with_python} -Dwith_gurobi=$with_gurobi -Dwith_cppad=ON -Dwith_fmtlib=ON -Dwith_rapidjson=ON -Dwith_catch2=ON -Dwith_tests=ON -Dwith_asl=ON -Dwith_openmp=OFF ..
cmake -DCMAKE_PREFIX_PATH=${SPACK_HOME}/var/spack/environments/coekenv/.spack-env/view -Dwith_python=${with_python} -Dwith_gurobi=$with_gurobi -Dwith_highs=ON -Dwith_cppad=ON -Dwith_fmtlib=ON -Dwith_rapidjson=ON -Dwith_catch2=ON -Dwith_tests=ON -Dwith_asl=ON -Dwith_openmp=OFF ..
make -j20
10 changes: 8 additions & 2 deletions lib/coek/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ option(with_asl "Use the ASL autograd library" OFF)
# Solver Dependencies
option(with_gurobi "Use the Gurobi solver" OFF)
set(GUROBI_HOME "" CACHE FILEPATH "Set the path to gurobi")
option(with_highs "Use the Highs solver" OFF)


##################### Other Config #####################
Expand All @@ -42,8 +43,13 @@ endif()

MESSAGE("-- With Gurobi Solver: ${with_gurobi}")
if(with_gurobi)
find_package(GUROBI)
list(APPEND solver_flags "-DWITH_GUROBI")
find_package(GUROBI REQUIRED)
endif()

MESSAGE("-- With Highs Solver: ${with_highs}")
if(with_highs)
find_package(HIGHS REQUIRED)
find_package(Threads REQUIRED)
endif()


Expand Down
18 changes: 11 additions & 7 deletions lib/coek/coek/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SET(sources
solvers/solver_results.cpp
solvers/solver.cpp
solvers/solver_repn.cpp
solvers/create_solver.cpp
solvers/testsolver.cpp
autograd/autograd.cpp
abstract/expr_rule.cpp
Expand Down Expand Up @@ -154,16 +155,19 @@ endif()
if(with_gurobi)
list(APPEND sources
solvers/gurobi/gurobi.cpp)
#if(NOT "$ENV{GUROBI_HOME}" STREQUAL "")
# MESSAGE("-- GUROBI_HOME $ENV{GUROBI_HOME}")
# list(APPEND coek_link_directories $ENV{GUROBI_HOME}/lib)
#else()
# MESSAGE("-- GUROBI_HOME Not provided")
#endif()
list(APPEND coek_compile_options -DWITH_GUROBI)
list(APPEND coek_link_libraries ${GUROBI_CXX_LIBRARY} ${GUROBI_LIBRARY})
list(APPEND coek_include_directories ${GUROBI_INCLUDE_DIRS})
#gurobi_g++5.2 gurobi90)
endif()

#
# HIGHS LIBRARY
#
if(with_highs)
list(APPEND sources
solvers/highs/highs.cpp)
list(APPEND coek_compile_options -DWITH_HIGHS)
list(APPEND coek_link_libraries highs::highs)
endif()

#
Expand Down
12 changes: 12 additions & 0 deletions lib/coek/coek/api/expression_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

namespace coek {

class ParameterTerm;
class IndexParameterTerm;
class VariableTerm;
class BaseExpressionTerm;
class SubExpressionTerm;

typedef std::shared_ptr<ParameterTerm> ParameterRepn;
typedef std::shared_ptr<IndexParameterTerm> IndexParameterRepn;
typedef std::shared_ptr<VariableTerm> VariableRepn;
typedef std::shared_ptr<BaseExpressionTerm> ExpressionRepn;
typedef std::shared_ptr<SubExpressionTerm> SubExpressionRepn;

class VariableTerm;
class Expression;
class Objective;
Expand Down
29 changes: 22 additions & 7 deletions lib/coek/coek/autograd/asl_repn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ void ASL_Repn::call_hesset()
nnz_lag_h = static_cast<size_t>(sphsetup(-1, coeff_obj, mult_supplied, uptri));
}

bool ASL_Repn::get_option(const std::string& option, std::string& value) const
bool ASL_Repn::get_option(const std::string& option, bool& value) const
{
if (option == "temp_directory") {
value = temp_directory;
if (option == "remove_nl_file") {
value = remove_nl_file;
return true;
}
return false;
Expand All @@ -485,16 +485,31 @@ bool ASL_Repn::get_option(const std::string& option, int& value) const
return false;
}

void ASL_Repn::set_option(const std::string& option, const std::string value)
bool ASL_Repn::get_option(const std::string& option, std::string& value) const
{
if (option == "temp_directory")
temp_directory = value;
if (option == "temp_directory") {
value = temp_directory;
return true;
}
return false;
}

void ASL_Repn::set_option(const std::string& option, int value)
void ASL_Repn::set_option(const std::string& option, bool value)
{
if (option == "remove_nl_file")
remove_nl_file = value;
}

void ASL_Repn::set_option(const std::string& option, int value)
{
if (option == "remove_nl_file")
remove_nl_file = (value == 1);
}

void ASL_Repn::set_option(const std::string& option, const std::string value)
{
if (option == "temp_directory")
temp_directory = value;
}

} // namespace coek
10 changes: 6 additions & 4 deletions lib/coek/coek/autograd/asl_repn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ASL_Repn : public NLPModelRepn {
ASL_pfgh* asl_;
// The temporary file directory used to write the NL file
std::string temp_directory;
// If set to 1, then the NL file is removed
int remove_nl_file = 1;
// If true, then the NL file is removed
bool remove_nl_file = true;

// ----------------------------------------------------------------------
// Problem information
Expand Down Expand Up @@ -103,10 +103,12 @@ class ASL_Repn : public NLPModelRepn {
void compute_J(std::vector<double>& J);

public:
bool get_option(const std::string& option, std::string& value) const;
bool get_option(const std::string& option, bool& value) const;
bool get_option(const std::string& option, int& value) const;
void set_option(const std::string& option, const std::string value);
bool get_option(const std::string& option, std::string& value) const;
void set_option(const std::string& option, bool value);
void set_option(const std::string& option, int value);
void set_option(const std::string& option, const std::string value);

protected:
void* nerror_;
Expand Down
29 changes: 28 additions & 1 deletion lib/coek/coek/autograd/autograd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ class NLPModelRepn {
void find_used_variables();

public:
/** Get the value of an boolean option
*
* The option value is returned by reference if it has
* a value.
*
* \param option the option name
* \param value a boolean value that is passed by reference
*
* \returns \c true if the option is found
*/
virtual bool get_option(const std::string& /*option*/, bool& /*value*/) const { return false; }
/** Get the value of an integer option
*
* The option value is returned by reference if it has
Expand Down Expand Up @@ -106,6 +117,12 @@ class NLPModelRepn {
return false;
}

/** Set a boolean option
*
* \param option the option name
* \param value the boolean value
*/
virtual void set_option(const std::string& /*option*/, bool /*value*/) {}
/** Set an integer option
*
* \param option the option name
Expand All @@ -123,7 +140,17 @@ class NLPModelRepn {
* \param option the option name
* \param value the string value
*/
virtual void set_option(const std::string& /*option*/, const std::string /*value*/) {}
virtual void set_option(const std::string& /*option*/, const std::string& /*value*/) {}

/** Set a string option
*
* \param option the option name
* \param value the string value
*/
void set_option(const std::string& option, const char* value)
{
this->set_option(option, std::string(value));
}
};

NLPModelRepn* create_NLPModelRepn(Model& model, const std::string& name);
Expand Down
21 changes: 21 additions & 0 deletions lib/coek/coek/autograd/cppad_repn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ void CppAD_Repn::reset(void)
set_variables(currx);
}

bool CppAD_Repn::get_option(const std::string& option, bool& value) const
{
if (option == "sparse_JH") {
value = sparse_JH;
return true;
}
else if (option == "simplify_expressions") {
value = simplify_expressions;
return true;
}
return false;
}

bool CppAD_Repn::get_option(const std::string& option, int& value) const
{
if (option == "sparse_JH") {
Expand All @@ -623,6 +636,14 @@ bool CppAD_Repn::get_option(const std::string& option, int& value) const
return false;
}

void CppAD_Repn::set_option(const std::string& option, bool value)
{
if (option == "sparse_JH")
sparse_JH = value;
else if (option == "simplify_expressions")
simplify_expressions = value;
}

void CppAD_Repn::set_option(const std::string& option, int value)
{
if (option == "sparse_JH")
Expand Down
2 changes: 2 additions & 0 deletions lib/coek/coek/autograd/cppad_repn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ class CppAD_Repn : public NLPModelRepn {
void compute_J(std::vector<double>& J);

public:
bool get_option(const std::string& option, bool& value) const;
bool get_option(const std::string& option, int& value) const;

void set_option(const std::string& option, bool value);
void set_option(const std::string& option, int value);

public:
Expand Down
2 changes: 2 additions & 0 deletions lib/coek/coek/model/nlp_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void NLPModel::initialize(Model& model, std::string type)
std::shared_ptr<NLPModelRepn> tmp(create_NLPModelRepn(model, type));
repn = tmp;

for (const auto& ioption : boolean_options())
repn->set_option(ioption.first, ioption.second);
for (const auto& ioption : integer_options())
repn->set_option(ioption.first, ioption.second);
for (const auto& doption : double_options())
Expand Down
49 changes: 49 additions & 0 deletions lib/coek/coek/solvers/create_solver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "coek/solvers/solver_repn.hpp"

namespace coek {

SolverRepn* create_coektest_solver();
NLPSolverRepn* create_ipopt_solver();
#ifdef WITH_GUROBI
SolverRepn* create_gurobi_solver();
#endif
#ifdef WITH_HIGHS
SolverRepn* create_highs_solver();
#endif

SolverRepn* create_solver(std::string& name, OptionCache& options)
{
if (name == "test")
return create_coektest_solver();

#ifdef WITH_GUROBI
if (name == "gurobi") {
auto tmp = create_gurobi_solver();
tmp->set_options(options);
return tmp;
}
#endif

#ifdef WITH_HIGHS
if (name == "highs") {
auto tmp = create_highs_solver();
tmp->set_options(options);
return tmp;
}
#endif

return 0;
}

NLPSolverRepn* create_nlpsolver(std::string& name, OptionCache& options)
{
if (name == "ipopt") {
auto tmp = create_ipopt_solver();
tmp->set_options(options);
return tmp;
}

return 0;
}

} // namespace coek
Loading

0 comments on commit 8de953e

Please sign in to comment.