OPL constraint index - constraints

I am programming an OPL model and do not know how to express the following constraint:
q_t-D_t_T*v_t <=0
Where D_t_T is the sum of all q_t in the set of t to T.
-- Update --
Yes, just q[t] and v[t] are variables. Is the suggestion also working if I defined the range of t as follows:
//parameters
int T=...;
range Perioden=1..T;
My constraint looks then:
forall(t in Perioden)
constraint1:
q[t]- (sum(i in t..T) q[i])*v[t]<=0;
Maybe one more basic question: How do I express the [t-1] in the following expression:
forall(t in Perioden)
constraint2:
y[t-1]+q[t]-y[t]==q[t];

Yes, just q[t] and v[t] are variables.
Is your suggestion also working if I defined the range of t as follows:
//parameters
int T=...;
range Perioden=1..T;
My constraint looks then:
forall(t in Perioden)
constraint1:
q[t]- (sum(i in t..T) q[i])*v[t]<=0;
Maybe one more basic question:
How do I express the [t-1] in the following expression:
forall(t in Perioden)
constraint2:
y[t-1]+q[t]-y[t]==q[t];

Related

Recursive Summation with Conditional in Prolog

I was ask to do a program in Prolog which has to be able to do a summation of pair and odd numbers with a recursive structure. The rule has the following form:
sum(N,PairSum,OddSum)
With N as the number given as parameter.
For Example: if N=5 then PairSum=4+2 and OddSum=5+3+1
My Code is the following
suma(0,0,0).
suma(N,SumPares,SumImpares) :-
N>0,
N1 is N -1,
suma(N1,SumaP,SumaI),
( (0 is mod(N,2))
-> SumPares is SumaP + (N-2)
; SumImpares is SumaI +(N-2)
).
The code compiles successfully, it fails when i run it. For example with N=5
suma(5,SumaPares,SumaImpares)
I get the following
ERROR: Arguments are not sufficiently instantiated ERROR: In: ERROR:
[12] _9750 is _9756+(2-2)
You should see 2 singleton warnings when consulting this code: SumaI and SumI only appear once in the body of second clause of procedure summation/3.
Looking at your code it seems that you wanted to use the same variable, so rename SumaI to SumI. You should now see no singleton warnings after reconsulting the code.
[edit after clarification]
After your edit you still have singleton warnings. Aside from typos the problem is that your conditional branch assigns one of the sum variables and leaves the other as-is. I believe you want something like this:
suma(0,0,0).
suma(N,SumaPares,SumaImpares) :-
N>0,
N1 is N -1,
suma(N1,SumaP,SumaI),
( 0 is mod(N,2) ->
(SumaPares is SumaP + (N-2), SumaImpares=SumaI)
; (SumaImpares is SumaI +(N-2), SumaPares=SumaP)
).
After following the advices from #gusbro and inluding some others i could make to this code to work properly. The key was in the base case that was suma(2,2,1) and not suma(0,0,0). Other problem i had was my summations in the if clause were I a do N-2 and I just have to leave the N alone and not substract anything. Here is the working code:
suma(2,2,1).
suma(N,SumaPares,SumaImpares) :-
N>0,
N1 is N -1,
suma(N1,SumaP,SumaI),
( 0 is mod(N,2) ->
(SumaPares is SumaP + N, SumaImpares is SumaI)
; (SumaImpares is SumaI +N, SumaPares is SumaP)
).
Thanks a lot for the help.

Capacity constraints in ampl

I am building a model that requires me to factor in the capacity of various vessels and I am not sure how to declare this in ampl.
Currently, I have:
Cap1: forall {i in I, t in T} x[1,i] * people[1,t] <= 500;
where I is the set of routes and T is the set of trips taken by the vessel. x[a,i] is an indicator variable which =1 when vessel a travels route i and people[a,t] is the amount of people taken on vessel a on trip t.
ampl always throws up the following error with respect to this constraint:
logical constraint _slogcon[1] is not an indicator constraint.
How can I fix this?
The syntax in AMPL is different than CPLEX. When you want to declare a "forall" you do this:
subject to constraint{index in set}: content of constraint
So, in your case that would be:
subject to Cap1{i in I, t in T}: x[1,i] * people[1,t] <= 500;
Regards!

Add my custom loss function to torch

I want to add a loss function to torch that calculates the edit distance between predicted and target values.
Is there an easy way to implement this idea?
Or do I have to write my own class with backward and forward functions?
If your criterion can be represented as a composition of existing modules and criteria, it's a good idea to simply construct such composition using containers. The only problem is that standard containers are designed to work with modules only, not criteria. The difference is in :forward method signature:
module:forward(input)
criterion:forward(input, target)
Luckily, we are free to define our own container which is able work with criteria too. For example, sequential:
local GeneralizedSequential, _ = torch.class('nn.GeneralizedSequential', 'nn.Sequential')
function GeneralizedSequential:forward(input, target)
return self:updateOutput(input, target)
end
function GeneralizedSequential:updateOutput(input, target)
local currentOutput = input
for i=1,#self.modules do
currentOutput = self.modules[i]:updateOutput(currentOutput, target)
end
self.output = currentOutput
return currentOutput
end
Below is an illustration of how to implement nn.CrossEntropyCriterion having this generalized sequential container:
function MyCrossEntropyCriterion(weights)
criterion = nn.GeneralizedSequential()
criterion:add(nn.LogSoftMax())
criterion:add(nn.ClassNLLCriterion(weights))
return criterion
end
Check whether everything is correct:
output = torch.rand(3,3)
target = torch.Tensor({1, 2, 3})
mycrit = MyCrossEntropyCriterion()
-- print(mycrit)
print(mycrit:forward(output, target))
print(mycrit:backward(output, target))
crit = nn.CrossEntropyCriterion()
-- print(crit)
print(crit:forward(output, target))
print(crit:backward(output, target))
Just to add to the accepted answer, you have to be careful that the loss function you define (edit distance in your case) is differentiable with respect to the network parameters.

Systemverilog random bit vector

i'm using system-verilog and I want to randomize a bit vector with size of 100.
But i want that only 10 cells will get value of 1.
I tried to use countones() in constraint but its not possible.
so i'm out of ides.
Thanks for any help!
I tried this code out and it works in Incisve:
package some_package;
class some_class;
rand bit[99:0] vec;
constraint just_10_ones {
$countones(vec) == 10;
}
endclass
endpackage
module top;
import some_package::*;
initial begin
static some_class obj = new();
obj.randomize();
$display("vec = %b", obj.vec);
end
endmodule
From what I remember, some vendors didn't support such constraints in the past, where a random variable is used as an input to a method. If they did support it, the value of the variable at the time of starting the randomize() was used for the input but this constraint did not affect its end value.

Gremlin: How to reference for loop variable within query?

reI'm stuck with a gremlin query to assign rank values to nodes based on a sorted list of keys passed to the query as a parameter.
Each node identified by "uniqueId" values should be assigned a rank based on order of occurrence in the reranked array.
This works:
reranked = [uniqueId1, uniqueId2, uniqueId3]
v.outE.as('e').inV.filter{it.key == reranked[2]}.back('e').sideEffect{it.rank = 2}
But this doesn't (replacing int with for-loop variable):
reranked = [uniqueId1, uniqueId2, uniqueId3]
for (i in 1..reranked.size())
v.outE.as('e').inV.filter{it.key == reranked[i]}.back('e').sideEffect{it.rank = i}
Do you know why this doesn't work? I'd also be happy for simpler ideas to reach the same goal.
You could do this using Groovy's eachWithIndex like:
reranked = [uniqueId1, uinqueId2, uniqueId3]
reranked.eachWithIndex{uniqueId, idx -> v.outE.as('e').inV.has('key', uniqueId).back('e').sideEffect{it.rank = idx} }
I have used Gremlin's has step above because it's much more efficient than filter in case of simple property look-up.
Well, it looks like I found a solution, maybe clumsy but hey, it works!
c=0
v.as('st').outE.as('e').inV.filter{it.key == reranked[c]}.back('e').sideEffect{
it.rank = reranked.size() - c }.outV.loop('st'){ c++ < so.size() }
I will still accept another answer if there's a fix for above situation and perhaps a more elgant approach to the solution.

Resources