# Difference: LogicInAMPL (1 vs. 7)

#### Revision 72009-09-18 - MichaelOSullivan

Line: 1 to 1

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

# Logic in AMPL

Changed:
<
<
>
>

## Relational Operators

Relational operators are used to compare two expressions. They are most commonly used in constraints, but not exclusively. The relational operators are:

Changed:
<
<
 < Less than <= Less than or equal to > Greater than >= Greater than or equal to >= Equal to <> Not equal to
>
>
 < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to <> Not equal to
Line: 31 to 29
Logical operators are used to combine logical expressions. They are most commonly used in conditional statements, conditional structures and conditional loops. The logical operators are not, and and or. They are used as shown in the table below:

Changed:
<
<
 not  True if  is false, false if  is true  and  True if  and  are both true, otherwise false  or  True if either  or  are true, otherwise false
>
>
 not  True if is false, false if is true  and  True if and are both true, otherwise false  or  True if either or are true, otherwise false

## Logical Expressions

Changed:
<
<
Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the relational operators:
Lower[r] <= sum {i in INGREDIENTS} Contributes[r, i] * Amount[i]


sum {s in SURFBOARDS} Recipe[m, s] * Production[s] <= Supply[m];

>
>
Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the relational operators:
Lower[r] <= sum {i in INGREDIENTS} Contributes[r, i] * Amount[i]

sum {s in SURFBOARDS} Recipe[m, s] * Production[s] <= Supply[m];
However, there are some special logical expressions in AMPL for use with sets:
Changed:
<
<
1. <e> in <SET> is true if <e> is a member of <SET>;
2. <e> not in <SET> is false if <e> is a member of <SET>;
3. exists {<e> in <SET>} <expression> is true if some <e> in <SET> has <expression> being true;
4. forall {<e> in <SET>} <expression> is true if all <e> in <SET> have <expression> being true;
5. <SUBSET> within <SET> is true if all the elements in <SUBSET> are in <SET>;
6. <SUBSET> not within <SET> is true if some element in <SUBSET> is not in <SET>.
>
>
1. in is true if is a member of ;
2. not in is false if is a member of ;
3. exists { in } is true if some in has being true;
4. forall { in } is true if all in have being true;
5. within is true if all the elements in are in ;
6. not within is true if some element in is not in .
Logical expressions can be built up from other logical expressions, binary parameters and logical operators.
Line: 64 to 57

## Conditional Expressions

A conditional expression is very much like the IF function in Microsoft Excel:

Changed:
<
<
param ifvalue := if <some logical expression> then
<a value>
[else
<another value>];

>
>
param ifvalue := if  then                                     [else                    ];

Changed:
<
<
If the logical expression is true then ifvalue will be set to <a value>, otherwise it is set to 0 (by default) or, if the else part of the expression is present, <another value>. Note that if the else keyword is present, then no ; needs to be included after <a value>.
>
>
If the logical expression is true then ifvalue will be set to , otherwise it is set to 0 (by default) or, if the else part of the expression is present, . Note that if the else keyword is present, then no ; needs to be included after .

## Conditional Structures

A conditional structure is the same as the classical if_-_then_-_else statement in programming languages like MATLAB, Fortran, Visual Basic and C++:

Changed:
<
<
if <logical expression> then
<a statement>;
[else
<another statement>;]


Note here that even if the else keyword is present you need to end <a statement> with ;. If you want to include more than one statement within the conditional structures you can use { and } to enclose your statements:

if <logical expression> then
{
<some statements>
}
[else
{
<some other statements>
}]

>
>
if  then   ; [else   ;]
if  then {    } [else {    }]

## Binary Parameters

In AMPL we can create binary parameters by using the binary keyword in the parameter declaration:

Changed:
<
<
param stillSearching binary;

>
>
param stillSearching binary;
Binary parameters are used in a similar way to boolean variables (in Matlab, C, etc) and logical variables (Fortran). If a binary parameter has the value 0 this is equivalent to false, and 1 is equivalent to true. Binary parameters can be used with conditional expressions to hold a true/false result from a logical expression:
Changed:
<
<
param isGreater binary;

let isGreater := if 4 > 5 then 1 else 0; # isGreater = 0 (false)
let isGreater := if 6 > 5 then 1; # else 0 is the default, isGreater = 1 (true)

so the syntax is
let <binary parameter> := if <expression> then 1;

>
>
param isGreater binary;  let isGreater := if 4 > 5 then 1 else 0; # isGreater = 0 (false) let isGreater := if 6 > 5 then 1; # else 0 is the default, isGreater = 1 (true)
so the syntax is
let  := if  then 1;
You can also set binary parameters within conditional structures
Changed:
<
<
binary <binary parameter>;

if <expression> then
let <binary parameter> := 1;
else
let <binary parameter> := 0;

>
>
binary ;  if  then   let  := 1; else   let  := 0;
Binary parameters may be used in logical expressions or as the condition in a conditional statement or conditional structure. They are very useful for building complex conditional statements or structures:
Changed:
<
<
Some example from depth first searching or column generation

Coming soon! and controlling conditional loops:
Some example from depth first searching or column generation

Coming soon!
>
>
Some example from depth first searching or column generation
Coming soon! and controlling conditional loops:
Some example from depth first searching or column generation
Coming soon!

#### Revision 62008-04-06 - MichaelOSullivan

Line: 1 to 1

 META TOPICPARENT name="AMPLSyntax"
<-- Ready to Review -->
Line: 51 to 51
However, there are some special logical expressions in AMPL for use with sets:
1. <e> in <SET> is true if <e> is a member of <SET>;
Changed:
<
<
1. {\tt <e> not in <SET>} is false if <e> is a member of <SET>;
>
>
1. <e> not in <SET> is false if <e> is a member of <SET>;

1. exists {<e> in <SET>} <expression> is true if some <e> in <SET> has <expression> being true;
2. forall {<e> in <SET>} <expression> is true if all <e> in <SET> have <expression> being true;
3. <SUBSET> within <SET> is true if all the elements in <SUBSET> are in <SET>;

Line: 1 to 1

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

Some example from depth first searching or column generation

>
>
Coming soon!
and controlling conditional loops:
Some example from depth first searching or column generation

>
>
Coming soon!

Line: 1 to 1

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

# Logic in AMPL

Deleted:
<
<
• Logical Expressions
• Binary Parameters
•
• Logical Operators
>
>
• Logical Expressions
•
• Conditional Expressions
• Conditional Structures
>
>
• Binary Parameters
•

## Relational Operators

Line: 24 to 26
>
>

## Logical Operators

Logical operators are used to combine logical expressions. They are most commonly used in conditional statements, conditional structures and conditional loops. The logical operators are not, and and or. They are used as shown in the table below:

Expression Result
not <expression> True if <expression> is false, false if <expression> is true
<e1> and <e2> True if <e1> and <e2> are both true, otherwise false
<e1> or <e2> True if either <e1> or <e2> are true, otherwise false

## Logical Expressions

Logical expressions are expressions that will evaluate to either true or false. Logical expressions are

Changed:
<
<
usually defined in terms of the relational operators:
>
>
usually defined in terms of the relational operators:

Lower[r] <= sum {i in INGREDIENTS} Contributes[r, i] * Amount[i]

Line: 48 to 61
>
>

## Conditional Expressions

A conditional expression is very much like the IF function in Microsoft Excel:

param ifvalue := if <some logical expression> then
<a value>
[else
<another value>];


If the logical expression is true then ifvalue will be set to <a value>, otherwise it is set to 0 (by default) or, if the else part of the expression is present, <another value>. Note that if the else keyword is present, then no ; needs to be included after <a value>.

## Conditional Structures

A conditional structure is the same as the classical if_-_then_-_else statement in programming languages like MATLAB, Fortran, Visual Basic and C++:

if <logical expression> then
<a statement>;
[else
<another statement>;]


Note here that even if the else keyword is present you need to end <a statement> with ;. If you want to include more than one statement within the conditional structures you can use { and } to enclose your statements:

if <logical expression> then
{
<some statements>
}
[else
{
<some other statements>
}]


## Binary Parameters

Changed:
<
<
In AMPL we can create binary parameters by using the {\tt binary} keyword in the parameter declaration:

\begin{verbatim}

>
>
In AMPL we can create binary parameters by using the binary keyword in the parameter declaration:


param stillSearching binary;
Changed:
<
<
\end{verbatim}

Binary parameters are used in a similar way to boolean variables (in Matlab, C, etc) and logical variables (Fortran). If a binary parameter has the value 0 this is equivalent to false, and 1 is equivalent to true. Binary parameters can be used with conditional expressions to hold a true/false result from a logical expression:

\begin{verbatim}

>
>

Binary parameters are used in a similar way to boolean variables (in Matlab, C, etc) and logical variables (Fortran). If a binary parameter has the value 0 this is equivalent to false, and 1 is equivalent to true. Binary parameters can be used with conditional expressions to hold a true/false result from a logical expression:



param isGreater binary;

let isGreater := if 4 > 5 then 1 else 0; # isGreater = 0 (false) let isGreater := if 6 > 5 then 1; # else 0 is the default, isGreater = 1 (true)

Changed:
<
<
\end{verbatim}

>
>
so the syntax is
Changed:
<
<

\begin{verbatim} \end{verbatim} or as the condition in a conditional statement or conditional structure. They are very useful for building complex conditional statements or structures:

\begin{verbatim} Some example from depth first searching or column generation \end{verbatim}

and controlling loops.

### Logical Operators

<\tt not} {\tt and} {\tt or}

### Conditional Expressions

A conditional expression is very much like the IF function in Excel: param ifvalue := if then [else ];

>
>
let <binary parameter> := if <expression> then 1;


Changed:
<
<
If the logical expression is true then ifvalue will be set to , otherwise it is set to 0 (by default) or, if the else part of the expression is present, . Note that if the else keyword is present, then no ; needs to be included after .
>
>
You can also set binary parameters within conditional structures
binary <binary parameter>;



Changed:
<
<

### Conditional Structures

>
>
if <expression> then let <binary parameter> := 1; else let <binary parameter> := 0;

Changed:
<
<
A conditional structure is the same as the classical if-then-else statement in programming languages like MATLAB, Fortran, Visual Basic and C++: if then ; [else ;] Note here that even if the else keyword is present you need to end with ;. If you want to include more than one statement within the conditional structures you can use { and } to enclose your statements. if then { } [else { }]
>
>
Binary parameters may be used in logical expressions or as the condition in a conditional statement or conditional structure. They are very useful for building complex conditional statements or structures:
Some example from depth first searching or column generation

and controlling conditional loops:
Some example from depth first searching or column generation


>
>
-- TWikiAdminGroup - 18 Mar 2008 \ No newline at end of file

Line: 1 to 1

 META TOPICPARENT name="AMPLSyntax"

# Logic in AMPL

Line: 16 to 16
Relational operators are used to compare two expressions. They are most commonly used in constraints, but not exclusively. The relational operators are:
Expression Meaning
< Less than
Changed:
<
<
 < Less than or equal to
>
>
 <= Less than or equal to

 > Greater than >= Greater than or equal to >= Equal to
Line: 24 to 24
Changed:
<
<

>
>

## Logical Expressions

Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the relational operators:
Changed:
<
<

\begin{verbatim} sum {i in INGREDIENTS} FatPercent[i] * Percentage[i] >= MinFat \end{verbatim}

\begin{verbatim} sum {i in INGREDIENTS} FibrePercent[i] * Percentage[i] <= MaxFibre; \end{verbatim}

However, there are some special logical expressions in AMPL for use with sets:

1. {\tt <e> in <SET>} is true if {\tt <e>} is a member of {\tt <SET>};
2. {\tt <e> not in <SET>} is false if {\tt <e>} is a member of {\tt <SET>};
3. exists { in } is true if some in has being true;
4. forall { in } is true if all in have being true;
5. within is true if all the elements in are in ;
6. not within is true if some element in is not in .
>
>
Lower[r] <= sum {i in INGREDIENTS} Contributes[r, i] * Amount[i]


sum {s in SURFBOARDS} Recipe[m, s] * Production[s] <= Supply[m];


However, there are some special logical expressions in AMPL for use with sets:

1. <e> in <SET> is true if <e> is a member of <SET>;
2. {\tt <e> not in <SET>} is false if <e> is a member of <SET>;
3. exists {<e> in <SET>} <expression> is true if some <e> in <SET> has <expression> being true;
4. forall {<e> in <SET>} <expression> is true if all <e> in <SET> have <expression> being true;
5. <SUBSET> within <SET> is true if all the elements in <SUBSET> are in <SET>;
6. <SUBSET> not within <SET> is true if some element in <SUBSET> is not in <SET>.

Logical expressions can be built up from other logical expressions, binary parameters and logical operators.

Changed:
<
<
Logical expressions can be built up from other logical expressions, binary parameters and logical operators.
>
>

Changed:
<
<

# Under Construction

>
>

## Binary Parameters

Deleted:
<
<

### Binary Parameters

In AMPL we can create binary parameters by using the {\tt binary} keyword in the parameter declaration:

\begin{verbatim} param stillSearching binary;

Line: 1 to 1

 META TOPICPARENT name="AMPLSyntax"
Changed:
<
<

>
>

# Logic in AMPL

## Relational Operators

Relational operators are used to compare two expressions. They are most commonly used in constraints, but not exclusively. The relational operators are:

Expression Meaning
< Less than
< Less than or equal to
> Greater than
>= Greater than or equal to
>= Equal to
<> Not equal to

### Logical Expressions

Logical expressions are expressions that will evaluate to either true or false. Logical expressions are usually defined in terms of the relational operators:

\begin{verbatim} sum {i in INGREDIENTS} FatPercent[i] * Percentage[i] >= MinFat \end{verbatim}

\begin{verbatim} sum {i in INGREDIENTS} FibrePercent[i] * Percentage[i] <= MaxFibre; \end{verbatim}

However, there are some special logical expressions in AMPL for use with sets:

1. {\tt <e> in <SET>} is true if {\tt <e>} is a member of {\tt <SET>};
2. {\tt <e> not in <SET>} is false if {\tt <e>} is a member of {\tt <SET>};
3. exists { in } is true if some in has being true;
4. forall { in } is true if all in have being true;
5. within is true if all the elements in are in ;
6. not within is true if some element in is not in .

Logical expressions can be built up from other logical expressions, binary parameters and logical operators.

# Under Construction

### Binary Parameters

In AMPL we can create binary parameters by using the {\tt binary} keyword in the parameter declaration:

\begin{verbatim} param stillSearching binary; \end{verbatim}

Binary parameters are used in a similar way to boolean variables (in Matlab, C, etc) and logical variables (Fortran). If a binary parameter has the value 0 this is equivalent to false, and 1 is equivalent to true. Binary parameters can be used with conditional expressions to hold a true/false result from a logical expression:

\begin{verbatim} param isGreater binary;

let isGreater := if 4 > 5 then 1 else 0; # isGreater = 0 (false) let isGreater := if 6 > 5 then 1; # else 0 is the default, isGreater = 1 (true) \end{verbatim}

so the syntax is

\begin{verbatim} \end{verbatim} or as the condition in a conditional statement or conditional structure. They are very useful for building complex conditional statements or structures:

\begin{verbatim} Some example from depth first searching or column generation \end{verbatim}

and controlling loops.

### Logical Operators

<\tt not} {\tt and} {\tt or}

### Conditional Expressions

A conditional expression is very much like the IF function in Excel: param ifvalue := if then [else ];

### Conditional Structures

A conditional structure is the same as the classical if-then-else statement in programming languages like MATLAB, Fortran, Visual Basic and C++: if then ; [else ;] Note here that even if the else keyword is present you need to end with ;. If you want to include more than one statement within the conditional structures you can use { and } to enclose your statements. if then { } [else { }]

Deleted:
<
<
Coming soon!
-- TWikiAdminGroup - 18 Mar 2008 \ No newline at end of file

Line: 1 to 1