How to create an indexed integer colset in CPN-Tools? - petri-net

How to create an indexed, easily iterable, int Colset?
I need to pass ints by index to a transition function, and I'm thinking about something with an index such as
colset PH = index ph with 1..n

You have an example of how to use the index with a function in the following manual (Page 8)
Look at the declared Chopsticks function:
val n = 5;
colset PH = index ph with 1..n;
colset CS = index cs with 1..n;
var p: PH;
fun Chopsticks(ph(i)) =
1`cs(i) ++ 1`cs(if i=n then 1 else i+1);
When you declare an index, you declare an id such as ph or cs. You can pass an index by its integer value using the id followed by its number, like cs(1).
So, if you want to iterate, you can use the integer value assigned to its id.

Related

get pairs / triple / quadruple... of elements from vector by function

I have a vector with a couple of elements and I want to write a function that returns me all combinations of x items from this vector.
The following code produces the right output for the case x=2 or x=3 or x=4.
However, I can not implement a solution for every possible x following this idea.
values = {'A','B','C','D','E'};
n = length(values);
data2 = {}; % case x=2
for i = 1:n
for j = i+1:n
data2{end+1} = {values{i}, values{j}};
fprintf('%s %s\n',values{i}, values{j})
end
end
data3 = {}; % case x=3
for i = 1:n
for j = i+1:n
for k = j+1:n
data3{end+1} = {values{i}, values{j}, values{k}};
fprintf('%s %s %s\n',values{i}, values{j}, values{k})
end
end
end
data4 = {}; % case x=4
for i = 1:n
for j = i+1:n
for k = j+1:n
for l = k+1:n
data4{end+1} = {values{i}, values{j}, values{k}, values{l}};
fprintf('%s %s %s %s\n',values{i}, values{j}, values{k}, values{l})
end
end
end
end
How would a function look like which would be able to return my data variable?
data = getCombinations(values, x) %values is vector with elements, x is integer value
EDIT
The following code comes pretty close:
data = perms(values)
data = data(:,1:x)
data = unique(data,'rows')
but it still produces output like A,B and B,A
EDIT2
This fixed it somehow but it is not very nice to look at and it does not work for text entries in cells but only for numbers
data = perms(values)
data = data(:,1:x)
data = sort(data,2)
data = unique(data,'rows')
EDIT3
This did it but it is not very nice to look at... Maybe there is a better solution?
function [data] = getCombinations(values,x)
i = 1:length(values);
d = perms(i);
d = d(:,1:x);
d = sort(d,2);
d = unique(d,'rows');
data = v(d);
end
If you don't want repetitions (and your example suggests you don't) then try nchoosek as nchoosek(1:n, x) to give indices:
values = {'A','B','C','D','E'};
n = length(values);
x = 3;
C = nchoosek(1:n, x);
data = values(C)
In the above, each row is a unique combination of 3 of the 5 elements of values.
Alternatively pass in the values directly:
data = nchoosek(values, x);

How do I implement a range update and range queries in Binary Indexed Tree?

I have read some tutorials on Binary Indexed Tree, but i'm not able to understand how to implement it when query and update both operations are in some range.
To implement range update and range query, you need to know about range update and point query ( update [a,b] with v; query(x) gives the value at A[x]).
We'll use two BIT's to implement range update and range query.
Let's say the array is initialized to 0. If we update [a,b] with v,
For some x, sum(0,x) = 0 if 0 < x < a
= v*(x - (a-1)) if a <= x <= b
= v * (b - (a-1)) if b < x
where v is the value at A[x] (calculated via BIT1)
From the above formula, we'll find T, when subtracted from v*x (v is calculated from BIT1) we get the result.
if 0 < x < a : sum(0,x) = 0, T = 0
a <= x <= b: sum(0,x) = v*x - v*(a-1) , T = v*(a-1)
b < x : sum(0,x) = v*(b-a+1) , T = -v*(b-(a-1)) (since A[x] = 0 when x > b)
We store T in second BIT (BIT2)
Now, to implement update [a,b] with v:
update(a,v) ; update(b+1,-v) in BIT1 and
update(a,v*(a-1)); update(b+1,-v*b) in BIT2
sum[0,x]:
QueryBIT1(x)*x - QueryBIT2(x); // call query() on corresponding BIT
where, update(index,value) and Query(index) are implementations that are used for point update and range query.
For, further details:
http://zobayer.blogspot.in/2013/11/various-usage-of-bit.html
https://kartikkukreja.wordpress.com/2013/12/02/range-updates-with-bit-fenwick-tree/

Effectively pick the variable with the min/max value that was assigned to it

So if every variable has a value assigned to it ( I mean value is an attribute of variable) . How do i most efficiently pick out the variable that has the max or min value ?
Can you give an example in Python please ?
So this would be my approach in Python :
domains = []
for var in variables :
domains.append(var.value)
min= min(domains)
for var in variables :
if var.value == min :
return var
domains.index(min(domains))
You may say that is not efficient but asymptotically you can't do any better when your list is not sorted.
A general sketch would be something like this (improvements would be providing a way of extracting the wanted value, once you have a list, the actual element can easily be retrieved using the index):
def MinAndIndex(l):
minval = l[0]
min_ix = 0
cnt = 0
for e in l:
if e < minval:
minval = e
min_ix = cnt
cnt += 1
return minval, min_ix

Filter with Linq comma separated field

I have a table with a field called COMMA_SEPARATED_VALUES. How can I filter with a single! (I have to integrate it into a larger query) LINQ query
all rows, where one of the entries is in a range of integer.
Table TEST
ID COMMA_SEPARATED_VALUES
-----------------------------------
1 '1,2,3,4'
2 '1,5,100,4,33'
3 '666,999'
4 '5,55,5'
Filter for Range "10 - 99" would result in
ID
------------------------
2 (because of 33)
4 (because of 55)
If you are aware of the performance side effect of calling AsEnumerable() method and it doesn't harm:
int lowerBound = 10; // lower bound of your range
int upperBound = 99; // upper bound of your range
var d = from row in context.Test.AsEnumerable()
let integers = row.COMMA_SEPERATED_VALUES
.Split(new char[] { ',' })
.Select(p => int.Parse(p))
where integers.Any(p => p < upperBound && p > lowerBound)
select row;

copying unordered keys from one table to ordered values in another

I have a table mapping strings to numbers like this:
t['a']=10
t['b']=2
t['c']=4
t['d']=11
From this I want to create an array-like table whose values are the keys from the first table, ordered by their (descending) values in the first table, like this:
T[1] = 'd' -- 11
T[2] = 'a' -- 10
T[3] = 'c' -- 4
T[4] = 'b' -- 2
How can this be done in Lua?
-- Your table
local t = { }
t["a"] = 10
t["b"] = 2
t["c"] = 4
t["d"] = 11
local T = { } -- Result goes here
-- Store both key and value as pairs
for k, v in pairs(t) do
T[#T + 1] = { k = k, v = v }
end
-- Sort by value
table.sort(T, function(lhs, rhs) return lhs.v > rhs.v end)
-- Leave only keys, drop values
for i = 1, #T do
T[i] = T[i].k
end
-- Print the result
for i = 1, #T do
print("T["..i.."] = " .. ("%q"):format(T[i]))
end
It prints
T[1] = "d"
T[2] = "a"
T[3] = "c"
T[4] = "b"
Alexander Gladysh's answer can be simplified slightly:
-- Your table
local t = { }
t["a"] = 10
t["b"] = 2
t["c"] = 4
t["d"] = 11
local T = { } -- Result goes here
-- Store keys (in arbitrary order) in the output table
for k, _ in pairs(t) do
T[#T + 1] = k
end
-- Sort by value
table.sort(T, function(lhs, rhs) return t[lhs] > t[rhs] end)
-- Print the result
for i = 1, #T do
print("T["..i.."] = " .. ("%q"):format(T[i]))
end
Tables in Lua do not have an order associated with them.
When using a table as an array with sequential integer keys from 1 to N, the table can be iterated in order using a loop or ipairs().
When using keys that are not sequential integers from 1 to N, the order can not be controlled. To get around this limitation a second table can be used as an array to store the order of the keys in the first table.

Resources