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
Related
I am trying to formulate a linear program that will assign different number of employees to start in different days. Each group of employees starting on a day will get two days off during the week. However, the schedule is unknown. For example, employees starting Monday can be off any two days in the week. Since the number that will start on day (i) is unknown and whether they will have a day off or not is unknown, I will have the product of two decision variables - one is an integer xi (employees starting on day i) and a binary variable yij (whether the employees starting on day i have a day off on day j).
I am done with formulation and here it is:
Decision variables 1: xi (employees starting on day i)
Decision variables 2: yij (1 if employees starting on day i are working on day j, or 0 if employees starting on day i are off on day j)
Objective function:
Minimize total employees-- sum (i in 1..7) xi
Subject to:
xi*yij >= Requiredj (the number of available workers on day j have to satisfy the demand on day j)
I am trying to code this on CPLEX but i dont know how to make xi*yij linear and write the code....can anyone please help me?
Thank you.
In How to with OPL How to multiply a decision variable by a boolean decision variable in CPLEX ?
// suppose we want b * x <= 7
dvar int x in 2..10;
dvar boolean b;
dvar int bx;
maximize x;
subject to
{
// Linearization
bx<=7;
2*b<=bx;
bx<=10*b;
bx<=x-2*(1-b);
bx>=x-10*(1-b);
// if we use CP we could write directly
// b*x<=7
// or rely on logical constraints within CPLEX
// (b==1) => (bx==x);
// (b==0) => (bx==0);
}
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 ampl and I am trying to formulate a mathematical model, and I need a double summation
I am trying to write :
600+ (the total capacity of the generators installed until year j) >= (demand of year j)
the capacity is being denoted by x[i,j], i in GENERATOR, j in YEAR.
600+ \sum_{j}(\sum_{i} x[i,j])>=demand[j]
subject to dem{j in YEAR}:600+sum{i in GENERATOR, j in YEAR}x[i,j]>=demand[j];
I tried this way but it gives me an error. Please help me to write a double summation.
If I've understood your requirements, what you need is something like this:
subject to dem{j_current in YEAR}:
600 + sum{i in GENERATOR, j_past in YEAR: j_past <= j_current}x[i,j_past]
>= demand[j_current];
(adds all x[i,j] UP TO current year)
or this:
subject to dem{j_current in YEAR}:
600 + sum{i in GENERATOR}x[i,j_current]
>= demand[j_current];
(adds all x[i,j] AT current year)
The way you had it will get you error messages because each constraint created by that statement already has a specific value of j so you can't then iterate over different values of j within the constraint.
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);
}
I'm very new to programming and I have no idea where to start, just looking for some help with this, I know its very simple but I'm clueless, thanks for the help!
So this is the code I have:
NSInteger sum = 0;
for (int a = 1; a < 500; a++) {
sum += (a * 2 - 1); }
NSLog(#"The sum of all the odd numbers within the range = %ld",(long)sum);
but I'm getting a answer of 249,001, but it should be 250,000
Appreciate the help!
Your immediate problem is that you're missing a term in the sum: your output differs from the actual answer by 999. This ought to motivate you to write a <= 500 instead as the stopping condition in the for loop.
But, in reality, you would not use a for loop for this as there is an alternative that's much cheaper computationally speaking.
Note that this is an arithmetic progression and there is therefore a closed-form solution to this. That is, you can get the answer out in O(1) rather than by using a loop which would be O(n); i.e. the compute time grows linearly with the number of terms that you want.
Recognising that there are 500 odd numbers in your range, you can use
n * (2 * a + (n - 1) * d) / 2
to compute this. In your case, n is 500. d (the difference between the terms) is 2. a (the first term) is 1.
See https://en.wikipedia.org/wiki/Arithmetic_progression