VariableInstance

class VariableInstance

The VariableInstance object stores the information regarding a specific instance of a variable. The instances can be accessed through the function Variable.get of the parent entity.

value()

classmethod VariableInstance.value()

Syntax

v = value()

Description

v = value() Get the current value of the variable instance

Output Arguments

v

The current value of the variable instance

Example

Create a variable x, get an instance and display its value.

ampl.eval('var x{1..2};');
x = ampl.getVariable('x');
instance = x.get(1);
instance.value

gives:

ans =
  0

setValue(v)

classmethod VariableInstance.setValue()

Syntax

setValue(v)

Description

setValue(v) Assigns the specified value to the variable. Equivalent to the AMPL code:

var x;
let x := v;

Input Arguments

v

The value to be assigned

Example

Create a variable x and sets its initial value. Then displays it:

ampl.eval('var x{1..2};');
x = ampl.getVariable('x');
instance = x.get(1);
instance.setValue(4);
instance.value

gives:

ans =
  4

dual

classmethod VariableInstance.dual()

Syntax

v = dual

Description

dual Get the current dual value of the variable instance.

Output Arguments

v

The dual value of the instance

Example

Create a variable x, get an instance and display its dual value.

ampl.eval('var x{1..2};');
x = ampl.getVariable('x');
instance = x.get(1);
instance.dual

gives:

ans =
  0

lb

classmethod VariableInstance.lb()

Syntax

v = lb

Description

lb Get the current lower bound of the variable. See Note on variables suffixes.

Output Arguments

v

The lower bound of the variable

Example

Create an indexed variable x (using AMPL.eval) and get the value of the lower bound.

ampl.eval('var x{1..3};');
x = ampl.getVariable('x');
instance = x.get(1);
v = instance.lb

gives:

v = 0

ub

classmethod VariableInstance.ub()

Syntax

v = ub

Description

ub Get the current upper bound of the instance. See Note on variables suffixes.

Output Arguments

v

The upper bound of the instance

Example

Create an indexed variable y (AMPL.eval) and get the value of one instance.

ampl.eval('var x{1..3};');
x = ampl.getVariable('x');
instance = x.get(1);
v = instance.lb

gives:

v = 0

fix

classmethod VariableInstance.fix()

Syntax

fix

fix(value)

Description

fix Fix this instance to its current value

fix(value) Fix this instance to the specified value

Example

Create a scalar variable x and an indexed variable y (using AMPL.eval), fix a few values, solve and unfix them

  ampl.eval('var x<=3; var y{1..3}<=4;');
  ampl.eval('maximize z: x + sum{i in 1..3} y[i];');
  x = ampl.getVariable('x');
  y = ampl.getVariable('y');
  x.fix(1);         % fix variables
  y.get(2).fix(2);
  ampl.solve; % solve
  x.display   % display solution
  y.display
  x.unfix;    % unfix variables
  y.unfix;
  ampl.solve; % solve and display
  x.display   % display solution
  y.display

gives::

  ans =
     x = 1

  ans =
     y [*] :=
     1  4
     2  2
     3  4
     ;

  ans =
     x = 3

  ans =
     y [*] :=
     1  4
     2  4
     3  4
     ;

unfix

classmethod VariableInstance.unfix()

Syntax

unfix

Description

unfix Unfix this instance

Example

Create a scalar variable x and an indexed variable y (using AMPL.eval), fix a few values, solve and unfix them

  ampl.eval('var x<=3; var y{1..3}<=4;');
  ampl.eval('maximize z: x + sum{i in 1..3} y[i];');
  x = ampl.getVariable('x');
  y = ampl.getVariable('y');
  x.fix(1);         % fix variables
  y.get(2).fix(2);
  ampl.solve; % solve
  x.display   % display solution
  y.display
  x.unfix;    % unfix variables
  y.unfix;
  ampl.solve; % solve and display
  x.display   % display solution
  y.display

gives::

  ans =
     x = 1

  ans =
     y [*] :=
     1  4
     2  2
     3  4
     ;

  ans =
     x = 3

  ans =
     y [*] :=
     1  4
     2  4
     3  4
     ;