# Constraint¶

public class Constraint extends Entity<ConstraintInstance>

This class represents an algebraic or logical constraint. In case the constraint is scalar, its values can be accessed via functions like Constraint.body and Constraint.dual. All the AMPL suffixes for constraints (see http://www.ampl.com/NEW/suffbuiltin.html) are available through methods of this class with the same name (and methods of ConstraintInstance for indexed constraints).

Note that, since this class represents both algebraic and logical constraints, some suffixes might not be available for every entity.

An UnsupportedOperationException is thrown if one of such methods is called for a non-scalar constraint and if a method corresponding to a suffix which is not supported by the type of the constraint is called.

The instances, represented by the class ConstraintInstance can be accessed via the method Constraint.get (see section Access to instances and values for further information on how to access instances).

To gain access to all the values in an entity (for all instances and all suffixes for that entities), see Entity.getValues and the DataFrame class.

## Fields¶

### isLogical¶

boolean isLogical

## Constructors¶

### Constraint¶

Constraint()

### Constraint¶

Constraint(boolean isLogical)

### Constraint¶

Constraint(AMPL ampl, Engine engine, String name)

## Methods¶

### astatus¶

public String astatus()

Get the current AMPL status (dropped, presolved, or substituted out)

### body¶

public double body()

Get the current value of the constraint’s body

### defvar¶

public int defvar()

Get the index in _var of “defined variable” substituted out by the constraint

### dinit¶

public double dinit()

Get the current initial guess for the constraint’s dual variable

### dinit0¶

public double dinit0()

Get the original initial guess for the constraint’s dual variable

### drop¶

public void drop()

Drop all instances in this constraint entity, corresponding to the AMPL code:

drop constraintname;


### dual¶

public double dual()

Get the current value of the constraint’s dual variable.

Note that dual values are often reset by the underlying AMPL interpreter by the presolve functionalities triggered by some methods. A possible workaround is to set the option presolve to false (see AMPL.setBoolOption).

### get¶

public ConstraintInstance get()

Get the constraint instance (to be used only on scalar constraints)

Throws: UnsupportedOperationException – if the constraint is not scalar. The corresponding instance. @ throws IllegalStateException if the constraint has been deleted in the underlying AMPL interpreter

### get¶

public ConstraintInstance get(Object... key)

Get the constraint instance corresponding to the specified key. See Access to instances and values for further information on how the key can be specified.

Parameters: key – The key to be found IllegalArgumentException – if trying to access an instance with the wrong number of keys (see Entity.indexarity) IllegalStateException – if the entity has been deleted in the underlying AMPL interpreter The constraint instance corresponding to the specified key. Returns null if the specified key is not present.

### get¶

public ConstraintInstance get(Tuple key)

Get the constraint instance corresponding to the specified key, expressed as a Tuple. See Access to instances and values for further information on how the key can be specified.

Parameters: key – The indexing Tuple of the instance to be found IllegalArgumentException – if trying to access an instance with the wrong indexarity (see Entity.indexarity) IllegalStateException – if the entity has been deleted in the underlying AMPL interpreter The constraint instance corresponding to the specified key. Returns null if the specified key is not present.

### isLogical¶

public boolean isLogical()

Check if the constraint is a logical constraint. The available suffixes differ between logical and non logical constraints. See http://www.ampl.com/NEW/suffbuiltin.html for a list of the available suffixes for algebraic constraints. The suffixes available for logical constraints are marked on the method description by “Valid only for logical constraints”.

Returns: True if logical

### lb¶

public double lb()

Get the current value of the constraint’s lower bound

### lbs¶

public double lbs()

Get the constraint lower bound sent to the solver (reflecting adjustment for fixed variables)

### ldual¶

public double ldual()

Get the current dual value associated with the lower bound

### lslack¶

public double lslack()

Get the slack at lower bound body - lb

### refreshInstances¶

void refreshInstances()

### restore¶

public void restore()

Restore all instances in this constraint entity, corresponding to the AMPL code:

restore constraintname;


### setDeclaration¶

void setDeclaration(String declaration)

### setDual¶

public void setDual(double dual)

Set the value of the dual variable associated to this constraint (valid only if the constraint is scalar). Equivalent to the AMPL statement:

let c := dual;


Note that dual values are often reset by the underlying AMPL interpreter by the presolve functionalities triggered by some methods. A possible workaround is to set the option presolve to false (see AMPL.setBoolOption).

Parameters: dual – The value to be assigned to the dual variable

### slack¶

public double slack()

Constraint slack (the lesser of lslack and uslack)

### sstatus¶

public String sstatus()

Get the solver status (basis status of constraint’s slack or artificial variable)

### status¶

public String status()

Get the AMPL status if not in, otherwise solver status

### toString¶

public String toString()

Returns a string representation of this constraint. Equivalent to the AMPL statement

show constraintName;


### ub¶

public double ub()

Get the current value of the constraint’s upper bound

### ubs¶

public double ubs()

Get the constraint upper bound sent to the solver (reflecting adjustment for fixed variables)

### udual¶

public double udual()

Get the current dual value associated with the upper bounds

### uslack¶

public double uslack()

Get the slack at upper bound ub - body

### val¶

public double val()

Get the AMPL val suffix. Valid only for logical constraints.