Why cplex gives me an error "name j2 does not exist"? - indexing

I wrote the following code in cplex but it gives me an error like "name j2 does not exist"
subject to {
forall (r in R)
forall (j1, j2 in J)//con j1 =!da j2
(T[j2][r] >= C[j1] - bigM*(1-Q[j1][j2][r]) - bigM*(2-z[j1][r]-z[j2][r]));
(T[j1][r] >= C[j2] - bigM*Q[j1][j2][r] - bigM*(2-z[j1][r]-z[j2][r]));
}
Can you help me understand where am I wrong? Thank you!

can you try to change
forall (j1, j2 in J)//con j1 =!da j2
(T[j2][r] >= C[j1] - bigM*(1-Q[j1][j2][r]) - bigM*(2-z[j1][r]-z[j2][r]));
(T[j1][r] >= C[j2] - bigM*Q[j1][j2][r] - bigM*(2-z[j1][r]-z[j2][r]));
into
forall (j1, j2 in J)//con j1 =!da j2
{
(T[j2][r] >= C[j1] - bigM*(1-Q[j1][j2][r]) - bigM*(2-z[j1][r]-z[j2][r]));
(T[j1][r] >= C[j2] - bigM*Q[j1][j2][r] - bigM*(2-z[j1][r]-z[j2][r]));
}
?

Related

Dafny question: How to sort the Dutch Flag problem with four colors?

I'm trying to sort the Dutch Flag problem with 4 colors instead of 3, it seems that Dafny does not really verify and I could not fix it as well. This is my code:
datatype Colour = RED | WHITE | PINK | BLUE
method FlagSort(flag: array<Colour>) returns (w:int, p:int, b:int)
ensures 0 <= w <= p <= b < flag.Length
ensures forall i :: 0 <= i < w ==> flag[i] == RED
ensures forall i :: w <= i < p ==> flag[i] == WHITE
ensures forall i :: p <= i < b ==> flag[i] == PINK
ensures forall i :: b <= i < flag.Length ==> flag[i] == BLUE
ensures multiset(flag[..]) == multiset(old(flag[..]))
modifies flag
{
var next := 0;
w, p := 0, 0;
b := flag.Length;
while next <= b
invariant 0 <= w <= p <= next <= b <= flag.Length
invariant forall i :: 0 <= i < w ==> flag[i] == RED
invariant forall i :: w <= i < p ==> flag[i] == WHITE
invariant forall i :: p <= i < next ==> flag[i] == PINK
invariant forall i :: b <= i < flag.Length ==> flag[i] == BLUE
invariant multiset(flag[..]) == multiset(old(flag[..]))
{
if flag[next] == RED {
flag[next], flag[w] := flag[w], flag[next];
w := w + 1;
if p < w {
p := p + 1;
}
if next < w {
next := next + 1;
}
} else if flag[next] == WHITE {
flag[next], flag[p] := flag[p], flag[next];
p := p + 1;
next := next + 1;
} else if flag[next] == PINK {
next := next + 1;
} else if flag[next] == BLUE {
b := b - 1;
flag[next], flag[b] := flag[b], flag[next];
}
}
}
Can anyone help me with this please, thank you!
I don't know the solution to this problem (you might be solving a hard problem !), but here is some relevant advice for each of the three errors Dafny found in your code.
Error 1
When you see this:
flag[next], flag[b] := flag[b], flag[next];
^^^ index out of range
you can add an assertion before like this:
assert 0 <= b;
assert b < |flags|;
flag[next], flag[b] := flag[b], flag[next];
Magically, the index out of range will go away, and in your case, the first assertion will fail. You can then apply verification debugging techniques to move the assertion up..
Error 2
while next <= b
^^^^^ cannot prove termination, try supplying a decreases clause
the problem is that it tried to insert the decrease clause b - next, which should always be decreasing and bounded below by zero. If you make it explicit, and hover the decreases expression, it will tell you "the decreases expression is always bounded below by zero", but you get a new error:
while next <= b
^^^^^ decreases expression might not decrease
decreases b - next
What you can do is add this line at the beginning of your while loop.
ghost var b_saved,next_saved := b, next;
and at the end of your while loop, add the decreases check explicitly:
assert b - next < b_saved-next_saved;
You'll see that now the decreases clause is verified, and you have an error on an assert, on which you can apply regular verification debugging techniques.
Error 3
if flag[next] == RED {
^^^^^^^^^^ index out of range.
Similarly, you can insert the implicit assertions there:
assert 0 <= next < flag.Length;
if flag[next] == RED { // No error there
You'll see an underline on next < flag.Length. What can you do to ensure this? Perhaps change an invariant?

How can I solve exponential equation in Maxima CAS

I have function in Maxima CAS :
f(t) := (2*exp(2*%i*%pi*t) - exp(4*%pi*t*%i))/4;
here:
t is a real number between 0 and 1
function should give a point on the boundary of main cardioid of Mandelbrot set
How can I solve equation :
eq1:c=f(t);
(where c is a complex number)
?
Solve doesn't work
solve( eq1,t);
result is empty list
[]
Result of this equation should give real number t ( internal angle or rotation number ) from complex point c
EDIT: Thx to comment by #JosehDoggie
I can draw initial equation using:
load(draw)$
f(t):=(2*exp(%i*t) - exp(2*t*%i))/4;
draw2d(
key="main cardioid",
nticks=200,
parametric( 0.5*cos(t) - 0.25*cos(2*t), 0.5*sin(t) - 0.25*sin(2*t), t,0,2*%pi),
title="main cardioid of M set "
)$
or
draw2d(polar(abs(exp(t*%i)/2 -exp(2*t*%i)/4),t,0,2*%pi));
Similar image ( cardioid) is here
Edit2:
(%i1) eq1:c = exp(%pi*t*%i)/2 - exp(2*%pi*t*%i)/4;
%i %pi t 2 %i %pi t
%e %e
(%o1) c = ---------- - ------------
2 4
(%i2) solve(eq1,t);
%i log(1 - sqrt(1 - 4 c)) %i log(sqrt(1 - 4 c) + 1)
(%o2) [t = - -------------------------, t = - -------------------------]
%pi %pi
So :
f1(c):=float(cabs( - %i* log(1 - sqrt(1 - 4* c))/%pi));
f2(c):=float(cabs( - %i* log(1 + sqrt(1 - 4* c))/%pi));
but the results are not good.
Edit 3 :
Maybe I shoud start from it.
I have:
complex numbers c ( = boundary of cardioid)
real numbers t ( from 0 to 1 or sometimes from 0 to 2*pi )
function f which computes c from t : c= f(t)
I want to find function which computes t from c: t = g(c)
testing values :
t = 0 , c= 1/4
t = 1/2 , c= -3/4
t = 1/3 , c = c = -0.125 +0.649519052838329*%i
t = 2/5 , c = -0.481762745781211 +0.531656755220025*%i
t = 0.118033988749895 c = 0.346828007859920 +0.088702386914555*%i
t = 0.618033988749895 , c = -0.390540870218399 -0.586787907346969*%i
t = 0.718033988749895 c = 0.130349371041523 -0.587693986342220*%i
load("to_poly_solve") $
e: (2*exp(2*%i*%pi*t) - exp(4*%pi*t*%i))/4 - c $
s: to_poly_solve(e, t) $
s: maplist(lambda([e], rhs(first(e))), s) $ /* unpack arguments of %union */
ratexpand(s);
Outputs
%i log(1 - sqrt(1 - 4 c)) %i log(sqrt(1 - 4 c) + 1)
(%o6) [%z7 - -------------------------, %z9 - -------------------------]
2 %pi 2 %pi

Forall syntax error

I am trying to write the following code but it gives me "syntax error, unexpected forall".
How do I fix this?
maximize sum(i in cargos, j in comps) profit[i]*x[i][j];
subject to {
cons01
forall(i in cargos)
available_wight:
sum(j in comps) x[i][j] <= weight[i];
cons02:
forall (j in comps)
weight_capacity:
sum(i in cargos)x[i][j] <= weight_cap[j];
cons03;
forall (j in comps)
space_capacity;
sum(i in cargos)valume[i]*x[i][j] <= space_cap[j];
remove
cons01
available_wight: is the label of the constraint.
regards

Indexed variable in AMPL

I have the following model with a variable that is a value from a vector (index of p in objective function)
But AMPL displays an error: subscript variables are not yet allowed.
How can I do to implement this kind of addressing in objective function?
Thanks in advance and best regards.
Gabriel
param dimension;
set T:={1..dimension};
set O:={0};
set V:= O union T;
param c{i in V, j in V};
param p{i in V};
set ady{i in V} within V := {j in V : i<>j and c[i,j] <> -1} ;
# Variables
var x{i in V, j in V} binary;
var u{i in V} integer;
# Objective
minimize costo: sum{i in V, j in V} p[u[i]-1] * x[i,j] * c[i,j];
# Constraints
s.t. grado_a {j in V} : sum{i in ady[j] : j <> i} x[i,j] = 1;
s.t. grado_b {i in V} : sum{j in ady[i] : i <> j} x[i,j] = 1;
s.t. origen {i in O} : u[i] = 0;
s.t. sigo_1 {i in T} : u[i] >=1;
s.t. sigo_2 {i in T} : u[i] <= card(V) -1;
s.t. precedencia {i in T, j in T : i <> j} : u[i] - u[j] + 1 <= (card(V) - 1)*(1 - x[i,j]) ;
AMPL doesn't allow variables in subscripts yet. However, the ilogcp driver for AMPL supports the element constraint, for example:
include cp.ampl;
minimize costo:
sum{i in V, j in V} element({v in V} p[v], u[i] - 1) * x[i,j] * c[i,j];
where element({v in V} p[v], u[i] - 1) is equivalent to p[u[i] - 1] and is translated into an IloElement constraint.

Summation on 1 <= i < j < k <= n in GLPK

I have been trying to solve seriation problem by using GNU. But I couldn't write a summation like the following.
param n, integer, >= 3;
set O := 1..n;
param d{i in O,j in O};
var x{i in O,j in O}, binary, i < j;
var v{i in O,j in O,k in O}, binary, i < j < k;
maximize total: sum{i in O,j in O, i<j}(d[i,j] - d[j,i])* x[i,j] + sum{i in O,j in O, i<j}d[j,i];
s.t. tran{i in O,j in O,k in O, i<j<k}: x[i,j] + x[j,i] - x[i,k] + v[i,j,k] = 1;
Thanks
You should use : instead of , in the "such that" clause i < j:
sum{i in O,j in O: i < j} ...
# ^ note ':' here