Skip to content

Commit

Permalink
Several changes
Browse files Browse the repository at this point in the history
1. Adding missing data() functions

2. Fixed walker errors with DataRefTerms

3. Added tests for data() logic
  • Loading branch information
whart222 committed Oct 6, 2024
1 parent 2e7426c commit 559e4d1
Show file tree
Hide file tree
Showing 16 changed files with 1,014 additions and 19 deletions.
4 changes: 4 additions & 0 deletions lib/coek/coek/api/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Parameter parameter() { return Parameter(); }

Parameter parameter(const std::string& name) { return Parameter(name); }

Parameter data() { return Parameter(); }

Parameter data(const std::string& name) { return Parameter(name); }

//
// IndexParameter
//
Expand Down
2 changes: 2 additions & 0 deletions lib/coek/coek/api/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class Parameter {

Parameter parameter();
Parameter parameter(const std::string& name);
Parameter data();
Parameter data(const std::string& name);

// Index Parameter
class IndexParameter {
Expand Down
12 changes: 10 additions & 2 deletions lib/coek/coek/ast/visitor_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,24 @@ double visit_VariableTerm(const expr_pointer_t& expr, VariableData& data)
}

#ifdef COEK_WITH_COMPACT_MODEL
double visit_VariableRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
{
throw std::runtime_error(
"Cannot evaluate an expression that contains a VariableRefTerm. This is an abstract "
"expression!");
}

double visit_ParameterRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
{
throw std::runtime_error(
"Cannot evaluate an expression that contains a ParameterRefTerm. This is an abstract "
"expression!");
}

double visit_VariableRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
double visit_DataRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
{
throw std::runtime_error(
"Cannot evaluate an expression that contains a VariableRefTerm. This is an abstract "
"Cannot evaluate an expression that contains a DataRefTerm. This is an abstract "
"expression!");
}
#endif
Expand Down Expand Up @@ -205,6 +212,7 @@ double visit_expression(const expr_pointer_t& expr, VariableData& data)
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
5 changes: 4 additions & 1 deletion lib/coek/coek/ast/visitor_mutable_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ void visit_VariableTerm(const expr_pointer_t& expr, MutableValuesData& data)
}

#ifdef COEK_WITH_COMPACT_MODEL
void visit_VariableRefTerm(const expr_pointer_t& /*expr*/, MutableValuesData& /*data*/) {}

void visit_ParameterRefTerm(const expr_pointer_t& /*expr*/, MutableValuesData& /*data*/) {}

void visit_VariableRefTerm(const expr_pointer_t& /*expr*/, MutableValuesData& /*data*/) {}
void visit_DataRefTerm(const expr_pointer_t& /*expr*/, MutableValuesData& /*data*/) {}
#endif

void visit_MonomialTerm(const expr_pointer_t& expr, MutableValuesData& data)
Expand Down Expand Up @@ -183,6 +185,7 @@ void visit_expression(const expr_pointer_t& expr, MutableValuesData& data)
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
9 changes: 8 additions & 1 deletion lib/coek/coek/ast/visitor_simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ void visit_VariableTerm(const expr_pointer_t& expr, VisitorData& data)
}

#ifdef COEK_WITH_COMPACT_MODEL
void visit_VariableRefTerm(const expr_pointer_t& expr, VisitorData& data)
{
data.last_expr = expr;
data.is_value = false;
}

void visit_ParameterRefTerm(const expr_pointer_t& expr, VisitorData& data)
{
data.last_expr = expr;
data.is_value = false;
}

void visit_VariableRefTerm(const expr_pointer_t& expr, VisitorData& data)
void visit_DataRefTerm(const expr_pointer_t& expr, VisitorData& data)
{
data.last_expr = expr;
data.is_value = false;
Expand Down Expand Up @@ -416,6 +422,7 @@ void visit_expression(const expr_pointer_t& expr, VisitorData& data)
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
16 changes: 11 additions & 5 deletions lib/coek/coek/ast/visitor_symdiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ void visit_VariableTerm(const expr_pointer_t& /*expr*/, PartialData& data)
}

#ifdef COEK_WITH_COMPACT_MODEL
void visit_ParameterRefTerm(const expr_pointer_t& /*expr*/, PartialData& data)
{
data.partial = ZEROCONST;
}

//
// We assume for now that a user cannot differentiate with respect to an referenced
// variable. It's not clear that we can infer whether two referenced variables are the same,
Expand All @@ -72,6 +67,16 @@ void visit_VariableRefTerm(const expr_pointer_t& /*expr*/, PartialData& data)
{
data.partial = ZEROCONST;
}

void visit_ParameterRefTerm(const expr_pointer_t& /*expr*/, PartialData& data)
{
data.partial = ZEROCONST;
}

void visit_DataRefTerm(const expr_pointer_t& /*expr*/, PartialData& data)
{
data.partial = ZEROCONST;
}
#endif

void visit_MonomialTerm(const expr_pointer_t& expr, PartialData& data)
Expand Down Expand Up @@ -278,6 +283,7 @@ expr_pointer_t compute_partial(const expr_pointer_t& expr, size_t i, PartialData
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
14 changes: 14 additions & 0 deletions lib/coek/coek/ast/visitor_to_MutableNLPExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ void visit(std::shared_ptr<VariableRefTerm>& /*expr*/, MutableNLPExpr& /*repn*/,
{
throw std::runtime_error("Unexpected variable reference.");
}

void visit(std::shared_ptr<ParameterRefTerm>& /*expr*/, MutableNLPExpr& /*repn*/,
double /*multiplier*/)
{
throw std::runtime_error("Unexpected parameter reference.");
}

void visit(std::shared_ptr<DataRefTerm>& /*expr*/, MutableNLPExpr& /*repn*/,
double /*multiplier*/)
{
throw std::runtime_error("Unexpected data reference.");
}
#endif

void visit(std::shared_ptr<MonomialTerm>& expr, MutableNLPExpr& repn, double multiplier)
Expand Down Expand Up @@ -438,6 +450,8 @@ void visit_expression(const expr_pointer_t& expr, MutableNLPExpr& repn, double m
VISIT_CASE(VariableTerm);
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
14 changes: 14 additions & 0 deletions lib/coek/coek/ast/visitor_to_QuadraticExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ void visit(std::shared_ptr<VariableRefTerm>& /*expr*/, QuadraticExpr& /*repn*/,
{
throw std::runtime_error("Unexpected variable reference.");
}

void visit(std::shared_ptr<ParameterRefTerm>& /*expr*/, QuadraticExpr& /*repn*/,
double /*multiplier*/)
{
throw std::runtime_error("Unexpected parameter reference.");
}

void visit(std::shared_ptr<DataRefTerm>& /*expr*/, QuadraticExpr& /*repn*/,
double /*multiplier*/)
{
throw std::runtime_error("Unexpected data reference.");
}
#endif

void visit(std::shared_ptr<MonomialTerm>& expr, QuadraticExpr& repn, double multiplier)
Expand Down Expand Up @@ -369,6 +381,8 @@ void visit_expression(const expr_pointer_t& expr, QuadraticExpr& repn, double mu
VISIT_CASE(VariableTerm);
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
13 changes: 11 additions & 2 deletions lib/coek/coek/ast/visitor_to_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ void visit_VariableTerm(const expr_pointer_t& expr, std::list<std::string>& repr
}

#ifdef COEK_WITH_COMPACT_MODEL
void visit_VariableRefTerm(const expr_pointer_t& expr, std::list<std::string>& repr)
{
auto tmp = safe_pointer_cast<VariableRefTerm>(expr);
std::stringstream sstr;
write_expr(tmp, sstr);
repr.push_back(sstr.str());
}

void visit_ParameterRefTerm(const expr_pointer_t& expr, std::list<std::string>& repr)
{
auto tmp = safe_pointer_cast<ParameterRefTerm>(expr);
Expand All @@ -56,9 +64,9 @@ void visit_ParameterRefTerm(const expr_pointer_t& expr, std::list<std::string>&
repr.push_back(sstr.str());
}

void visit_VariableRefTerm(const expr_pointer_t& expr, std::list<std::string>& repr)
void visit_DataRefTerm(const expr_pointer_t& expr, std::list<std::string>& repr)
{
auto tmp = safe_pointer_cast<VariableRefTerm>(expr);
auto tmp = safe_pointer_cast<DataRefTerm>(expr);
std::stringstream sstr;
write_expr(tmp, sstr);
repr.push_back(sstr.str());
Expand Down Expand Up @@ -276,6 +284,7 @@ void visit_expression(const expr_pointer_t& expr, std::list<std::string>& repr)
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
8 changes: 7 additions & 1 deletion lib/coek/coek/ast/visitor_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,17 @@ void visit_VariableTerm(const expr_pointer_t& expr, VariableData& data)
}

#ifdef COEK_WITH_COMPACT_MODEL
void visit_VariableRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
{
throw std::runtime_error("Attempting to find variables in an abstract expression!");
}

void visit_ParameterRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
{
throw std::runtime_error("Attempting to find variables in an abstract expression!");
}

void visit_VariableRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
void visit_DataRefTerm(const expr_pointer_t& /*expr*/, VariableData& /*data*/)
{
throw std::runtime_error("Attempting to find variables in an abstract expression!");
}
Expand Down Expand Up @@ -195,6 +200,7 @@ void visit_expression(const expr_pointer_t& expr, VariableData& data)
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down
27 changes: 24 additions & 3 deletions lib/coek/coek/ast/visitor_write_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ void visit_VariableTerm(const expr_pointer_t& expr, std::ostream& ostr)
}

#ifdef COEK_WITH_COMPACT_MODEL
void visit_VariableRefTerm(const expr_pointer_t& expr, std::ostream& ostr)
{
auto tmp = safe_pointer_cast<VariableRefTerm>(expr);
bool first = true;
ostr << tmp->name << "[";
for (auto& val : tmp->indices) {
if (first)
first = false;
else
ostr << ",";
if (auto ival = std::get_if<int>(&val)) {
ostr << *ival;
}
else if (auto eval = std::get_if<expr_pointer_t>(&val)) {
visit_expression(*eval, ostr);
}
}
ostr << "]";
}

void visit_ParameterRefTerm(const expr_pointer_t& expr, std::ostream& ostr)
{
auto tmp = safe_pointer_cast<ParameterRefTerm>(expr);
Expand All @@ -69,9 +89,9 @@ void visit_ParameterRefTerm(const expr_pointer_t& expr, std::ostream& ostr)
ostr << "]";
}

void visit_VariableRefTerm(const expr_pointer_t& expr, std::ostream& ostr)
void visit_DataRefTerm(const expr_pointer_t& expr, std::ostream& ostr)
{
auto tmp = safe_pointer_cast<VariableRefTerm>(expr);
auto tmp = safe_pointer_cast<DataRefTerm>(expr);
bool first = true;
ostr << tmp->name << "[";
for (auto& val : tmp->indices) {
Expand Down Expand Up @@ -271,8 +291,9 @@ void visit_expression(const expr_pointer_t& expr, std::ostream& ostr)
VISIT_CASE(IndexParameterTerm);
VISIT_CASE(VariableTerm);
#ifdef COEK_WITH_COMPACT_MODEL
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif

VISIT_CASE(MonomialTerm);
Expand Down
2 changes: 2 additions & 0 deletions lib/coek/coek/coek.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "coek/api/objective.hpp"
#ifdef __cpp_lib_variant
# include "coek/api/parameter_array.hpp"
# include "coek/api/data_array.hpp"
# include "coek/api/variable_array.hpp"
# include "coek/api/constraint_map.hpp"
# include "coek/api/subexpression_map.hpp"
Expand All @@ -31,6 +32,7 @@
# include "coek/compact/expression_sequence.hpp"
# include "coek/compact/objective_sequence.hpp"
# include "coek/compact/parameter_map.hpp"
# include "coek/compact/data_map.hpp"
# include "coek/compact/sequence_context.hpp"
# include "coek/compact/variable_map.hpp"
# include "coek/model/compact_model.hpp"
Expand Down
13 changes: 10 additions & 3 deletions lib/coek/coek/compact/visitor_exprtemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ expr_pointer_t visit_IndexParameterTerm(const expr_pointer_t& expr)
"value.");
}

expr_pointer_t visit_VariableRefTerm(const expr_pointer_t& expr)
{
auto tmp = safe_pointer_cast<VariableRefTerm>(expr);
return tmp->get_concrete_variable();
}

expr_pointer_t visit_ParameterRefTerm(const expr_pointer_t& expr)
{
auto tmp = safe_pointer_cast<ParameterRefTerm>(expr);
return tmp->get_concrete_parameter();
}

expr_pointer_t visit_VariableRefTerm(const expr_pointer_t& expr)
expr_pointer_t visit_DataRefTerm(const expr_pointer_t& expr)
{
auto tmp = safe_pointer_cast<VariableRefTerm>(expr);
return tmp->get_concrete_variable();
auto tmp = safe_pointer_cast<DataRefTerm>(expr);
return tmp->get_concrete_data();
}

expr_pointer_t visit_ObjectiveTerm(const expr_pointer_t& expr)
Expand Down Expand Up @@ -217,6 +223,7 @@ expr_pointer_t visit_expression(const expr_pointer_t& expr)
// VISIT_CASE(VariableTerm);
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
// VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
VISIT_CASE(StrictInequalityTerm);
Expand Down
9 changes: 8 additions & 1 deletion lib/coek/coek/model/writer_nl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void visit_expression(const expr_pointer_t& expr, VisitorType& data)
#if __cpp_lib_variant
VISIT_CASE(VariableRefTerm);
VISIT_CASE(ParameterRefTerm);
VISIT_CASE(DataRefTerm);
#endif
VISIT_CASE(MonomialTerm);
VISIT_CASE(InequalityTerm);
Expand Down Expand Up @@ -120,14 +121,20 @@ inline void visit_IndexParameterTerm(const expr_pointer_t&, TYPE&)
}

#if __cpp_lib_variant
template <class TYPE>
inline void visit_VariableRefTerm(const expr_pointer_t&, TYPE&)
{
throw std::runtime_error("Cannot write an NL file using an abstract expression!");
}

template <class TYPE>
inline void visit_ParameterRefTerm(const expr_pointer_t&, TYPE&)
{
throw std::runtime_error("Cannot write an NL file using an abstract expression!");
}

template <class TYPE>
inline void visit_VariableRefTerm(const expr_pointer_t&, TYPE&)
inline void visit_DataRefTerm(const expr_pointer_t&, TYPE&)
{
throw std::runtime_error("Cannot write an NL file using an abstract expression!");
}
Expand Down
1 change: 1 addition & 0 deletions lib/coek/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SET(sources
smoke/test_indexed.cpp
smoke/test_var.cpp
smoke/test_param.cpp
smoke/test_data.cpp
smoke/test_expr.cpp
smoke/test_obj.cpp
smoke/test_con.cpp
Expand Down
Loading

0 comments on commit 559e4d1

Please sign in to comment.