I have an error in my code in CPLEX which says:
Operator not available for dvar int+[][time] + dexpr float.
int i=...; //set of origins
int j=...; //set of destinations
int t=...;//set of time
//parameters
float al[origins][destinations][time]=...;//Proportion of all utilized vehicles that are dispatched
//variables
dvar int+ o[origins][destinations][time];//numbers of vehicles present at i at beginning of t
dvar int+ l[origins][destinations][time];//numbers of rented vehicles dispatched from i to j during t
dvar int+ e[origins][destinations][time];//numbers of unutilized vehicles dispatched from i to j during t
subject to{
constraint:
forall(i in origins, t in time:(t-1) in time)
o[i][t]== o[i][t-1]+ sum (j in destinations, t in time ) al[i][j][t] * e[j][i][t]- l[i][j][t-1];
}
How do I solve this error?
Your decision variable o is defined to have 3 indices, however, in your constraint you only use two indices.
Also, I think
sum (j in destinations, t in time ) al[i][j][t] * e[j][i][t]- l[i][j][t-1];
should be written as (note the additional parentheses)
sum (j in destinations, t in time ) (al[i][j][t] * e[j][i][t]- l[i][j][t-1]);
Related
I am a beginner of programming. I'm trying to practice run a simulation with CPLEX.
Since I want to be a person who wants to work in an area in optimization.
Therefore, I am trying to study some journals from different areas by myself.
The attached image is the objective function and constraints.
I made up the code written below. I am not sure I made it right or wrong.
enter image description here
//Data
{string} product = ...;
{string} interval = ...;
// Limit
float low[interval] = ...;
float upper[interval] = ...;
// Maximum demand
float A[product] = ...;
// Slope of demand function
float varphi[product][interval] = ...;
// Price
float p[product] = ...;
// Setup cost
float f[product] = ...;
// Inventory holding cost rate
float h[product][interval] = ...;
// Variable cost rate
float v[product][interval] = ...;
// Variables
dvar float+ X[product][interval]; enter code here
dvar float+ D[product][interval];
dvar float+ a[interval];
dvar float+ beta[product][interval];
// Objective
maximize sum(i in product, j in interval) p[i]*X[i][j]-f[i]*beta[i][j]-v[i][j]*X[i][j]-0.5*h[i][j]*X[i][j] - F;
// Constraint
subject to{
forall(j in interval) sum(j in interval) a[j] == 1;
forall(i in product, j in interval) beta[i][j] <= a[j];
forall(i in product, j in interval) sum(i in product, j in interval) X[i][j] <= C;
forall(i in product, j in interval) X[j][i] <= D[i][j]*beta[i][j];
forall(j in interval) sum(i in product) beta[i][j] <= upper[j]*a[j];
forall(j in interval) sum(i in product) beta[i][j] >= low[j]*a[j];
forall(i in product, j in interval) D[i][j] = (A[i]-varphi[i][j]*p[i])*a[j];
}
I want to demonstrate how an iterative procedure may be applied to progressively narrow the interval of search to precisely determine the optimal number of products to produce in order to maximize profit.
The journal explains as "The cost and revenue data for these smaller intervals are provided as new inputs to the model which then identifies one of these new intervals as best in the subsequent iteration. The procedure is repeated in successive iterations until the last interval has only one or two levels (i.e., numbers of products) from which the model is able to make a final choice of product variety. In order to save time when the marginal benefit from successive iterations is very small, we also terminate the process if the difference between the objective function values (profit) of successive iterations is below a small predetermined convergence parameter. The model determines which of the levels of the final interval is optimal and also identifies which particular products to produce and in what quantities.
The first stage of this process begins with 100 products (as before) and a configuration involving price structure 3 and cost structure 3 (please see Tables 2–5). The product variety range is divided into four intervals with interval boundaries shown in Table 1. The result from this stage is that the third interval is selected as optimal, with 75 products and their corresponding optimal production quantities identified. The data are shown in Table 9 and the results are provided in Table 12, Table 13 (which also provide results from subsequent stages)."
Tables are attached below.
enter image description here
enter image description here
enter image description here
enter image description here
Product is total 100.
I have not decided the values of other parameters.
What I want to know is how to run the following iterative procedure?
When I run CPLEX, the objective function in the code keeps telling there is an error?
Thank you for your kind answers and you are the bests.
Regards,
forall(j in interval) sum(j in interval) a[j] == 1;
Looks bad since you used j twice !
For errors you should share dat file so that Other users could try
I am new to this platform CPLEX which means a beginner of programming.
I'm trying to write the code for CPLEX for below questions.
Question 1.
∑j (X^D(i,j,t) <= min(k^P(i,t),k^A(i,t) for all i, t
enter image description here
I tried to write the code like something below
forall(i in plants,t in years)
{
sum(j in products:j!)(X[i,j,t]) <= kP[i,t];
sum(j in products:j!)(X[i,j,t]) <= kA[i,t];
}
Is this right?
First, how do you write the code (sum of j, without having specific values)
Second, is there a way to express 'min' constraint in CPLEX?
The next question is how do you write the code of sum of four values.
It's the advanced version of the upper question.
cost_t=∑s,i,m,j(c^S(s,i,m,j,t)*X^S(s,i,m,j,t)+∑i,j,t(c(i,j,t)*X(i,j,t)+∑i,r,j,t(c^D(i,r,j,t)*X^D(i,r,j,t)
enter image description here
How do you write the sum part with four or three index?
Thank you for your kind answers and you are the bests.
Regards,
range plants=1..10;
range years=2021..2022;
range products=1..4;
dvar int+ X[plants][products][years];
int kP[i in plants,t in years]=i*t;
int kA[i in plants,t in years]=i*t+2;
dvar float cost;
subject to
{
forall(i in plants,t in years)
{
sum(j in products:j!=1)(X[i,j,t]) <= minl(kP[i,t],kA[i,t]);
}
cost==sum(i,i2 in plants,t in years,j in products:i!=j) (X[i][j][t]+X[i2][j][t]);
}
works fine
minl means minimum of a few values
some qutsions about cplex intermediate variable define and solutions export need your help.
how to define intermediate parameter.
'MC from SheetRead(my_sheet, "SCmodel!b3:m26"); //MC[m][n] is parameters ;'
'how to define parameter EC[m][n], in which EC = 19.6-17*MC; '
how to define intermediate variable.
'dvar float+ X[m][n];'
'how to define variable Y[m][n], in which Y = X * MC + MD.....'
how to export solutions to excel. I use ILOG CPLEX in Win10 64bit.
about questions 1 and 3 you should use SheetRead and SheetWrite.
Within CPLEX you have a full example at CPLEX_Studio129\opl\examples\opl\oil
about question 2
range m=1..4;
range n=1..3;
dvar float+ X[m][n];
dvar float+ Y[m][n];
float MC=1;
float MD=2;
subject to
{
forall(i in m,j in n) Y[i][j]==X[i][j]*MC+MD;
}
I get an error in a OPL model when I use that constraint:
forall (j1,j2 in p: row[j1]==row[j2] && j1<j2)
where row is a variable:
dvar int row [p];
The error is like this:
Decision variable row not allowed.
I don't know why this is not possible, but how can fix this problem?
The condition is slicing should be bound and should not contain any decision variable. You should rely on logical constraints:
range p=1..4;
dvar int row[p] in p;
subject to
{
forall(j1,j2 in p) ((row[j1]==row[j2] ) => (row[j1]>=2));
}
This works fine.
In my model, first I calculate the number of ports in which ship drop the cargo
forall(i in 1..N,j in k+1..N)
z[i][j]==sum(z in k..N-1)z*dr[i][j][z];
Then I want to use this number as the index of "t",in the form of
t[z[i][j]]
I'm faced with
error:5002 q1 is not convex
How I can solve this problem?
How to use a decision variable as an index with CPLEX ?
range r=1..5;
float value[r]=[2,3,4.5,1,0];
dvar int i in 1..5;
maximize sum(k in r) value[k]*(k==i);
subject to
{
}
execute
{
writeln("i=",i);
}