enter image description here
Hi,
In my cplex code, I have to consider an index summation which is a parameters like what I showed in the picture. Can you help me how I can code it in cplex?
You can use sum:
range T0=5..6;
int v[i in 1..10]=i;
int su=sum(i in T0) i;
assert su==11;
execute
{
writeln(su);
}
which gives
11
Related
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
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);
}
I'm trying to calculate the value of n that solves the problem below. I am not exactly sure where I am messing up. I tried using a do while loop also, but I am having trouble figuring out the logic error. Could anyone assist?
If S = √ (6*( 1+1/2^2+1/3^2 +1/4^2 + 1/5^2 + ... ) ) = (pi^2)/6, after how many terms will the sum be equal to PI to 6 decimal places. PI to 6 decimal places is 3.141592. The relevant part of my code is shown below:
double s = 0;
for(int n=1;abs(sqrt(6*s) - 3.141592) >= pow(10,-6);n++) {
s += (1/(pow(n,2)));
NSLog(#"%i",n);
}
int abs(int i)
computes the absolute value of an integer. Therefore in
abs(sqrt(6*s) - 3.141592)
the floating point number sqrt(6*s) - 3.141592 is converted to an int
first, which gives zero as soon as the absolute value of this number is less than one.
You want to use fabs() instead.
I want to get all combination of numbers to sum.
My input is X that is variable and X is the number of numbers,
For example:
X=4 means we have 1,2,3,4
x=100 means 1,2,3,4,5,,98,99,100
Now I want to get {(1,2)(1,3)(1,4)(2,3)(2,4)(3,4),(1,2,3)(1,3,4)…}
We can’t have repetitive sequence like (1,2)(2,1)(1,2,3)(1,3,2)
I want to get all combinations that these numbers can be sum without repetitive sequence.
Can anyone help me to find it’s algorithm? I must code it in VBA of Excel using for loops
the "algorithm" is solved from Maths and given by the combinations of X by 2:
X!/((X-2)!*2!)=X!/((X-2)!*2)
(note: Just in case... "!" is the factorial...)
now if you want to use for-loop to calculate factorial (written in c):
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}