Q1 is not convex error while using decision variable in index - indexing

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);
}

Related

index of summation in cplex

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

How do I run a simulation experiment (iterative procedure) in CPLEX?

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

CPLEX - adding min in constraints & how to write sum with different values

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

Given a set of points or vectors, find the set of N points that are closest to each other

I have for example 100 vectors each has a dimension of 12. I would like to find for example 8 vectors that are closest to each other. In other words, the top 8 matching vectors. I may use Euclidean or Manhattan distance as a measure metric to quantify the similarity between the vectors. An initial thinking reveals that I could formulate this problem as a 0-1 nonlinear programming which is NP hard to solve as the number of vectors increases. I also went through the k-means clustering algorithm but it does not use the Euclidean distance as a measure. Any idea which algorithm can target this problem. The reason I am asking is because I am sure this problem was addressed in the literature but I could not find such algorithm.
This can actually be formulated as a quadratic or linear integer program:
The quadratic model can look like:
min sum((i,j), x(i)*x(j)*dist(i,j))
sum(i, x(i)) = 8
x(i) ∈ {0,1}
The linear MIP model is a variant of the quadratic model:
min sum((i,j), y(i,j)*dist(i,j))
sum(i, x(i)) = 8
y(i,j) >= x(i)+x(j)-1
x(i) ∈ {0,1}
y(i,j) ∈ [0,1]
We can refine things by only considering distances with i < j (essentially no double counting).
Instead of summing over all distances, we can also minimize the maximum distance in our selected points:
min z
z >= y(i,j)*dist(i,j) for all i<j
sum(i, x(i)) = 8
y(i,j) >= x(i)+x(j)-1
x(i) ∈ {0,1}
y(i,j) ∈ [0,1]
These models are independent of what metric or dimensionality you use. Whether using Euclidean or Manhattan distances or whether you normalize or use weights, the models stay the same. The same thing for whether you have low- or high-dimensional data. These models just need a distance matrix.
The MIP models solve quite fast with Gurobi. With random data using your sizes (select 8 points from 100 using 12-dimensional coordinates), these models take 50 and 9 seconds for the linear sum and max model to find proven optimal solutions. Some more details are here.
For a 2d data set we can plot the results:
I agree with Jérôme Richard that the distance measure needs to be clarified. Is it the sum of the Euclidean lengths of the 56 line segments connecting pairs of the eight selected points, or the maximum distance between any pair, or what?
That said, if "similarity" is the goal, we might ask which eight points are contained in the smallest possible ball or box. Finding the smallest ball is a convex quadratic problem and finding the smallest hyperrectangle has a nonconvex objective (I think), but finding the vectors that fit in the smallest hypercube is easy to formulate as a mixed integer linear program.
with OPL CPLEX you can solve this both with Mathematical Programming and Constraint Programming.
Math prog:
int scale=1000000;
int nbdim=2;
int m=8;
int n=50;
range dim=1..nbdim;
range points=1..n;
float x[p in points][d in dim]=rand(scale)/scale;
float distance[p1 in points][p2 in points]=sqrt(sum(d in dim)((x[p1][d]-x[p2][d])^2));
// Should we keep that point ?
dvar boolean which[points];
dexpr float maxDist=max(ordered i,j in points) distance[i][j]*((which[i]==1) && (which[j]==1));
minimize maxDist;
subject to
{
sum (p in points) which[p]==m;
}
{int} chosenPoints={k | k in points:which[k]==1};
Constraint Programming:
using CP;
int scale=1000000;
int nbdim=2;
int m=8;
int n=50;
range dim=1..nbdim;
range points=1..n;
float x[p in points][d in dim]=rand(scale)/scale;
float distance[p1 in points][p2 in points]=sqrt(sum(d in dim)((x[p1][d]-x[p2][d])^2));
// which point for as the m th point ?
dvar int which[1..m] in points;
dexpr float maxDist=max(ordered i,j in 1..m) distance[which[i]][which[j]];
minimize maxDist;
subject to
{
allDifferent(which);
}
{int} chosenPoints={which[k] | k in 1..m};

Error when using a conditional forall in OPL

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.