Skip to content

Commit edeae0f

Browse files
authored
Add checks for unoptimized variable and constraint values in IpoptModel (#92)
1 parent eb8576e commit edeae0f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/ipopt_model.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ void IpoptModel::set_variable_start(const VariableIndex &variable, double start)
123123

124124
double IpoptModel::get_variable_value(const VariableIndex &variable)
125125
{
126+
if (m_is_dirty)
127+
{
128+
throw std::runtime_error(
129+
"Variable value is not available before optimization. Call optimize() first.");
130+
}
126131
return m_result.x[variable.index];
127132
}
128133

@@ -151,6 +156,11 @@ std::string IpoptModel::pprint_variable(const VariableIndex &variable)
151156

152157
double IpoptModel::get_obj_value()
153158
{
159+
if (m_is_dirty)
160+
{
161+
throw std::runtime_error(
162+
"Objective value is not available before optimization. Call optimize() first.");
163+
}
154164
return m_result.obj_val;
155165
}
156166

@@ -174,12 +184,22 @@ int IpoptModel::_constraint_internal_index(const ConstraintIndex &constraint)
174184

175185
double IpoptModel::get_constraint_primal(const ConstraintIndex &constraint)
176186
{
187+
if (m_is_dirty)
188+
{
189+
throw std::runtime_error(
190+
"Constraint primal value is not available before optimization. Call optimize() first.");
191+
}
177192
int index = _constraint_internal_index(constraint);
178193
return m_result.g[index];
179194
}
180195

181196
double IpoptModel::get_constraint_dual(const ConstraintIndex &constraint)
182197
{
198+
if (m_is_dirty)
199+
{
200+
throw std::runtime_error(
201+
"Constraint dual value is not available before optimization. Call optimize() first.");
202+
}
183203
int index = _constraint_internal_index(constraint);
184204
auto dual = -m_result.mult_g[index];
185205
return dual;

0 commit comments

Comments
 (0)