ZIMPL: Variables with two indices - scip

i have started to use the zpl.format to solve a linear program with scip.
In my linear programm there are variables with 2 indices. Can I rebuild this in the zpl.format like var b[x,t] or something similar?

You index with a two dimension set.
Set I := { <1,2>, <2,3>, <5,7> };
var b[I];
for in I do ...

Related

OpenCL Kernel and traditional loops

I'm studying OpenCL and I don't understand the relationship between traditional loop in a C/C++ code and kernel code.
Just for be clear a situation like that:
So my question is: In the traditional loops I have n variable as my boundary while in kernel code I don't have it but I have get_global_id(0) that indicates the memory scope of my array, this means that I start from 0, and iterate until get_global_id matches with the maximum size of the array, n in this case? Or is something different?
Because in this other example I don't know how to write the correspond kernel code
I hope my question is clear because I'm not very well in english, sorry.
Thanks in advance for the help, if there are problems let me know!
An OpenCL kernel is coded like a single iteration of a for-loop, but all iterations are run in parallel with random order.
Consider this vector addition example in C++, where for i=0..N-1, you add each element of the vectors one after the other:
for(int i=0; i<N; i++) { // loop index i
C[i] = A[i]+B[i]; // compute one after the other
}
In OpenCL, the vector addition looks like the inside of this for-loop, but as a function with the kernel keyword and all vectors as parameters:
kernel void add_kernel(const global float* A, const global float* B, global float* C) {
const int i = get_global_id(0);
C[i] = A[i]+B[i]; // compute all loop indices i in parallel
}
You might be wondering: Where is N? You give N to the kernel on the C++ side as its "global range", so the kernel knows how much elements i to calculate in parallel.
Because in the OpenCL kernel every iteration runs in parallel, there must not be any data dependencies from one iteration to the next; otherwise you have to use a double buffer (only read from one buffer and only write to the other). In your second example with A[i] = B[i-1]+B[i]+B[i+1] you do exactly that: only read from B, only write to A. The implementation with periodic boundaries can be done branch-less, see here.

How to use PySCIPOpt for feasibility-only problem

I have used CVXPY and some of its LP solvers to determine whether a solution to an A*x <= b problem is feasible, and now I would like to try PySCIPOpt. I could not find an example of this in the docs, and I'm having trouble figuring out the right syntax. With CVXPY the code is simply:
def do_cvxpy(A, b, solver):
x = cvxpy.Variable(A.shape[1])
constraints = [A#x <= b] #The # denotes matrix multiplication in CVXPY
obj = cvxpy.Minimize(0)
prob = cvxpy.Problem(obj, constraints)
prob.solve(solver=solver)
return prob.status
I think with PySCIPOpt one cannot use matrix notation as above, but must treat vectors and matrices as collections of scalar variables, each of which has to be added individually, so I tried this:
def do_scip(A, b):
model = Model("XYZ")
x = {}
for i in range(A.shape[1]):
x[i] = model.addVar(vtype="C", name="x(%s)" % i)
model.setObjective(0) #Is this right for a feasibility-only problem?
model.addCons(A*x <= b) #This is certainly the wrong syntax
model.optimize()
return model.getStatus()
Could anyone please help me out with the correct form for the constraint in addCons() for this kind of problem, and confirm that an acceptable way to ask whether a solution is feasible is to simply pass 0 as the objective?
I'm still not positive about the setObjective(0), but at least I can get the code to run without errors by "unpacking" the A matrix and the b vector and adding each element as a constraint:
for i in range(ncols):
for j in range(nrows):
model.addCons(A[j,i]*x[i] <= b[i])
I also discovered that CVXPY actually has an interface to SCIP, but it gives me an error when I try to use it:
getSolObjVal cannot only be called in stage SOLVING without a valid solution
which seems to suggest that the interface cannot be used for feasibility-only problems.

Is there a way to check feasibilty for given values of decision variables in CPLEX?

right now I am trying to implement a particle swarm optimization algorithm in CPLEX to solve a Vehicle Routing Problem with a large number of customers.
Firstly, I wrote an optimization model in OPL that is now able to get solutions for smaller instances. In order to now also be able to solve problems with a bigger number of customers, I want to do it using a heuristic I have from a paper (Particle Swarm Optimization).
For this, I am implementing each step of the algorithm in a main-block (control flow) in ILOG Script.
In each iteration, the algorithm proposes a solution ( = values for the decision variables) that now needs to be checked if it is feasible and what the solution value is.
In the next iteration, the solution then is tried to be improved.
This is repeated until a certain number of iterations is reached.
I already got the algorithm to work. But now I don't know how I can do the feasibility check.
I basically now have values for the decision variables that I need to run in the model to check if all constraints hold for this solution.
How does this work in CPLEX using control flow?
Obviously, I know how to trigger OPL to generate the model and run the CPLEX solver to get a solution for it, but how does it work when you want to give CPLEX the values for the decision variables and just want it to test the feasibility and not do any optimization?
In Making Optimization Simple you could check "check feasibility" that relies on fixed start.
int nbKids=300;
// a tuple is like a struct in C, a class in C++ or a record in Pascal
tuple bus
{
key int nbSeats;
float cost;
}
// This is a tuple set
{bus} pricebuses={<40,500>,<30,400>};
// asserts help make sure data is fine
assert forall(b in pricebuses) b.nbSeats>0;assert forall(b in pricebuses) b.cost>0;
// solutions we want to test
range options=1..3;
int testSolution[options][pricebuses]=[[5,2],[5,3],[5,4]];
// decision variable array
dvar int+ nbBus[pricebuses];
// objective
minimize
sum(b in pricebuses) b.cost*nbBus[b];
// constraints
subject to
{
sum(b in pricebuses) b.nbSeats*nbBus[b]>=nbKids;
}
float cost=sum(b in pricebuses) b.cost*nbBus[b];
execute DISPLAY_After_SOLVE
{
writeln("The minimum cost is ",cost);
for(var b in pricebuses) writeln(nbBus[b]," buses ",b.nbSeats, " seats");
}
main
{
thisOplModel.generate();
// Test feasibility through fixed start
for(var o in thisOplModel.options)
{
for(var b in thisOplModel.pricebuses)
{
thisOplModel.nbBus[b].UB=thisOplModel.testSolution[o][b];
thisOplModel.nbBus[b].LB=thisOplModel.testSolution[o][b];
}
if (cplex.solve())
{
write(thisOplModel.testSolution[o]," is feasible");
writeln(" and the cost is ",cplex.getObjValue());
}
else writeln(thisOplModel.testSolution[o]," is not feasible");
}
}
/*
which gives
[5 2] is not feasible
[5 3] is not feasible
[5 4] is feasible and the cost is 4100
*/
but if you prefer asserts you also can rely on that.

How can I do elementwise operation on parameter vectors in AMPL

In MATLAB, if I have two vectors a=[a_1,..,a_n], b=[b_1,..,b_n], I can obtain another vector c = [a_1/b_1,..,a_n/b_n] by a./b. How can I achieve this in AMPL?
You can use something like this:
set S := {1,2,3};
param p{S};
var x{S};
var y{i in S} = x[i]*p[i];
or alternately:
set S := {1,2,3};
param p{S};
var x{S};
var y{S};
s.t. c1{i in S}: y[i] = x[i]*p[i];
However, your ability to do this may be limited by the constraints supported by your solver, e.g. if you define a relationship that implies a nonlinear constraint while using a nonlinear solver.

Functions in GAMS

In GAMS, I need to put an equation in a function. Look at this example
$ontext
minimize 5x^2+3x-12
subject to
4x+2x^2<10
$offtext
variable OF, x;
equation obj,cons;
obj .. OF=E=5*x*x+3*x-12;
cons .. 4*x+2*x*x=L=10;
model this /all/;
solve this using NLP maximizing OF;
display x.l,OF.l,cons.l;
The solution is
VARIABLE x.L = 1.449
VARIABLE OF.L = 2.854
EQUATION cons.L = 10.000
Now, let say I want to define the objective as a function:
f(x)=5x^2+3x-12
and replace the first constraint with
obj .. OF=E=f(x);
How should I do this?
Thank you!
I am not sure, if I understand your motivation to do this, but what you could do, is using a macro like this:
variable OF, x;
equation obj,cons;
$macro f(x) 5*x*x+3*x-12
obj .. OF=E=f(x);
cons .. 4*x+2*x*x=L=10;
model this /all/;
solve this using NLP maximizing OF;
display x.l,OF.l,cons.l;