declare default value for variables in ampl - ampl

Now I have variable set x in my ampl model, and I want to define a default value for each x.
set N := 1..10;
var x {i in N} default 0;
If I build the model like this, the initial value of all x would be set as 0. How can I set different value for each x, like [0,0,0,0,0,1,1,1,1,1]?

If you just want to change some parts of x to a non-default value, this is easy to do. For example:
var x{i in N} default 0;
let{i in 6..10} x[i] := 1;
I'm not aware of any way to have more than one default value for different elements of a var.

Related

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.

Is there a way to make a specific variable from a set an integer in Ampl?

set P;
set K;
set I:= {i in K};
set J:={j in P};
param C {P} >=0;
param A {K,P} >=0;
param B {K} >=0;
var X{j in P} >=0;
P consists of 4 set values, namely sweatshirt-f, sweatshirtB/F, tshirtf and tshirtBF, but I would like sweatshirt-f only to return an integer:
maximize f: sum{j in P} C[j]*X[j];
s.t. Constraint {i in K}:
sum{j in P} A[i,j]*X[j]<=B[i];
Option 1: declare all of them as integer, then relax integer constraints for the others:
var X{j in P} >=0 integer;
let {i in P: i <> "sweatshirt-f"} X[i].relax := 1;
Option 2: declare a dummy variable as integer, then constrain relevant value to match it:
var X{j in P} >=0;
var int_dummy integer;
s.t. force_integer: X["sweatshirt-f"] = int_dummy;
I'm not able to test those right now, so you may have to do a little debugging, but that should get you in the neighbourhood of a solution.

AHDL dff resets to it default value

I'm doing variable frequency clock on AHDL. Algoritm is: one counter (trigger) counts from 0 to x, and when it reaches x - we have pulse. I have another trigger which is used to store that X. Also I have two inputs plus and minus which are used to change frequency (increase or decrease X value).
And I have following code:
constant maximum = 9;
constant minimum = 1;
constant default = 5;
subdesign generator(
plus, minus, clk: input;
pulse, level[3..0], curr_val[3..0]: output;
)
variable
level[3..0]: dff;
curr_val[3..0]: dff;
begin
defaults
level[].d = default; % load 5 as default X value %
end defaults;
level[].clk = clk;
curr_val[].clk = clk;
pulse = (curr_val[] == level[]); % if main counter reached X - send one pulsation %
% main counter %
if curr_val[] < level[] then
curr_val[] = curr_val[] + 1;
elsif curr_val[] == level[] then
curr_val[] = 0;
end if;
% buttons %
if plus then
if (level[].q > minimum) then % if X == maximum ignore button %
level[].d = level[].q - 1;
end if;
end if;
if minus then
if (level[].q < maximum) then
level[].d = level[].q + 1;
end if;
end if;
end;
The problem is - after one tick when I change X (level[]) value - it goes back to default value. Am I missing something?
Syntax highlighting is wrong since wrong tag. % text % is commentary.
Found the problem.
The defaults block works everytime if value was not set. So, if you want to store same value you should set it at every time.

AMPL Variables in subscripts are not yet allowed

I really hope someone can help with this...
This is what i have in the .mod file
set I := 1..10;
set J := 1..10;
set K := 1..2;
set W := 1..20;
param v{K, W};
param d{I, J};
var x1, integer;
var y1, integer;
var x2, integer;
var y2, integer;
var assist{W}, binary;
and this is the code generating error:
minimize nome: sum{w in W} (if (assist[w] == 0) then
(if (x1 >= v[1,w]) then
(if (y1 >= v[2,w]) then
(d[x1 - v[1,w],y1 - v[2,w]])....
where the error regards the last line and says:
Variables in subscripts are not yet allowed.
context: (d[x1 - v[1,w],y1 - >>> v[2,w]] <<< )
this is one of constraints (others are just the same):
subject to rangex1:
x1 > 0 && x1 <= 10;
As the error message says, you can't use decision variables within a subscript in AMPL. In this case x1 and y1 are decision variables, so d[x1 - v[1,w],y1 - v[2,w]] is not allowed. You'll need to reformulate the problem in a way that avoids this issue.

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

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.