USING SETS IN LINGO What Are Sets? Sets are simply groups of related objects. A set might be a...

24
USING SETS IN LINGO What Are Sets? Sets are simply groups of related objects. A set might be a list of products, trucks, or employees. Each member in the set may have one or more characteristics. For example, each product in a set of products might have a price attribute; each truck in a set of trucks might have a capacity attribute; and each employee in a set of employees might have a salary attribute.

description

A primitive set has the following syntax: SETS: setname / member_list / : attribute_list; ENDSETS The set name :is a name you choose to designate the set. A member list : is a list of the members that represent the set. If the set members are included in the set definition, they may be listed either explicitly or implicitly. When listing members explicitly, you enter a unique name for each member for example we could have used an explicit member list to define the set WAREHOUSES in the sets section as follows: SETS: WAREHOUSES / WH1 WH2 WH3 WH4 WH5 WH6/: CAPACITY; ENDSETS

Transcript of USING SETS IN LINGO What Are Sets? Sets are simply groups of related objects. A set might be a...

USING SETS IN LINGOWhat Are Sets?

Sets are simply groups of related objects. A set might be a list of

products, trucks, or employees. Each member in the set may have one

or more characteristics.

For example, each product in a set of products might have a price

attribute; each truck in a set of trucks might have a capacity attribute;

and each employee in a set of employees might have a salary attribute.

Why Use Sets?

LINGO’s language allows you to express your largest models very quickly and

easily.

Types of Sets

LINGO recognizes two kinds of sets: primitive and derived sets.

Primitive Sets:

A primitive set is a set composed only of objects that can’t be reduced.

To define a primitive set , you specify:

• The name of the set.

• Optionally, its members (objects contained in the set).

• Optionally, any attributes the members of the set may have.

A primitive set has the following syntax:SETS: setname / member_list / : attribute_list;

ENDSETS

The set name :is a name you choose to designate the set.

A member list : is a list of the members that represent the set. If the set members

are included in the set definition, they may be listed either explicitly or

implicitly. When listing members explicitly, you enter a unique name for each

member

for example we could have used an explicit member list to define the set

WAREHOUSES in the sets section as follows:

SETS:

WAREHOUSES / WH1 WH2 WH3 WH4 WH5 WH6/: CAPACITY;

ENDSETS

Implicit Member Example List Format Set Members

1..n 1..5 1, 2, 3, 4, 5

dayM..dayN MON..FRI MON, TUE, WED, THU, FRI

monthM..monthN OCT..JAN OCT, NOV, DEC, JAN

Month Year M.. Month Year N OCT2001..JAN2002 OCT2001, NOV2001, DEC2001, JAN2002

Derived set :The derived set is defined similarly primitive set, but must also include the parent set list. An example of a derived set could be:

SETS:

PRODUCT / A B/;

MACHINE / M N/;

WEEK / 1 2/;

ALLOWED (PRODUCT, MACHINE, WEEK) ;

ENDSETS

Sets PRODUCT, MACHINE, and WEEK are primitive sets, while ALLOWED is

derived from parent sets, PRODUCT, MACHINE, and WEEK. Taking all

the combinations of members from the three parent

sets, we come up with the following members in the

ALLOWED set:

USING DATA IN LINGO:

LINGO provides a separate section called the DATA section in which

values can be defined for different variables.

The DATA section begins with the tag DATA: and ends with the tag ENDDATA.

Statements within the DATA section follow the syntax: object_list = value_list;

Example 1:SETS: WAREHOUSES / 1..NUMBER_OF_WH/: CAPACITY; ENDSETS

DATA:

NUMBER_OF_WH = 6;

ENDDATA

EXAMPLE 2: consider the following model: SETS: SET1: X, Y; ENDSETS DATA: SET1 = A B C; X = 1 2 3; Y = 4 5 6 ; DATA END We have two attributes X and Y defined on the set SET1. The three values of X are set to 1, 2, and 3, while Y is set to 4, 5, and 6.

Set Looping Functions There are currently four set looping functions in LINGO.The syntax for a set looping function is: @function(setname [ (set_index_list) [|conditional_qualifier]] :

expression_list);

The names of the functions and their uses are:

@FOR Used to generate constraints over members of a set.

@SUM Computes the sum of an expression over all members of a set.

@MIN Computes the minimum of an expression over all members of a set.

@MAX Computes the maximum of an expression over all members of a set.

@SUM Set Looping Function

Example 3:

Consider the model:

MODEL:

SETS: VENDORS/V1..V5/: DEMAND;

ENDSETS

DATA:

DEMAND = 5 1 3 4 6;

ENDDATA

TOTAL_DEMAND = @SUM(VENDORS(J): DEMAND(J));

Note that:

LINGO evaluates the @SUM function by first initializing an internal accumulator to zero. LINGO

then begins looping over the members in the VENDORS set. The set index variable, J, is set to the

first member of VENDORS (i.e., V1) and DEMAND (V1) is then added to the accumulator. This

process continues until all DEMAND values have been added to the accumulator. The value of the

sum is then stored in the TOTAL_DEMAND variable.

@FOR Set Looping Function

Example 4:

Here is a model that uses an @FOR statement to compute the reverse value for five numbers placed into the VALUE

attribute:

MODEL:

SETS:

NUMBERS /1..5/: VALUE, Reverse;

ENDSETS

DATA:

VALUE = 3 4 2 7 10;

ENDDATA

@FOR( NUMBERS(I): REVERSE( I ) = 1 / VALUE(I) );

@MIN and @MAX Set Looping Functions Again, consider the Example 3:SETS: VENDORS/ V1..V5 /:DEMAND; ENDSETS DATA: DEMAND = 5 1 3 4 6; ENDDATA MIN_DEMAND = @MIN( VENDORS( J): DEMAND( J)); MAX_DEMAND = @MAX( VENDORS( J): DEMAND( J)); END

We can write above example by this method SETS: VENDORS: DEMAND; ENDSETS DATA: VENDORS, DEMAND = V1,5 V2,1 V3,3 V4,4 V5,6; ENDDATA MIN_DEMAND = @MIN( VENDORS( J): DEMAND( J)); MAX_DEMAND = @MAX( VENDORS( J): DEMAND( J)); END

Using Variable Domain Functions

These variable domain functions are:

@GIN –any positive integer value

@BIN –a binary value ( 0 or 1)

@FREE –any positive or negative real value

@BND –any value within the specified bounds

Example :

Solve the following knapsack problem by using lingo software :

Max 5X1+3X2+8X3+9X4+4X5

3X1+X2+5X3+4X4+X5=25

0≤X1 ≤2 , 0 ≤X2 ≤1 , 0 ≤X3 ≤4 , 0 ≤X4 ≤2, 0 ≤X5 ≤1

X1,X2,X3,X4,and X5 are integer.

Example :

Solve the following LP problem

MAX 4X1+2X2+3X3+6X4-4X5-11X6

3X1+X2+5X3+2X4-2X5-X6 ≤ 9

-X1+X2+X3+2X4-3X6 ≤ 3

2X1-X2-4X3+X4+X5-X6 ≤ -2

X1,X2,X3,X4,X5and X6 are binary variables