cplex یزاسهنیهب رازفا مرن شزومآ - sharifie.sharif.edu/~sp/cplex.pdf · 2019....

30
CPLEX آموزشینهم افزار به نر سازی برنامه تصادفیزی ریعی رفی دکتر کنندهیه ته: علی کرمانی آذر98

Upload: others

Post on 03-Feb-2021

14 views

Category:

Documents


0 download

TRANSCRIPT

  • CPLEX سازی نرم افزار بهینهآموزش

    ریزی تصادفیبرنامهدکتر رفیعی

    علی کرمانی: تهیه کننده

    98آذر

  • A glimpse of IBM ILOG OPL

    CPLEX یک حل کننده(Solver ) ه برای مدل های ریاضی است و قابلیت اتصال به اکثر محیط هاای برااماو GAMSتفااو مداده ب باا ساایر اارا اایارهاای بهیناه ساازی ا یار . اویسی و بهینه سازی را دارسات

    LINGOده در این است که، این ارا اایارها محیطی برای توسعه مدل ریاضی بوده و مجدومه ای از حل کنن.را در خود جای داده اادCPLEXها از جدله

    CPLEX بیشتر یکsolverصنعتی بوده و هیچ اسخهtrial یاstudent قیدات . برای ب ارائه اشده اساتLicense ب زیاد است و به هدین جهت بسیاری از دااشجویاب ترجیح می دهند از محیط هاای اارا ااایاری

    .در ب محیط ها استفاده ادایندsolverدیگر استفاده ادوده و از این

  • CPLEX Programming Environment

  • CPLEX Programming Environment

  • CPLEX Programming Environment

    ریزی غیر قطعیدرس برنامه

    علی جعفری: اراِئه دهنده

    98فروردین

  • More about OPL projects

    OPL projects are located inside directories, typically with the same name as the project, on the

    computer's file system. These directories typically contain:

    • Model files (.mod)

    • Data files (.dat)

    • Settings files (.ops)

    • Model files contain data declarations, as well as the model definition in terms of decision

    variables, the objective function and constraints. Model files may also contain IBMI LOG

    Script statements.

    • Data files initialize the data declared in the model files. Data can be initialized directly in

    the .dat file, or imported from external sources.

    • Setting files are used to change the default settings in OPL, for example parameters that

    define solution algorithm behavior, display options, and so forth.

  • CPLEX Programming Environment

    ریزی غیر قطعیدرس برنامه

    علی جعفری: اراِئه دهنده

    98فروردین

  • CPLEX Programming Environment

    ریزی غیر قطعیدرس برنامه

    علی جعفری: اراِئه دهنده

    98فروردین

  • CPLEX Programming Environment(mode)

  • CPLEX Programming Environment (data)

  • CPLEX Programming Environment

  • CPLEX Programming Environment

  • CPLEX Programming Environment

  • Data-Range

    • range Rows=1..10;

    • int n = 8;

    range Rows = n+1..2*n+1

    • range R = 1..100;

    forall(I in R) {

    //element of a loop

    };

    • range float myFloatRange= 1.2..2.2;

  • Arrays-Initializing arrays

    • In the model file:

    int myIntegerArray[1..4] = [1, 3, 5, 7];

    • In the data file:

    myIntegerArray = [1, 3, 5, 7];

    • range R = 1..4;

    int a[R]=[1,2,3,4];

  • Arrays-multidimensional arrays

    • A two-dimensional array declaration and initialization:

    int my2DArray[1..2][1..3]=[[5,2,5],[4,4,6]];

    • Declaration in the model file:

    int my2DArray[1..2][1..3]=...;

    • initialization in the data file:

    my2DArray=[[5,2,5],[4,4,6]];

    • A three-dimensional array declaration and initialization:

    int my3DArray[1..2][1..3][1..4]=

    [[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]];

    • You can combine different types of indices in multi dimensional arrays,for example:

    int numberOfWorkers[Days][1..3]=...;

  • Data

    When declaring data, you need to decide:

    • The name for the data item

    • The data type:

    a. Integer (OPL keyword int)

    b. Real (OPL keyword float)

    c. String (OPL keyword string)

    The data structure, which can be a scalar, a range, a set, an array, or a tuple.

    Examples:

    float unitProfit=...;

    int a[i][j]=...;

  • Data-initialization

    Data can be initialized:

    in the model (.mod file)or the data(.dat file)files

    read from spread sheets and data bases.

    Examples:

    Float unitProfit=2.5; in the.mod file

    Or

    Float unitProfit =…; in the .mod file & unitProfit=2.5; in the.dat file

    {int} myIntegerSet={1,3,5,7}; in the.mod file

    Or

    {int} myIntegerSet=…; in the .mod file & myIntegerSet={1,3,5,7}; in the .dat file

  • Decision variables

    • dvar is the OPL keyword used to declare decision variables.

    • float is the OPL keyword used for real numbers, and the + is added to denote that the

    quantities are non-negative.

    When declaring decision variables, you need to decide:

    The name of the variable

    The variable type:

    Integer (OPL keyword int)

    Real (OPL keyword float, for MP only)

    Boolean (OPL keyword boolean)

    Example:

    dvar float+ y[i][j];

    Note that all declarations in OPL end with a semicolon.

  • Objective function

    • maximize is the OPL keyword used for maximization problems. minimize is used for

    minimization problems.

    • sum is the OPL keyword to compute the summation of a collection of expressions.

    • Note that we use normal parentheses, as in (p in Products), to denote the selection to sum

    over.

    • p is an index used to access each element of the set of Products.

    Example:

    maximize sum(p in Products)profit[p]*production[p];

  • Constraints

    Constraints in OPL are written in a block starting with subject to{and ending

    With }.

    Example:

    subject to{

    productionConstraint: production

  • Constraints

    • forall is the OPL keyword used when expressions are similar, except for their indices. In this case, it's used to write only one constraint for all components, seeing that the constraints only differ according to the product or component they refer to.

    Example:

    subject to{

    forall(c in Components)

    sum(p in Products)usageFactor[p][c]*production[p]

  • Operators-numeric

  • Operators-numeric

  • Exchanging data with a spreadsheet

    To exchange data with a spreadsheet:

    1. Establish a connection between the OPL application and the spreadsheet using

    SheetConnection

    • SheetConnection takes a handle – OPL will use this as the “pipeline.”

    2. Read data from the spreadsheet using SheetRead.

    • You can read from one or more sheets in an Excel file.

    • You can define a range of cells to read.

    3. Write data to the spreadsheet using SheetWrite.

    • You can write to one or more sheets in an Excel file.

    • You can define a range of cells to write to.

  • Exchanging data with a spreadsheet

    Connecting to a spreadsheet

    Before an OPL model can read from and/or write to a spreadsheet, it must connect to

    the spreadsheet using the SheetConnection instruction.

    This is not done in the .mod file but in the .dat file.

    Steps:

    1. Write your model in the .mod file exactly as you would any model in a project

    with independent data.

    2. To initialize the data, use links to the spreadsheet in the .dat file.

    Syntax:

    SheetConnection ("filename.xls");

    Example:

    SheetConnection connex ("mySheet.xls");

    Establishes a connection connex to a spreadsheet named mySheet.xls in read/write

    mode

  • Exchanging data with a spreadsheet

    Reading from a spreadsheet

    Once the model is connected to the spreadsheet, data can be read into these data

    elements using the SheetRead command:

    • One-dimensional arrays

    • Two-dimensional arrays or sets

    Steps:

    1. In the .mod file, declare the array or set, , that you want to fill with data

    from the spreadsheet;

    2. In the .dat file, call the data to be read using the syntax:

    from SheetRead (,“!:”;

    is optional. If no is given, and the Excel workbook has more than one

    sheet, the currently active sheet is read.

  • Exchanging data with a spreadsheet

    Reading from a spreadsheet

    Example:

    Streets from SheetRead (connex,”addresses!A1:C13”)

    Reads the range of cells from A1 to C13 into a data element named Streets

    from the

    sheet named addresses that is located in the spreadsheet that uses the

    connex

    connection.

    • is the array or set, declared in the model file, that you want to fill

    • is the name of the sheet inside the Excel file that you want to read from.

    • is the first cell, in the spreadsheet called by the connection , of the

    range that you want to use to fill .

    • is the last cell, in the spreadsheet called by the connection

    , of the range that you want to use to fill .

  • Exchanging data with a spreadsheet

    Writing to a spreadsheet

    You can write into spreadsheets using the SheetWrite command. As with SheetRead, you write the

    SheetWrite command in the .dat file.

    Steps:

    1. You must already have an array or set declared in the .mod file and instantiated, from which you

    will write to the spreadsheet. This is represented here by the token .

    2. In the .dat file, write the data to a range in the spreadsheet, using the syntax: to SheetWrite

    to SheetWrite (, “!:”;

  • Exchanging data with a spreadsheet

    example

    1. In the model file, declare the data and variables.

    {string} Products =...;

    {int} TimePeriods =...;

    float rate[Products] =...;

    dvar float+ Make[Products][TimePeriods];

    dvar float+ Sell[Products][TimePeriods];

    2. In the data file, declare the connection to the spreadsheet.

    SheetConnection sheet("xsteel.xls");

    3. In the data file, write how to read the data values in the spreadsheet.

    Products from SheetRead(sheet,"data!A2:A3");

    TimePeriods from SheetRead(sheet,"data!A17:A20");

    rate from SheetRead(sheet,"data!B2:B3");

    4. Also in the data file, add the statements that write the result into the spreadsheet.

    Make to SheetWrite(sheet,"RESULT!A2:D3");

    Sell to SheetWrite(sheet,"RESULT!A8:D9");