Skip to content

Commit

Permalink
variable error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 29, 2024
1 parent 63746c7 commit c0088d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
8 changes: 7 additions & 1 deletion amplpy/entity.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ cdef class Entity(object):
campl.AMPL_StringFree(&self._name)

def to_string(self):
cdef campl.AMPL_ERRORINFO* errorinfo
cdef campl.AMPL_RETCODE rc
cdef char* output_c
campl.AMPL_EntityGetDeclaration(self._c_ampl, self._name, &output_c)
errorinfo = campl.AMPL_EntityGetDeclaration(self._c_ampl, self._name, &output_c)
if rc != campl.AMPL_OK:
campl.AMPL_StringFree(&output_c)
PY_AMPL_CALL(errorinfo)
output = str(output_c.decode('utf-8'))
campl.AMPL_StringFree(&output_c)

return output

def __str__(self):
Expand Down
58 changes: 36 additions & 22 deletions amplpy/variable.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ cdef class Variable(Entity):
value: value to be set.
"""
if isinstance(value, Parameter):
campl.AMPL_VariableInstanceSetValue(self._c_ampl, self._name, self._index, value.value())
PY_AMPL_CALL(campl.AMPL_VariableInstanceSetValue(self._c_ampl, self._name, self._index, value.value()))
else:
campl.AMPL_VariableInstanceSetValue(self._c_ampl, self._name, self._index, float(value))
PY_AMPL_CALL(campl.AMPL_VariableInstanceSetValue(self._c_ampl, self._name, self._index, float(value)))

def astatus(self):
"""
Expand All @@ -91,23 +91,23 @@ cdef class Variable(Entity):
variable out.
"""
cdef int value
campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DEFEQN, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DEFEQN, &value))
return value

def dual(self):
"""
Get the dual value on defining constraint of variable substituted out.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DUAL, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DUAL, &value))
return value

def init(self):
"""
Get the current initial guess.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_INIT, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_INIT, &value))
return value

def init0(self):
Expand All @@ -116,111 +116,111 @@ cdef class Variable(Entity):
statement).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_INIT0, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_INIT0, &value))
return value

def lb(self):
"""
Returns the current lower bound.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB, &value))
return value

def ub(self):
"""
Returns the current upper bound.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB, &value))
return value

def lb0(self):
"""
Returns the initial lower bounds, from the var declaration.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB0, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB0, &value))
return value

def ub0(self):
"""
Returns the initial upper bound, from the var declaration.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB0, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB0, &value))
return value

def lb1(self):
"""
Returns the weaker lower bound from AMPL's presolve phase.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB1, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB1, &value))
return value

def ub1(self):
"""
Returns the weaker upper bound from AMPL's presolve phase.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB1, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB1, &value))
return value

def lb2(self):
"""
Returns the stronger lower bound from AMPL's presolve phase.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB2, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB2, &value))
return value

def ub2(self):
"""
Returns the stronger upper bound from AMPL's presolve phase.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB2, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB2, &value))
return value

def lrc(self):
"""
Returns the reduced cost at lower bound.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LRC, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LRC, &value))
return value

def urc(self):
"""
Returns the reduced cost at upper bound.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_URC, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_URC, &value))
return value

def lslack(self):
"""
Return the slack at lower bound (``val - lb``).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LSLACK, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LSLACK, &value))
return value

def uslack(self):
"""
Return the slack at upper bound (``ub - val``).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_USLACK, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_USLACK, &value))
return value

def rc(self):
"""
Get the reduced cost (at the nearer bound).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_RC, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_RC, &value))
return value

def slack(self):
Expand All @@ -229,27 +229,41 @@ cdef class Variable(Entity):
:func:`~amplpy.Variable.lslack` and :func:`~amplpy.Variable.uslack`.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_SLACK, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_SLACK, &value))
return value

def sstatus(self):
"""
Solver status (basis status of variable).
"""
cdef campl.AMPL_ERRORINFO* errorinfo
cdef campl.AMPL_RETCODE rc
cdef char* value_c
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_SSTATUS, &value_c)
errorinfo = campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_SSTATUS, &value_c)
rc = campl.AMPL_ErrorInfoGetError(errorinfo)
if rc != campl.AMPL_OK:
campl.AMPL_StringFree(&value_c)
PY_AMPL_CALL(errorinfo)
value = str(value_c.decode('utf-8'))
campl.AMPL_StringFree(&value_c)

return value

def status(self):
"""
AMPL status if not `in`, otherwise solver status.
"""
cdef campl.AMPL_ERRORINFO* errorinfo
cdef campl.AMPL_RETCODE rc
cdef char* value_c
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_STATUS, &value_c)
errorinfo = campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_STATUS, &value_c)
rc = campl.AMPL_ErrorInfoGetError(errorinfo)
if rc != campl.AMPL_OK:
campl.AMPL_StringFree(&value_c)
PY_AMPL_CALL(errorinfo)
value = str(value_c.decode('utf-8'))
campl.AMPL_StringFree(&value_c)

return value

# Aliases
Expand Down

0 comments on commit c0088d9

Please sign in to comment.