Professional Documents
Culture Documents
Cplex tutorial-SP-Radman - 96
Cplex tutorial-SP-Radman - 96
درسبرنامهریزیتصادفی
ارائهدهنده:مریمرادمان
آبانماه1396
فهرستعناوین
معرفی نرمافزار
برنامهریزی ریاضی
• معرفی محیط نرمافزار و نحوه ایجاد پروژه جدید
• مثال برنامهریزی تولید
• تعریف دادهها
• تعریف متغیرها
• تعریف تابع هدف
• تعریف محدودیتها
• بررسی خروجیهای مدل
• معرفی آرایههای چند بعدی
• معرفی filtering indexes
برنامهریزی محدودیتها
• معرفی عملگرهای ریاضی ،منطقی ،سازگاری و اختصاصی
• بررسی مثال n-queen
• بررسی مثال فروشنده دورهگرد
نحوه تبادل دادهها بین Cplexو Excel
نرمافزارCPLEX
CPLEX یک بستهی نرمافزاری شامل مجموعهای از برنامههای مدلسازی ریاضی است .این برنامه
توسط Robert E. Bixbyتوسعه یافت .در سال 1997شرکت CPLEXتوسط شرکت ILOG
خریداری شد.
این برنامه بیشتر به یک Solverصنعتی است تا یک Solverآکادمیک و تحقیقاتی .به همین علت قیمت
Licenseاین نرمافزار بسیار باالست.
نکتهی قابلتوجه دیگر این است که شرکت ILOGهیچ نسخهی Trialیا Student Versionاز این
نرمافزار را ارایه نمیکند .که این باعث میشود تا بسیاری از افراد در ایران نتوانند آن را آزمایش کنند.
نرمافزارCPLEX
CPLEX در حقیقت یک موتور حالل ( )solverاست که قابلیت اتصال به اکثر محیطهای
برنامهنویسی و بهینهسازی مدلهای ریاضی را دارا میباشد .هر چند شرکت ILOGیک
محیط ساده برای ویرایش و توسعهی مدلها در برنامه قرار دادهاست ،ولی اکثر کاربران
حرفهای ترجیح میدهند تا از قابلیتهای اتصال آن بهره بگیرند( .اتصال به ،GAMS
C# ، matlabو )...
مجموعههایی مثل GAMSو Lingo/Lindoعمدتاَ محیط توسعه مدل ریاضی به حساب
میآیند و از مجموعهای از solverها در درون خود بهره میگیرند .ولی CPLEXیک
solverاست.
به عنوان مثال ،شما ممکن است یک مدل ریاضی را در برنامه ی GAMSبنویسید و سپس
توسط CPLEXآن را حل کنید.
نرمافزارGAMS
Mathematical Programming
Definitions…
In the case of CP, OPL can represent the following types of objective
functions, data, decision variables, and constraints:
CPLEX Programming Environment
برنامههایپیشین
CPLEX Programming Environment
ایجادبرنامهجدید
CPLEX Programming Environment
نامبرنامهجدید
انتخابمکانذخیرهبرنامه
توضیحدرموردبرنامهدر
صورتنیاز
فعالکردنهر 4گزینه:
-1ایجادفایلاجرایبرنامه
-2ایجادفایلنوشتنمدل
-3ایجادفایلتنظیماتمدل
-4ایجادفایلدادههایمدل
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 IBM ILOG 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
ایجادبرنامهجدید
CPLEX Programming Environment
بادابلکلیدبرروی
برنامهجدید،نیزهر
چهارفایلرامشاهده
میکنید.
CPLEX Programming Environment
برنامهخودرادرفایل
)وارد.mod(مدل
.میکنید
CPLEX Programming Environment
دادههایبرنامهرادر
فایل().datوارد
میکنید.
CPLEX Programming Environment
تنظیماتمربوطبهحلبرنامه،شامل نوع
الگوریتموتنظیماتمربوطبهمشخصات
آنالگوریتمرادرفایلتنظیمات().ops
اجرانمایید.
CPLEX Programming Environment
اینقسمتمربوطبهتنظیماتمدل
برنامهریزیریاضیاست.
اینقسمتمربوطبهتنظیماتمدل
برنامهریزیمحدودیتهااست.
انتقالبهمحیطنرمافزار
CPLEX Programming Environment
پسازنوشتنمدلوواردکردندادههای
مربوطبهآنازرویفایلاجرایی،مدل
رااجرانمایید.
CPLEX Programming Environment
اطالعاتاجمالی
درموردمدل
دادههاومتغیرهای نتایجخروجیمدل
تصمیمگیری
زماناجرایمدل
انتقالبهمحیطنرمافزاربرایتوضیحخروجیها درصورتعدموجودفضایموجهبرایمساله،برخیمتغیرهاکهبااسم
مشخصشدهاند،آزادسازیمیشوندکهدراینقسمتبیانمیشود.
A simple example:
a production planning problem
Problem description
Consider a typical production planning problem where a company produces
a number of products. Each product has a unit profit associated with it, and
is made up of different components. The company's objective is to
maximize the profit while using only the available stock of components.
The data
The following table shows the data for this problem, together with the OPL
data type and OPL data declaration:
A simple example:
a production planning problem
The constraints
subject to{
Forall(c in Components)
sum(p in Products) usageFactor[p][c] * production[p] <= stock[c];
}
• In the OPL, constraints are written inside a block starting with subject
to {, and ending with }
• Here, 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.
• c is an index used to access each element of the set of Components.
A simple example:
a production planning problem
Data instance
انتقالبهمحیطنرمافزاربرایبررسیمثالیدیگر
An other example
Model file
12
𝑚𝑎𝑥 𝑑𝑖 𝑧𝑖
𝑖=1
𝑠. 𝑡
6
𝑐𝑗 𝑦𝑗 ≤ 𝐵
𝑗=1
6
𝑧𝑖 ≤ 𝑎𝑖𝑗 𝑦𝑗 ∀𝑖
𝑗=1
𝑧𝑖 , 𝑦𝑗 = 0,1
An other example
Data file
The choice of solver engine
The following two solver engines are available in OPL:
• int n = 8;
range Rows = n+1..2*n+1
• range R = 1..100;
int A[R]; // A is an array of 100 integers
• range R = 1..100;
forall(i in R) {
//element of a loop
{;
maximize profit;
Constraints in OPL are written in a block starting with subject to { and ending
with }.
subject to {
productionConstraint: production <= capacity;
}
productionConstraint is the name, or constraint label, of this constraint.
Labeling constraints is optional.
• IBM ILOG CP Optimizer is intended for the following two categories of problems:
Detailed scheduling problems
Decision variables are of type interval and describe an interval of time
during which, for example, a task occurs
Certain combinatorial optimization problems not well-suited for MP These
problems contain only discrete decision variables
How does CP Optimizer work?
The OPL keyword using at the beginning of a model file invokes the solution
engine to be used. To call IBM ILOG CP Optimizer as the optimization engine,
use the command:
using CP;
as the first line of your .mod file.
Constrained Programming
operations
Constrained Programming
expressions
Constrained Programming
Arithmetic constraints
Constrained Programming
Logical operator
Constrained Programming
Compatibility Constraints
• allowedAssignments
• forbiddenAssignments
𝑥 = 3,1,2
𝑦 = [2,3,1]
Constrained Programming
Specialized constraints
Constrained Programming
Example 1
n Queens
Place 𝑛-queens on an
𝑛 𝑛 board so that no
pair of queens attacks
each other
Constrained Programming
Example 1
n Queens
using CP;
int n=8;
dvar int x[1..n]in 1..n;
subject to{
allDifferent(all(i in 1..n) x[i]);
forall(i in 1..n-1)
forall(j in i+1..n)
abs(x[i]-x[j])!=j-i;
}
Constrained Programming
Example 2
Syntax:
SheetConnection <handle> ("filename.xls");
Example:
SheetConnection connex ("mySheet.xls");
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, <dataElement>, that you want to fill
with data from the spreadsheet;
2. In the .dat file, call the data to be read using the syntax:
<dataElement> from SheetRead (<handle>,“<SheetName>!<startCell>:<endCell>”;
• <dataElement> is the array or set, declared in the model file, that you
want to fill
• <SheetName> is the name of the sheet inside the Excel file that you
want to read from.
• <startCell> is the first cell, in the spreadsheet called by the connection
<handle>, of the range that you want to use to fill <dataElement>.
• <endCell> is the last cell, in the spreadsheet called by the connection
<handle>, of the range that you want to use to fill <dataElement>.
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 <dataElement>.
2. In the .dat file, write the data to a range in the spreadsheet, using the
syntax: