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.
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 am trying to learn Verilog and I have this simple code
module division (
output reg [14:0] A,
input [14:0] D);
reg[4:0] i;
always #(*) begin
for (i = 14; i >= 0; i = i-1) begin
A[0] = D[i];
end
end
endmodule
This returns the error : Index out of range for D.. I have no idea why, since D is declared on that interval. Can you please help me?
I know the code might not make any sense, but I only included the relevant part to the issue.
You have declared i as unsigned, so the expression i >= 0 will always be true.
When i reaches 0, the next iteration is 5'b11111, which is out of range. You should declare i as an integer or add the signed keyword.
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 having hard time regarding how to model a constraint. I'm trying to solve a transportation problem and one of the constraints is as follows:
t[s[i]] >= t[i] + travelTime (i, s[i])
which t is the time at which node i is visited and s is the successor variable of node i ( t and s are variables).
I also have a travel time matrix :
int time = travelTime(int origin, int destination)
which defines the travel time between pair of nodes.
I have defined the constraint as below:
for (int i : set){
IloIntExpr expr = model.linearIntExpr();
expr = model.sum(model.element(t, s[i]), model.prod(t[i], -1));
IloIntExpr next = model.element(range(0, nbNodes), s[i]);
model.addGe(expr, travelTime(i, next));
}
***range function generates [0,1, ... ,nbNodes] and [size(s) = size(t) = nbNodes]
now the problem is that next is returning IloIntExpr and I want it to be int to pass it to travelTime(int, int), and also IloIntExpr is not castable to int.
What can be the possible solution for my problem.
I really appreciate any help in this regard.
I am new in GLPK. This is some of my code:
set I := setof{(i,r,p,d) in T} i;
var Y{I,I}, binary;
s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;
I want to have only six values in Y that are 1. Can anyone tell me how to do it in proper way? Because s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;always produces an error.
Thank you.
This is just a syntax problem. The constraint should look like the following:
s.t. c1: sum{i in I, j in I}(Y[i,j]) = 6;
The first brackets after the name of your constraints imply that the constraint is applied to every single [I, I]. What you want is to fix the sum of all Y in your problem, so you need the constraint to only apply once to your problem (so delete these brackets).
In the sum-syntax don't put the variable you want to sum in the brackets, they belong after them. Inside the brackets you can define the range of the sum.