Skip to content

Commit

Permalink
error handling and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 29, 2024
1 parent 8bc7bf3 commit 8527fe7
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 108 deletions.
36 changes: 18 additions & 18 deletions amplpy/constraint.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cdef class Constraint(Entity):
"""
cdef bool_c value
try:
campl.AMPL_ConstraintIsLogical(self._c_ampl, self._name, &value)
PY_AMPL_CALL(campl.AMPL_ConstraintIsLogical(self._c_ampl, self._name, &value))
return value
except AttributeError:
return False
Expand All @@ -65,21 +65,21 @@ cdef class Constraint(Entity):
Drop all instances in this constraint entity, corresponding to the AMPL
code: `drop constraintname;`.
"""
campl.AMPL_EntityDrop(self._c_ampl, self._name)
PY_AMPL_CALL(campl.AMPL_EntityDrop(self._c_ampl, self._name))

def restore(self):
"""
Restore all instances in this constraint entity, corresponding to the
AMPL code: `restore constraintname;`.
"""
campl.AMPL_EntityRestore(self._c_ampl, self._name)
PY_AMPL_CALL(campl.AMPL_EntityRestore(self._c_ampl, self._name))

def body(self):
"""
Get the current value of the constraint's body.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_BODY, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_BODY, &value))
return value

def astatus(self):
Expand All @@ -98,23 +98,23 @@ cdef class Constraint(Entity):
constraint.
"""
cdef int value
campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DEFVAR, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DEFVAR, &value))
return value

def dinit(self):
"""
Get the current initial guess for the constraint's dual variable.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT, &value))
return value

def dinit0(self):
"""
Get the original initial guess for the constraint's dual variable.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT0, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT0, &value))
return value

def dual(self):
Expand All @@ -127,23 +127,23 @@ cdef class Constraint(Entity):
(see :func:`~amplpy.AMPL.setOption`).
"""
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 lb(self):
"""
Get the current value of the constraint's 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):
"""
Get the current value of the constraint's 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 lbs(self):
Expand All @@ -152,7 +152,7 @@ cdef class Constraint(Entity):
adjustment for fixed variables).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LBS, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LBS, &value))
return value

def ubs(self):
Expand All @@ -161,31 +161,31 @@ cdef class Constraint(Entity):
adjustment for fixed variables).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UBS, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UBS, &value))
return value

def ldual(self):
"""
Get the current dual value associated with the lower bound.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LDUAL, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LDUAL, &value))
return value

def udual(self):
"""
Get the current dual value associated with the upper bounds.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UDUAL, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UDUAL, &value))
return value

def lslack(self):
"""
Get the slack at lower bound `body - 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):
Expand All @@ -201,7 +201,7 @@ cdef class Constraint(Entity):
Constraint slack (the lesser of lslack and 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):
Expand Down Expand Up @@ -240,15 +240,15 @@ cdef class Constraint(Entity):
Args:
dual: The value to be assigned to the dual variable.
"""
campl.AMPL_ConstraintSetDual(self._c_ampl, self._name, float(dual))
PY_AMPL_CALL(campl.AMPL_ConstraintSetDual(self._c_ampl, self._name, float(dual)))

def val(self):
"""
Get the AMPL val suffix. Valid only for logical constraints.
"""
cdef double value
if self.is_logical():
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_VAL, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_VAL, &value))
return value
else:
return None
Expand Down
2 changes: 1 addition & 1 deletion amplpy/entity.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cdef class Entity(object):
cdef object _entity

@staticmethod
cdef create(campl.AMPL* ampl_c, char *name, campl.AMPL_TUPLE* index, parent):
cdef create(campl.AMPL* ampl_c, char *name, campl.AMPL_TUPLE* index, object parent):
cdef campl.AMPL_ERRORINFO* errorinfo
cdef campl.AMPL_RETCODE rc
cdef campl.AMPL_ENTITYTYPE entitytype
Expand Down
8 changes: 4 additions & 4 deletions amplpy/objective.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cdef class Objective(Entity):
Get the value of the objective.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, NULL, campl.AMPL_NUMERICSUFFIX.AMPL_VALUE, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, NULL, campl.AMPL_NUMERICSUFFIX.AMPL_VALUE, &value))
return value

def astatus(self):
Expand Down Expand Up @@ -67,7 +67,7 @@ cdef class Objective(Entity):
objective.
"""
cdef int value
campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name, NULL, campl.AMPL_NUMERICSUFFIX.AMPL_EXITCODE, &value)
PY_AMPL_CALL(campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name, NULL, campl.AMPL_NUMERICSUFFIX.AMPL_EXITCODE, &value))
return value

def message(self):
Expand Down Expand Up @@ -96,13 +96,13 @@ cdef class Objective(Entity):
"""
Drop this objective instance.
"""
campl.AMPL_EntityDrop(self._c_ampl, self._name)
PY_AMPL_CALL(campl.AMPL_EntityDrop(self._c_ampl, self._name))

def restore(self):
"""
Restore this objective (if it had been dropped, no effect otherwise).
"""
campl.AMPL_EntityRestore(self._c_ampl, self._name)
PY_AMPL_CALL(campl.AMPL_EntityRestore(self._c_ampl, self._name))

def minimization(self):
"""
Expand Down
Loading

0 comments on commit 8527fe7

Please sign in to comment.