NO FRAMES

Class IloExpr

Definition file: ilconcert/iloexpression.h
An instance of this class represents an expression in a model.

An instance of this class represents an expression in a model. An instance of IloExpr is a handle.

Expressions in Environments

The variables in an expression must all belong to the same environment as the expression itself. In other words, you must not mix variables from different environments within the same expression.

Most member functions in this class contain assert statements. For an explanation of the macro NDEBUG (a way to turn on or turn off these assert statements), see the concept Assert and NDEBUG.

Programming Hint: Creating Expressions

In addition to using a constructor of this class to create an expression, you may also initialize an instance of IloExpr as a C++ expression built from variables of a model. For example:


 IloNumVar x;
 IloNumVar y;
 IloExpr expr = x + y;

Programming Hint: Empty Handles and Null Expressions

This statement creates an empty handle:

IloExpr e1;

You must initialize it before you use it. For example, if you attempt to use it in this way:

e1 += 10; // BAD IDEA

Without the compiler option -DNDEBUG, that line will cause an assert statement to fail because you are attempting to use an empty handle.

In contrast, the following statement

IloExpr e2(env);

creates a handle to a null expression. You can use this handle to build up an expression, for example, in this way:

e2 += 10; // OK

Normalizing Linear Expressions: Reducing the Terms

Normalizing is sometimes known as reducing the terms of a linear expression.

Linear expressions consist of terms made up of constants and variables related by arithmetic operations; for example, x + 3y is a linear expression of two terms consisting of two variables. In some expressions, a given variable may appear in more than one term, for example, x + 3y +2x. Concert Technology has more than one way of dealing with linear expressions in this respect, and you control which way Concert Technology treats expressions from your application.

In one mode, Concert Technology analyzes linear expressions that your application passes it and attempts to reduce them so that a given variable appears in only one term in the linear expression. This is the default mode. You set this mode with the member function IloEnv::setNormalizer(IloTrue).

In the other mode, Concert Technology assumes that no variable appears in more than one term in any of the linear expressions that your application passes to Concert Technology. We call this mode assume normalized linear expressions. You set this mode with the member function IloEnv::setNormalizer(IloFalse).

Certain constructors and member functions in this class check this setting in the environment and behave accordingly: they assume that no variable appears in more than one term in a linear expression. This mode may save time during computation, but it entails the risk that a linear expression may contain one or more variables, each of which appears in one or more terms. Such a case may cause certain assertions in member functions of this class to fail if you do not compile with the flag -DNDEBUG.

Certain constructors and member functions in this class check this setting in the environment and behave accordingly: they attempt to reduce expressions. This mode may require more time during preliminary computation, but it avoids of the possibility of a failed assertion in case of duplicates.

See Also:

Constructor Summary
public IloExpr()
public IloExpr(IloNumExprI * expr)
public IloExpr(const IloNumLinExprTerm term)
public IloExpr(const IloIntLinExprTerm term)
public IloExpr(IloNumExprArg)
public IloExpr(const IloEnv env, IloNum=0)
Method Summary
public IloNumgetConstant() const
public IloNumLinTermI *getImpl() const
public IloExpr::LinearIteratorgetLinearIterator() const
public IloBoolisNormalized() const
public IloIntnormalize() const
public IloExpr &operator *=(IloNum val)
public IloExpr &operator+=(const IloIntLinExprTerm term)
public IloExpr &operator+=(const IloNumLinExprTerm term)
public IloExpr &operator+=(const IloIntVar var)
public IloExpr &operator+=(const IloNumVar var)
public IloExpr &operator+=(const IloNumExprArg expr)
public IloExpr &operator+=(IloNum val)
public IloExpr &operator-=(const IloIntLinExprTerm term)
public IloExpr &operator-=(const IloNumLinExprTerm term)
public IloExpr &operator-=(const IloIntVar var)
public IloExpr &operator-=(const IloNumVar var)
public IloExpr &operator-=(const IloNumExprArg expr)
public IloExpr &operator-=(IloNum val)
public IloExpr &operator/=(IloNum val)
public voidremove(const IloNumVarArray vars)
public voidsetConstant(IloNum cst)
public voidsetLinearCoef(const IloNumVar var, IloNum value)
public voidsetLinearCoefs(const IloNumVarArray vars, IloNumArray values)
public voidsetNumConstant(IloNum constant)
Inherited Methods from IloNumExpr
getImpl, operator *=, operator+=, operator+=, operator-=, operator-=, operator/=
Inherited Methods from IloNumExprArg
getImpl
Inherited Methods from IloExtractable
asConstraint, asIntExpr, asModel, asNumExpr, asObjective, asVariable, end, getEnv, getId, getImpl, getName, getObject, isConstraint, isIntExpr, isModel, isNumExpr, isObjective, isVariable, setName, setObject
Inner Class
IloExpr::LinearIterator An iterator over the linear part of an expression.
Constructor Detail

IloExpr

public IloExpr()

This constructor creates an empty handle. You must initialize it before you use it.


IloExpr

public IloExpr(IloNumExprI * expr)

This constructor creates an expression from a pointer to the implementation class of numeric expressions IloNumExprI*.


IloExpr

public IloExpr(const IloNumLinExprTerm term)

This constructor creates an integer expression with linear terms using the undocumented class IloNumLinExprTerm.


IloExpr

public IloExpr(const IloIntLinExprTerm term)

This constructor creates an integer expression with linear terms using the undocumented class IloIntLinExprTerm.


IloExpr

public IloExpr(IloNumExprArg)

This constructor creates an expression using the undocumented class IloNumExprArg.


IloExpr

public IloExpr(const IloEnv env, IloNum=0)

This constructor creates an expression in the environment specified by env. It may be used to build other expressions from variables belonging to env. You must not mix variables of different environments within an expression.


Method Detail

getConstant

public IloNum getConstant() const

This member function returns the constant term in the invoking expression.


getImpl

public IloNumLinTermI * getImpl() const

This member function returns the implementation object of the invoking enumerated variable.


getLinearIterator

public IloExpr::LinearIterator getLinearIterator() const

This methods returns a linear iterator on the invoking expression.


isNormalized

public IloBool isNormalized() const

This member function returns IloTrue if the invoking expression has been normalized using IloExpr::normalize.


normalize

public IloInt normalize() const

This member function normalizes the invoking linear expression. Normalizing is sometimes known as reducing the terms of a linear expression. That is, if there is more than one linear term using the same variable in the invoking linear expression, then this member function merges those linear terms into a single term expressed in that variable. The return value specifies the number of merged terms.

For example, 1*x + 17*y - 3*x becomes 17*y - 2*x, and the member function returns 1 (one).

If you attempt to use this member function on a nonlinear expression, it throws an exception.


operator *=

public IloExpr & operator *=(IloNum val)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x * ...


operator+=

public IloExpr & operator+=(const IloIntLinExprTerm term)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x + ...


operator+=

public IloExpr & operator+=(const IloNumLinExprTerm term)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x + ...


operator+=

public IloExpr & operator+=(const IloIntVar var)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x + ...


operator+=

public IloExpr & operator+=(const IloNumVar var)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x + ...


operator+=

public IloExpr & operator+=(const IloNumExprArg expr)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x + ...


operator+=

public IloExpr & operator+=(IloNum val)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x + ...


operator-=

public IloExpr & operator-=(const IloIntLinExprTerm term)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x - ...


operator-=

public IloExpr & operator-=(const IloNumLinExprTerm term)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x - ...


operator-=

public IloExpr & operator-=(const IloIntVar var)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x - ...


operator-=

public IloExpr & operator-=(const IloNumVar var)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x - ...


operator-=

public IloExpr & operator-=(const IloNumExprArg expr)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x - ...


operator-=

public IloExpr & operator-=(IloNum val)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x - ...


operator/=

public IloExpr & operator/=(IloNum val)

This operator is recommended for building a Concert Technology expression in a loop. It is more efficient than x = x / ...


remove

public void remove(const IloNumVarArray vars)

This member function removes all occurrences of all variables listed in the array vars from the invoking expression. For linear expressions, the effect of this member function is equivalent to setting the coefficient for all the variables listed in vars to 0 (zero).


setConstant

public void setConstant(IloNum cst)

This member function assigns cst as the constant term in the invoking expression.


setLinearCoef

public void setLinearCoef(const IloNumVar var, IloNum value)

This member function assigns value as the coefficient of var in the invoking expression if the invoking expression is linear. This member function applies only to linear expressions. In other words, you can not use this member function to change the coefficient of a non linear expression. An attempt to do so will cause Concert Technology to throw an exception.


setLinearCoefs

public void setLinearCoefs(const IloNumVarArray vars, IloNumArray values)

For each of the variables in vars, this member function assigns the corresponding value of values as its linear coefficient if the invoking expression is linear. This member function applies only to linear expressions. In other words, you can not use this member function to change the coefficient of a nonlinear expression. An attempt to do so will cause Concert Technology to throw an exception.


setNumConstant

public void setNumConstant(IloNum constant)

This member function assigns cst as the constant term in the invoking expression.