Difference: VariablesInAMPL (6 vs. 7)

Revision 72009-10-09 - MichaelOSullivan

Line: 1 to 1
 
META TOPICPARENT name="AMPLSyntax"
<-- Ready to Review -->

Variables in AMPL

Line: 22 to 22
 

Declaring a Variable

AMPL variables are created by using the var keyword and a label for the variable:

Changed:
<
<
var ; 
>
>
var <varname>; 
 

Example

var x1; 

Often, you will want to create one variable for every member (respectively pair, tuple) from a set (a two-dimensional set, multi-dimensional set, respectively). To do this you simply add the set after the variable name:

Changed:
<
<
var  [{}]; 
>
>
var <varname> [{<set>}]; 
 

Example

var Percentage {INGREDIENTS}; 
Line: 41 to 41
 

Variable Types

Variables must be one of four types: real (the default), binary (0-1), integer or symbolic. You declare the type of a variable by adding the type keyword after the variable label (or giving no type if it is a real variable).

Changed:
<
<
var  []; 
>
>
var <varname> [<type>]; 
 

Examples

var Build binary;
var Production {SURFBOARDS} integer; 
Line: 59 to 59
 
var Percentage {INGREDIENTS} >= 0;
var Production {SURFBOARDS} integer >= 0; 

Bounds are set by adding them after the variable label:

Changed:
<
<
var  [>= ] [<= ]; 
Note There are many attributes that may follow a variable declaration. They can be placed in any order and must be separated by either whitespace or commas.
var Percentage {INGREDIENTS} <= 0, >= 100;
var Production {SURFBOARDS} integer >= 0; 
>
>
var  <varname> [>= <lb>] [<= <ub>]; 

Note There are many attributes that may follow a variable declaration. They can be placed in any order and must be separated by either whitespace or commas.

var Percentage {INGREDIENTS} <= 0, >= 100;
var Production {SURFBOARDS} integer >= 0; 
  Return to top
Line: 75 to 77
 Another way of setting initial variable values is to set a new default value (otherwise AMPL sets variables to 0 by default). This is done using the default keyword.
var Price {SURFBOARDS} >= 0 default 100; 
Changed:
<
<
You can still change the initial value of the variables using the {\tt let} keyword, but if you don't they will automatically take on the default value.
>
>
You can still change the initial value of the variables using the let keyword, but if you don't they will automatically take on the default value.
 
var Price {SURFBOARDS} >= 0 default 1;
let Price['Super Thruster'] := 50;
solve; # Price['Malibu'] starts at 100, Price['Super Thruster'] starts at 50 

Return to top

Line: 104 to 106
 Once variables have been added to a model you cannot remove them. However, you can set their values and fix them so that their value doesn't change. AMPL will then treat that variable as a parameter with the fixed value.

To fix a variable, use the fix keyword and the variable name.

Changed:
<
<
fix ; 
>
>
fix <varname>; 
  If you decide that the variable should be considered again, you can unfix the variable.

To stop a variable being fixed, use the unfix keyword and the variable name.

Changed:
<
<
unfix ; 
>
>
unfix <varname>; 
  Consider the Surfboard Production Problem we can fix the Price variables to remove their nonlinearity. This allows us to use CPLEX to find the optimal production levels for different prices and explore the nonlinear objective function of the price variables.
param Objective {1..150, 1..150};
fix Price;
for {p in 1..150 by 1, q in 1..150 by 1} {
  let Price['Malibu'] := p;
  let Price['Super Thruster'] := q;
  option solver cplex;
  solve;
  let Objective[p, q] := TotalProfit;
} 
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback