GLPK: set expression following default must have dimension 2 rather than 1 - ampl

I have an AMPL model file that I'm working to convert to GLPK. It begins:
param n; # The number of nodes in the graph
set V := {1 .. n}; # The set of vertices in the graph
set E within V cross V; # The set of edges in the graph
set NE within V cross V := {i in V, j in V: i < j} diff E;
set FIXED within V cross V default {}; # The set of demand pairs with fixed flow
When running this, I get the following error:
_test.mod:5: set expression following default must have dimension 2 rather than 1
Context: : i < j } diff E ; set FIXED within V cross V default { } ;
MathProg model processing error
This must be a syntactic difference between MathProg and its superset, AMPL—running the code in AMPL works perfectly. How does one express a 2D empty set in MathProg?

Alright, the hack of a solution is this:
set FIXED within V cross V default {i in V, j in V: 1 < 0};
Put an obviously false condition. It'll have the dimensionality you want and still be empty.

Related

Axiomatic Semantics - How to calculate a weakest precondition of a program

Assuming the post-condition, how can I compute the weakest pre-condition of a program containing two statements?
For example :
a=x;
y = 0
{x = y + a}
Another example:
y = x;
y = x + x + y
{y = 3x ^ z> 0}
I tried to solve them but both questions resulted in pre-conditions or post-condition that are identical to the statement and I don't know if this is valid.
for example, the precondition of the last statement is "y=x" , thus it is the post condition of the preceding statement which is " y=x" as well
You can apply the rules of Hoare Logic here. Specifically, for the examples you have, you only need the rule for assignment:
{ P[E/x] } x = E { P }
Here, P[E/x] means take P and substitute (i.e. replace) all occurrences of x with E. For example, if P is x == 0 then P[0/x] gives 0 == 0.
To calculate the weakest precondition, you start from the end and work backwards. For your first example, we start with the last statement:
{ ??? } y = 0 { x == y + a }
The goal is to determine something suitable for ???. Applying our rule for assignment above, we can see that this is a solution:
{ x == 0 + a } y = 0 { x == y + a }
We can further simplify this to { x == a }. Then, we move on to address the statement before y = 0, and so on.

z3py: Symbolic expressions cannot be cast to concrete Boolean values

I'm having troubles to define the objective fucntion in a SMT problem with z3py.
Long story, short, I have to optimize the placing of smaller blocks inside a board that has fixed width but variable heigth.
I have an array of coordinates (represented by an array of integers of length 2) and a list of integers (representing the heigth of the block to place).
# [x,y] list of integer variables
P = [[Int("x_%s" % (i + 1)), Int("y_%s" % (i + 1))]
for i in range(blocks)]
y = [int(b) for a, b in data[2:]]
I defined the objective function like this:
obj= Int(max([P[i][1] + y[i] for i in range(blocks)]))
It calculates the max height of the board given the starting coordinate of the blocks and their heights.
I know it could be better, but I think the problem would be the same even with a different definition.
Anyway, if I run my code, the following error occurs on the line of the objective function:
" raise Z3Exception("Symbolic expressions cannot be cast to concrete Boolean values.") "
While debugging I've seen that is P[i][1] that gives an error and I think it's because the program reads "y_i + 3" (for example) and they can't be added togheter.
Point is: it's obvious that the objective function depends on the variables of the problem, so how can I get rid of this error? Is there another place where I should define the objective function so it waits to have the P array instantiated before doing anything?
Full code:
from z3 import *
from math import ceil
width = 8
blocks = 4
x = [3,3,5,5]
y = [3,5,3,5]
height = ceil(sum([x[i] * y[i] for i in range(blocks)]) / width) + 1
# [blocks x 2] list of integer variables
P = [[Int("x_%s" % (i + 1)), Int("y_%s" % (i + 1))]
for i in range(blocks)]
# value/ domain constraint
values = [And(0 <= P[i][0], P[i][0] <= width - 1, 0 <= P[i][1], P[i][1] <= height - 1)
for i in range(blocks)]
obj = Int(max([P[i][1] + y[i] for i in range(blocks)]))
board_problem = values # other constraints I've not included for brevity
o = Optimize()
o.add(board_problem)
o.minimize(obj)
if (o.check == 'unsat'):
print("The problem is unsatisfiable")
else:
print("Solved")
The problem here is that you're calling Python's max on symbolic values, which is not designed to work for symbolic expressions. Instead, define a symbolic version of max and use that:
# Return maximum of a vector; error if empty
def symMax(vs):
m = vs[0]
for v in vs[1:]:
m = If(v > m, v, m)
return m
obj = symMax([P[i][1] + y[i] for i in range(blocks)])
With this change your program will go through and print Solved when run.

How to fix "submatrix incorrectly defined" in Scilab?

I am trying to find three parameters (a, b, c) to fit my experimental data using ODE solver and optimization by least squares using Scilab in-built functions.
However, I keep having the message "submatrix incorrectly defined" at line "y_exp(:,1) = [0.135 ..."
When I try another series of data (t, yexp) such as the one used in the original template I get no error messages. The template I use was found here: https://wiki.scilab.org/Non%20linear%20optimization%20for%20parameter%20fitting%20example
function dy = myModel ( t , y , a , b, c )
// The right-hand side of the Ordinary Differential Equation.
dy(1) = -a*y(1) - b*y(1)*y(2)
dy(2) = a*y(1) - b*y(1)*y(2) - c*y(2)
endfunction
function f = myDifferences ( k )
// Returns the difference between the simulated differential
// equation and the experimental data.
global MYDATA
t = MYDATA.t
y_exp = MYDATA.y_exp
a = k(1)
b = k(2)
c = k(3)
y0 = y_exp(1,:)
t0 = 0
y_calc=ode(y0',t0,t,list(myModel,a,b,c))
diffmat = y_calc' - y_exp
// Make a column vector
f = diffmat(:)
MYDATA.funeval = MYDATA.funeval+ 1
endfunction
// Experimental data
t = [0,20,30,45,75,105,135,180,240]';
y_exp(:,1) =
[0.135,0.0924,0.067,0.0527,0.0363,0.02445,0.01668,0.012,0.009]';
y_exp(:,2) =
[0,0.00918,0.0132,0.01835,0.0261,0.03215,0.0366,0.0393,0.0401]';
// Store data for future use
global MYDATA;
MYDATA.t = t;
MYDATA.y_exp = y_exp;
MYDATA.funeval = 0;
function val = L_Squares ( k )
// Computes the sum of squares of the differences.
f = myDifferences ( k )
val = sum(f.^2)
endfunction
// Initial guess
a = 0;
b = 0;
c = 0;
x0 = [a;b;c];
[fopt ,xopt]=leastsq(myDifferences, x0)
Does anyone know how to approach this problem?
Just rewrite lines 28,29 as
y_exp = [0.135,0.0924,0.067,0.0527,0.0363,0.02445,0.01668,0.012,0.009
0,0.00918,0.0132,0.01835,0.0261,0.03215,0.0366,0.0393,0.0401]';
or insert a clear at line 1 (you may have defined y_exp before with a different size).

CPLEX max function over a range

I have a non-linear constraint in the form of max_{k in V, j in F^o: o > d} {U_{jk} - U_{ik} > 0 for all i in F^d. The set V denotes a fleet of vehicles, while F^o represent customers of a certain type and F^i represents customers of a certain type. How do I implement a max function that will be able to compute this in CPLEX, maxl() and IloMAx() does not seem to work
If I understood correctly your max function is to return the maximum value, among the positive values, of U_jk - U_ik for all k in V and j in F^o such that o > d. If that is correct all you need is a couple of loops, one on the k and another one on the j. For each (k, j) pair you need then to verify all the conditions:
Exceeding the higher value obtained; while also
Having a positive value for U_jk - U_ik.
I'll assume you will have the o > d condition checked outside the max function to simplify it. I propose you to do it as follows:
IloInt myMaxFunction(IloIntArray V, IloIntArray Fo, Ilo2IntArray U, IloInt i) {
IloInt jMax;// j index of the maximum value
IloInt kMax;// k index of the maximum value
IloInt maxVal = -IloInfinity;// maximum value
IloInt Difference;
for (IloInt k = 0; k < V.getSize(); k++) {
for (IloInt j = 0; j < Fo.getSize(); j++) {
Difference = U[F0[j]][V[k]] - U[i][V[k]];
if ((Difference > 0) && (Difference > maxVal)) {
jMax = j;
kMax = k;
maxVal = Difference;
}
}
}
return maxVal;
}
You will be entering the two linear arrays, the first containing the sets of vehicles, the second one containing the customers farther than a distance d to the depot I assume. The third parameter is a bidimensional set of integers you can define with: typedef IloArray<IloIntArray> Ilo2IntArray. Finally, you will also need the customer i as input.
For all (k, j) pair of elements such that k is a vehicle in V and j is a customer on the set F^o you will compute the difference U_jk - U_ik, verify simultaneously the aforementioned conditions. In such case, you will update the indexes and maximal value and continue.
Notice, that maxVal must be initialized to a value that will be improved the first time the conditions are verified. The best/higher value will be returned by the function.
One is glad to be of service...
Y

mathematica how to eliminate variable name after solving

I am trying to solve equations and output the derivations. I have no problem solving for the derivation but when I try to output the derivation it always comes with the variable name, examples:
{{w0fromxfun1[x] -> (8.46504 miu^(4/9) qi^(4/9) (-1. x + xf)^(4/9))/
Ep^(4/9)}}
{{uave[x] -> (0.382926 Ep^(1/4) qi^(3/4))/(
hf (miu (-1. x + xf))^(1/4))}}
See this link for a better view
My code for solving the derivation is here:
equ5 = uave[x] == ((qi Ep^(1/3))/(
3.59623 hf (hf miu (xf - x))^(1/3)))^(3/4);
diffequsol2 = PowerExpand[FullSimplify[DSolve[equ5, uave[x], x]]] // N;
waveofthemaxes =
FullSimplify[
1/xf Integrate[w0fromxfun[x], {x, 0, xf}, Assumptions -> trivial]];
equ6 = w0fromxfun1[
x] == ((4.5788*(hf miu qi/((\[Pi]/4) hf ) (-x + xf))^(1/3))/Ep^(
1/3))^(4/3);
diffequsol1 =
PowerExpand[FullSimplify[DSolve[equ6, w0fromxfun1[x], x]]] // N
See here for a better view of the code
I don't want the variable names in front of the derivations, I tried Fullsimplify and simplify but don't work.