Skip to content

Commit

Permalink
improve strings in c
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 24, 2024
1 parent 00f407e commit 9d0cb06
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 318 deletions.
28 changes: 17 additions & 11 deletions amplpy/ampl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ cdef class AMPL:
Returns:
The AMPL entity with the specified name.
"""
return Entity.create(self._c_ampl, name, NULL)
cdef char* name_c = strdup(name.encode('utf-8'))
return Entity.create(self._c_ampl, name_c, NULL)

def get_variable(self, name):
"""
Expand All @@ -216,9 +217,10 @@ cdef class AMPL:
KeyError: if the specified variable does not exist.
"""
cdef campl.AMPL_ENTITYTYPE entitytype
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name.encode('utf-8'), &entitytype))
cdef char* name_c = strdup(name.encode('utf-8'))
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name_c, &entitytype))
if entitytype != campl.AMPL_VARIABLE: raiseKeyError(campl.AMPL_VARIABLE, name)
return Variable.create(self._c_ampl, name, NULL)
return Variable.create(self._c_ampl, name_c, NULL)

def get_constraint(self, name):
"""
Expand All @@ -231,9 +233,10 @@ cdef class AMPL:
KeyError: if the specified constraint does not exist.
"""
cdef campl.AMPL_ENTITYTYPE entitytype
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name.encode('utf-8'), &entitytype))
cdef char* name_c = strdup(name.encode('utf-8'))
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name_c, &entitytype))
if entitytype != campl.AMPL_CONSTRAINT: raiseKeyError(campl.AMPL_CONSTRAINT, name)
return Constraint.create(self._c_ampl, name, NULL)
return Constraint.create(self._c_ampl, name_c, NULL)

def get_objective(self, name):
"""
Expand All @@ -246,9 +249,10 @@ cdef class AMPL:
KeyError: if the specified objective does not exist.
"""
cdef campl.AMPL_ENTITYTYPE entitytype
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name.encode('utf-8'), &entitytype))
cdef char* name_c = strdup(name.encode('utf-8'))
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name_c, &entitytype))
if entitytype != campl.AMPL_OBJECTIVE: raiseKeyError(campl.AMPL_OBJECTIVE, name)
return Objective.create(self._c_ampl, name, NULL)
return Objective.create(self._c_ampl, name_c, NULL)

def get_set(self, name):
"""
Expand All @@ -261,9 +265,10 @@ cdef class AMPL:
KeyError: if the specified set does not exist.
"""
cdef campl.AMPL_ENTITYTYPE entitytype
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name.encode('utf-8'), &entitytype))
cdef char* name_c = strdup(name.encode('utf-8'))
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name_c, &entitytype))
if entitytype != campl.AMPL_SET: raiseKeyError(campl.AMPL_SET, name)
return Set.create(self._c_ampl, name, NULL)
return Set.create(self._c_ampl, name_c, NULL)

def get_parameter(self, name):
"""
Expand All @@ -276,9 +281,10 @@ cdef class AMPL:
KeyError: if the specified parameter does not exist.
"""
cdef campl.AMPL_ENTITYTYPE entitytype
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name.encode('utf-8'), &entitytype))
cdef char* name_c = strdup(name.encode('utf-8'))
PY_AMPL_CALL(campl.AMPL_EntityGetType(self._c_ampl, name_c, &entitytype))
if entitytype != campl.AMPL_PARAMETER: raiseKeyError(campl.AMPL_PARAMETER, name)
return Parameter.create(self._c_ampl, name, NULL)
return Parameter.create(self._c_ampl, name_c, NULL)

def eval(self, statements):
"""
Expand Down
46 changes: 23 additions & 23 deletions amplpy/constraint.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cdef class Constraint(Entity):
and the :class:`~amplpy.DataFrame` class.
"""
@staticmethod
cdef create(campl.AMPL* ampl_c, name, campl.AMPL_TUPLE* index):
cdef create(campl.AMPL* ampl_c, char* name, campl.AMPL_TUPLE* index):
entity = Constraint()
entity._c_ampl = ampl_c
entity._name = name
Expand All @@ -52,7 +52,7 @@ cdef class Constraint(Entity):
"""
cdef bool_c value
try:
campl.AMPL_ConstraintIsLogical(self._c_ampl, self._name.encode('utf-8'), &value)
campl.AMPL_ConstraintIsLogical(self._c_ampl, self._name, &value)
return value
except AttributeError:
return False
Expand All @@ -62,29 +62,29 @@ 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.encode('utf-8'))
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.encode('utf-8'))
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_BODY, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_BODY, &value)
return value

def astatus(self):
"""
Get the current AMPL status (dropped, presolved, or substituted out).
"""
cdef char* value_c
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_STRINGSUFFIX.AMPL_ASTATUS, &value_c)
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_ASTATUS, &value_c)
value = str(value_c.decode('utf-8'))
campl.AMPL_StringFree(&value_c)
return value
Expand All @@ -95,23 +95,23 @@ cdef class Constraint(Entity):
constraint.
"""
cdef int value
campl.AMPL_InstanceGetIntSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DEFVAR, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT0, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DINIT0, &value)
return value

def dual(self):
Expand All @@ -124,23 +124,23 @@ cdef class Constraint(Entity):
(see :func:`~amplpy.AMPL.setOption`).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_DUAL, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LB, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UB, &value)
return value

def lbs(self):
Expand All @@ -149,7 +149,7 @@ cdef class Constraint(Entity):
adjustment for fixed variables).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LBS, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LBS, &value)
return value

def ubs(self):
Expand All @@ -158,47 +158,47 @@ cdef class Constraint(Entity):
adjustment for fixed variables).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UBS, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LDUAL, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_UDUAL, &value)
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LSLACK, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_LSLACK, &value)
return value

def uslack(self):
"""
Get the slack at upper bound `ub - body`.
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_USLACK, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_USLACK, &value)
return value

def slack(self):
"""
Constraint slack (the lesser of lslack and uslack).
"""
cdef double value
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_SLACK, &value)
campl.AMPL_InstanceGetDoubleSuffix(self._c_ampl, self._name, self._index, campl.AMPL_NUMERICSUFFIX.AMPL_SLACK, &value)
return value

def sstatus(self):
Expand All @@ -207,7 +207,7 @@ cdef class Constraint(Entity):
variable).
"""
cdef char* value_c
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_STRINGSUFFIX.AMPL_SSTATUS, &value_c)
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_SSTATUS, &value_c)
value = str(value_c.decode('utf-8'))
campl.AMPL_StringFree(&value_c)
return value
Expand All @@ -217,7 +217,7 @@ cdef class Constraint(Entity):
Get the AMPL status if not `in`, otherwise solver status.
"""
cdef char* value_c
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name.encode('utf-8'), self._index, campl.AMPL_STRINGSUFFIX.AMPL_STATUS, &value_c)
campl.AMPL_InstanceGetStringSuffix(self._c_ampl, self._name, self._index, campl.AMPL_STRINGSUFFIX.AMPL_STATUS, &value_c)
value = str(value_c.decode('utf-8'))
campl.AMPL_StringFree(&value_c)
return value
Expand All @@ -237,15 +237,15 @@ cdef class Constraint(Entity):
Args:
dual: The value to be assigned to the dual variable.
"""
campl.AMPL_ConstraintSetDual(self._c_ampl, self._name.encode('utf-8'), float(dual))
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.encode('utf-8'), self._index, campl.AMPL_NUMERICSUFFIX.AMPL_VAL, &value)
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/dataframe.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ cdef class DataFrame(object):
ncols = self._get_num_cols()
nindices = self._get_num_indices()
if ncols - nindices == 1:
return [row[-1] for row in lst]
return [row[len(row)-1] for row in lst]
else:
return [row[nindices:] for row in lst]
else:
Expand Down
Loading

0 comments on commit 9d0cb06

Please sign in to comment.