In AMPL I have a set of variables x[e], for some calculations I need a binary variable w[e] which equals 1 when x[e] > 0 and 0 if x[e] = 0. I tried a lot of stuff to make this constraint, but I failed to come up with something. Is this possible?
I have solved your problem the following way:
var u binary;
this is our binary variable that will be 0 or 1.
Then we put following constraint:
subject to U_constraint :
x <= 999999 * u;
Now when x = 0 then AMPL will make u = 0 and when x != 0 obviously u = 1.
Related
I set variable x = 0. But under a while statement I changed it to x+=1. I'm trying to account for when x is in between 1 and 4 while also accounting for when x is over 4.
I have a fixed input and output for my simulink embeded function.
However I would like to compute a variable size element inside the function, (only used for calculation).
Therefore I would prefer not to declare the block as receiving or sending dynamic size signal. (or using coder.varsize)
ex:
K = find( p_var(:) == 0 ); % p_var is a vector containing some zeros
p_var(K) = []; % p_var is a therefore a varsize vector
% the variability is something I need for reason
n = length(p_var) % n is dynamic
M = ones(10,n) % M & L are also dynamic
L = ones(n,50)
G = M*L; % G is fixed of size [10*50]
Here the variable G is always fixed... but I have this type of error :
Dimension 2 is fixed on the left-hand side but varies on the right ([1 x 9] ~= [1 x :?])
Thank you for your time.
You have to define an upper bound for the size of p_var. This can be done in a couple of ways, such as using coder.varsize as shown below.
A couple of other things to note:
If p_var is an input to your function you cannot change its size,but would need a temporary variable as shown below.
You most likely do not want to use find as you have done, but should use logical indexing instead.
function G = fcn(u)
p_var = u;
coder.varsize('p_var', [10,1]);
p_var(p_var(:) == 0) = []; % p_var is therefore a varsize vector
the variability is something I need for reason
n = length(p_var); % n is dynamic
M = ones(10,n); % M & L are also dynamic
L = ones(n,50);
G = M*L; % G is fixed of size [10*50]
I'm working in virtual network embedding, and I'm creating a model with glpk to embed the networks.
I have this following objective function:
minimize cost: sum{(i,j) in VEdges} sum{u in SNodes, v in SNodes} weight[u,v] * fw[i,j,u,v] * secSupEdge[u,v] + sum{u in SNodes, v in SNodes} r[u,v] * secSupEdge[u,v];
Then I have the following two restrictions (among others)
s.t. relConst2{(i,j) in VEdges, u in AllNodes, v in AllNodes}: bwDem[i,j] * phiw[i,j,u,v] >= fw[i,j,u,v];
s.t. linkSecConst0{(i,j) in VEdges, u in SNodes, v in SNodes}: phiw[i,j,u,v] * secDemEdge[i,j] <= secSupEdge[u,v];
"phiw" is a binary variable
"fw" and "r" are variables that take any value >= 0
all the others ("weight", "bwDem", "secDemEdges", "secSupEdge") are just params
I want to relate phiw with fw. When fw > 0, phiw should take the value 1. When fw == 0, phiw should take the value 0.
Normally it does what I want, but sometimes phiw takes the value 1 when fw has the value 0, which is not what I want. This happens because the restrictions are met:
Example 1:
s.t. relConst2: 4 * 1 >= 0
s.t. linkSecConst0: 1 * 2 <= 2
Is there a way to minimize the value of phiw variable but not putting it in the objective function? Or putting it in the objective function but not changing the value of the result neither the value of all other variables?
The question is about minimizing phiw, however, the description of the problem suggests that what you want to do is link the values of phiw and fw, and specifically to have phiw = 1 when fw > 0, and phiw = 0 otherwise (i.e., fw = 0).
I would suggest that you add constraint that directly maps the conditional on fw to the value of phiw, such as:
s.t. LinkConstraint { (i,j) in VEdges, u in AllNodes, v in AllNodes }:
if fw[i,j,u,v] > 0 then 1 else 0 = phiw[i,j,u,v] ;
I have a variable called Rest defined as:
var Rest{I,J,T} >= 0;
where T is the set of time periods and I and J the arcs. I need to define that every value for I and J where T = 0 must be 0. I is the set of supply nodes, and J the set of demand nodes.
I've tried:
let Rest[*,*,0] default 0;
but it got me syntax error. I tried this in both the .dat and .mod file using both := and :
I also tried to put this in the .dat file
var Rest default 0:=
[*,*,0] 1 City1 0;
but it gave me the error
Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11443):
error processing constraint Constraint1[1,'Leveaniemi',1]:
invalid subscript Rest[1,'City1',0]
Thanks in advance!
EDIT:
I now use:
var Rest default 0 :=
[*,*,0] 1 Leveaniemi 0;
which give me the error
Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11438):
error processing constraint Constprocessing commands.
Executing on neos-3.neos-server.org
Error (2) in /opt/ampl/ampl -R amplin
(I am using NEOS server, Gurobi solver). What does this even mean? Also if I declare a Variable Rest like that will it cause every Rest solution to become 0? Or does the compiler interpret it as a start value?
EDIT:
I've tried to implement the solution provided by vitaut. It did not work however, as expressed in the comments below that reply. I figured that since I've defined T as:
set T := 1 2 3 ... 15;
and since I wanted to do a let statement at t = 0, I have to account for that and define Rest as:
var Rest{I,J,TimeT};
where TimeU is T union a set with only a 0 element, i.e. TimeU is interpreted as:
TimeU := 0 1 2 3 ... 15;
With these fixed however, the compiler complains that all my variables and parameters are already defined.
The correct syntax of a let command is
let {i in I, j in J} Rest[i, j, 0] := 0;
However, it will assign starting values to the variables which can change during the optimization process. If you want to make Rest[i, j, 0] always equal to zero, then you should use a constraint instead:
s.t. c{i in I, j in J} Rest[i, j, 0] = 0;
I have been learning Lua and I was wondering if it is allowed to reference two local variables of the same name.
For example, in the following code segment, is the syntax legal (without undefined behavior)?
I ask because it does run, but I cannot seem to figure out what is happening behind the scenes. Is this simply referencing the same x local? Or are there now two local x variables that mess things up behind the scenes. I'd like to know what exactly is happening here and why it is the case.
local x = 5 + 3; -- = 8
local x = 3 - 2; -- = 1
print("x = " .. x); -- x = 1
There are two variables. The second shadows (but does not remove or overwrite) the first.
Sometimes you can still access the earlier definition via a closure.
local x = 5 + 3
local function getX1()
return x
end
local x = 3 - 2
local function getX2()
return x
end
print("x = " .. x); -- x = 1
print("x = " .. getX1()); -- x = 8
print("x = " .. getX2()); -- x = 1
All your local variables have been remembered by Lua :-)
local x = 5 + 3; -- = 8
local x = 3 - 2; -- = 1
local i = 0
repeat
i = i + 1
local name, value = debug.getlocal(1, i)
if name == 'x' then
print(name..' = '..value)
end
until not name
Yes, it is legal. Lua handles local-variable declarations as statements.
Here's an interesting example from the Lua Reference Manual:
Notice that each execution of a local statement defines new local variables. Consider the following example:
a = {}
local x = 20
for i=1,10 do
local y = 0
a[i] = function () y=y+1; return x+y end
end
The loop creates ten closures (that is, ten instances of the anonymous function). Each of these closures uses a different y variable, while all of them share the same x.
In this example, if ignore the returning closure part, there are 10 local variables named y in the same for block.