Mathematica: Excel imported list parameters not working on piecewise function - excel

I have a piecewise function like:
q1N := (e + w)/(e + b)
FeasR := Piecewise[{{1, q1N <= q}, {0, q < q1N}}]
parameters e,w and b are imported from Excel using this code:
InData := Import["C:\\Users\\HR\\Desktop\\DatImp.csv"]
MainData := InData[[2 ;; 1001, 2 ;; 7]]
b := MainData[[All, 1]]
w := MainData[[All, 2]]
e := MainData[[All, 3]]
q := MainData[[All, 4]]
b,e,q and w shape different cases where FeasR would determine feasibility based on comparison of q1N and q.
The problem is that FeasR output is not in the form of {{1,0,1,1,0,...}}.
I would appreciate your help.

In this example InData is random so the output will differ.
InData = RandomReal[10, {10, 7}];
MainData = InData[[2 ;; 10, 2 ;; 7]];
b = MainData[[All, 1]]
w = MainData[[All, 2]]
e = MainData[[All, 3]]
q = MainData[[All, 4]]
q1N := (e + w)/(e + b)
FeasR = Piecewise[{{1, Thread[q1N <= q]}, {0, Thread[q < q1N]}}]
{ 1 {True,True,False,True,True,True,True,True,True}
{ 0 True
However, this is not the format you want. Use Boole instead.
FeasR = Boole[Thread[q1N <= q]]
{1, 1, 0, 1, 1, 1, 1, 1, 1}

Related

MiniZinc - Constraint to enforce two arrays be equal

I'm trying to model a satisfaction problem with MiniZinc but I'm stuck at coding this constraint:
Given two arrays A and B of equal length, enforce that A is a permutation of B
In other words, I'm looking for a constraint that enforces [1,2,2]=[1,2,2] and also [1,2,2]=[2,2,1]. Informally, A and B have to be equal. The arrays are both defined on the same set of integers, in particular the set 1..n-1, for some n. Values in both A and B can be repeated (see example).
Is there such a global constraint in MiniZinc? Thank you.
Here is the predicate I tend to use for these cases. It requires an extra array (p) which contains the permutations from array a to array b.
/*
Enforce that a is a permutation of b with the permutation
array p.
*/
predicate permutation3(array[int] of var int: a,
array[int] of var int: p,
array[int] of var int: b) =
forall(i in index_set(a)) (
b[i] = a[p[i]]
)
/\
all_different(p)
;
A simple model using this:
include "globals.mzn";
int: n = 3;
array[1..n] of var 1..2: x;
array[1..n] of var 1..2: y;
array[1..n] of var 1..n: p;
/*
Enforce that array b is a permutation of array a with the permutation
array p.
*/
predicate permutation3(array[int] of var int: a,
array[int] of var int: p,
array[int] of var int: b) =
forall(i in index_set(a)) (
b[i] = a[p[i]]
)
/\
all_different(p)
;
solve satisfy;
constraint
x = [2,2,1] /\
permutation3(x,p,y)
;
output [
"x: \(x)\ny: \(y)\np: \(p)\n"
];
Output:
x: [2, 2, 1]
y: [1, 2, 2]
p: [3, 2, 1]
----------
x: [2, 2, 1]
y: [2, 1, 2]
p: [2, 3, 1]
----------
x: [2, 2, 1]
y: [1, 2, 2]
p: [3, 1, 2]
----------
x: [2, 2, 1]
y: [2, 1, 2]
p: [1, 3, 2]
----------
x: [2, 2, 1]
y: [2, 2, 1]
p: [2, 1, 3]
----------
x: [2, 2, 1]
y: [2, 2, 1]
p: [1, 2, 3]
----------
==========
There is an alternative formulation of this which don't requires the extra permutation p (it's defined inside the predicate):
predicate permutation3b(array[int] of var int: a,
array[int] of var int: b) =
let {
array[index_set(a)] of var index_set(a): p;
} in
forall(i in index_set(a)) (
b[i] = a[p[i]]
)
/\
all_different(p)
;
For the same problem, this will only output these 3 different solutions (the first model has more solutions since the the permutations differs).
x: [2, 2, 1]
y: [2, 2, 1]
----------
x: [2, 2, 1]
y: [2, 1, 2]
----------
x: [2, 2, 1]
y: [1, 2, 2]
----------
==========
Personally I tend to use the first version since I tend to want to output the permutation and like to have control over the variables.
In addition to the predicate shown by hakank, here are two other ways to express the same predicate
include "globals.mzn";
%
% Ensure that a and b are perumutations of each other by
% counting the number of occurences of each value in the
% domains of a and b,
%
predicate permutation_count(array[int] of var int: a,
array[int] of var int: b) =
let {
set of int: I = index_set(a),
constraint assert(I = index_set(b), "Index sets of a and b must match"),
set of int: domain = dom_array(a) intersect dom_array(b),
set of int: NValues = 1..card(domain),
array[NValues] of int: values = [ v | v in domain ],
array[NValues] of var 0..card(I): counts,
} in
global_cardinality_closed(a, values, counts) /\
global_cardinality_closed(b, values, counts);
%
% Ensure that a and b are permutations of each other by
% sorting each and constraining that to be the same.
%
predicate permutation_sort(array[int] of var int: a,
array[int] of var int: b) =
let {
set of int: I = index_set(a),
constraint assert(I = index_set(b), "Index sets of a and b must match"),
set of int: domain = dom_array(a) intersect dom_array(b),
array[I] of var domain: sorted_values,
} in
sorted_values = sort(a) /\
sorted_values = sort(b);
int: n = 3;
array[1..n] of var 1..2: x;
array[1..n] of var 1..2: y;
constraint permutation_count(x, y);
solve satisfy;
The first one counts the values in both input arrays, since in permutations the counts must be the same. The second variant uses the sorting constraint to sort both a and b to check that they are the same.
Which variant works best can vary between solvers, problems, and problem isntances. The counting solution may be problematic if the domains of the inputs are large, which is worth remembering.

use gather_nd to acquire result in a loop - shapes of all inputs must match

I have this example in numpy:
import numpy as np
import tensorflow as tf
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11 , 12],
[13, 14, 15]])
res = np.zeros((5, 2), dtype=object)
for idx in range(0, len(a)-2, 2):
a0 = a[idx]
a1 = a[idx + 1]
a2 = a[idx + 2]
c = a0 + a1 + a2
res[idx:idx + 2] = ([idx, c])
res
array([[0, array([12, 15, 18])],
[0, array([12, 15, 18])],
[2, array([30, 33, 36])],
[2, array([30, 33, 36])],
[0, 0]], dtype=object)
and I want to do it in tensorflow:
a_tf = tf.convert_to_tensor(a)
res_tf = tf.zeros((5, 2), dtype=object)
for idx in range(0, a.shape[0]-2, 2):
a0 = tf.gather_nd(a, [idx])
a1 = tf.gather_nd(a, [idx + 1])
a2 = tf.gather_nd(a, [idx + 2])
c = a0 + a1 + a2
res = tf.gather_nd([idx, c], [idx:idx +2])
until line with the computation of c is ok.
At last line (res) it gives me:
res = tf.gather_nd([idx, c], [idx:idx +2])
^
SyntaxError: invalid syntax
I am not sure how to receive the result.
UPDATE
basically, the problem lies in the fact that [idx, c] is of type list and trying to do : tf.convert_to_tensor([idx, c], gives :
InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [] != values[1].shape = [3] [Op:Pack] name: packed/
res = tf.gather_nd([idx, c], [idx:idx +2])
is syntactically incorrect. If you want to extract indices, it should rather be
res = tf.gather_nd([idx, c], range(idx, idx +2))
The latter would likely also throw an error. The indices in range(idx, idx +2) are higher than the indices of the list [idx, c].
Also, it is not possible to create a tensor with the shape of res unless using ragged tensors. Here is a possible fix of what you're trying to do
a_tf = tf.convert_to_tensor(a)
res_tf = tf.zeros((5, 2), dtype=object)
l = []
for idx in range(0, a.shape[0]-2, 2):
a0 = tf.gather_nd(a, [idx])
a1 = tf.gather_nd(a, [idx + 1])
a2 = tf.gather_nd(a, [idx + 2])
c = a0 + a1 + a2
helper = [idx]
helper.extend(c.numpy().tolist())
l.append(helper)
print(tf.constant(l))

Compute distance metric in a complete graph

Suppose I have an array, namely Map. Map[i][j] means the distance between area i and area j. Under this definition, we get:
a) Map[i][i] always equals 0.
b) Map[i][k] <= Map[i][j] + Map[j][k] for all i,j,k
I want to build a function func(Map,k) returning a metric D, while D[i][j] is the shortest distance of a route from area i to area j, and this route should pass through at least k different area.
This is my python code to do so:
def func(Map,k):
n=len(Map)
D_temp = [list(x) for x in Map]
D = [list(x) for x in Map]
for m in range(k - 1):
for i in range(n):
for j in range(n):
tmp = [D[i][x] + Map[x][j] for x in range(n) if x != i and x != j]
D_temp[i][j] = min(tmp)
D = [list(x) for x in D_temp]
return D
func([[0, 2, 3], [2, 0, 1], [3, 1, 0]],2)
return a distance metric D which equals [[4, 4, 3], [4, 2, 5], [3, 5, 2]]
D[0][0] equals 4, because the shortest route from area0 to area0 which pass through at least 2 area is {area0-->area1-->area0}, and the distance of the route is Map[0][1]+Map[1][0]=2+2=4
Wanted to know what would be the best way to do that?
You can use the A* algorithm for this, using Map[i][j] as the heuristic for the minimum remaining distance to the target node (assuming that, as you said, Map[i][j] <= Map[i][x] + Map[x][j] for all i,j,x). The only difference to a regular A* would be that you only accept paths if they have a minimum length of k.
import heapq
def min_path(Map, k, i, j):
heap = [(0, 0, i, [])]
while heap:
_, cost, cur, path = heapq.heappop(heap)
if cur == j and len(path) >= k:
return cost
for other in range(len(Map)):
if other != cur:
c = cost + Map[cur][other]
heapq.heappush(heap, (c + Map[other][j], c, other, path + [other]))
Change your func to return a list comprehension using this min_path accordingly.
def func(Map, k):
n = len(Map)
return [[min_path(Map, k, i, j) for i in range(n)] for j in range(n)]
res = func([[0, 2, 3], [2, 0, 1], [3, 1, 0]], 2)
This gives me the result [[4, 4, 3], [4, 2, 3], [3, 3, 2]] for len(path) >= k, or [[4, 4, 3], [4, 2, 5], [3, 5, 2]] for len(path) == k.

Dynamic Programming: Tabulation of a Recursive Relation

The following recursive relation solves a variation of the coin exchange problem. Count the number of ways in which we can sum to a required value, while keeping the number of summands even:
def count_even(coins, num_coins, req_sum, parity):
if req_sum < 0:
return 0
if req_sum == 0 and not parity:
return 1
if req_sum == 0 and parity:
return 0
if num_coins == 0:
return 0
count_wout_high_coin = count_even(coins, num_coins - 1, req_sum, parity)
count_with_high_coin = count_even(coins, num_coins, req_sum - coins[num_coins - 1], not parity)
return count_wout_high_coin + count_with_high_coin
This code would yield the required solution if called with parity = False.
I am having issues implementing a tabulation technique to optimize this algorithm. On a first attempt I tried to follow the same pattern as for other DP problems, and took the parity as another parameter to the problem, so I coded this triple loop:
def count_even_tabulation(S, m, n):
if m <= 0 or n < 0:
return 0
if n == 0:
return 1
table = [[[0 for x in range(m)] for x in range(n + 1)] for x in range(2)]
for j in range(m):
table[0][0][j] = 1
table[1][0][j] = 0
for p in range(2):
for i in range(1, n + 1):
for j in range(m):
y = table[p][i][j - 1] if j >= 1 else 0
x = table[1 - p][i - S[j]][j] if i - S[j] >= 0 else 0
table[p][i][j] = x + y
return table[0][n][m - 1]
However, this approach is not creating the right tables for parity equal to 0 and equal to 1:
[1, 1, 1]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[1, 1, 1]
[0, 1, 1]
[0, 0, 1]
[0, 0, 0]
How can I adequately implement a tabulation approach for the given recursion relation?

calculation of variance function equation

I have an error in this code as I want to calculate the variance between the values in the(x1) and (x2) list. any recommendation?!
def my_var(L):
s = 0
t = 0
u = 0
for i in range(0, len(L)):
s += L[i]
t = s/len(L)
u += ((L[i]-t)*(L[i]-t))
return u / len(L)
x1 = [1, 3, 4, -3, 8]
x2 = [1, -4, 7, 2]
v1 = my_var(x1)
v2 = my_var(x2)
print(v1)
print(v2)
You're doing many things incorrectly based on how I learned prob and stats. You need to calculate the average (mean) and then sum each value subtracted by the mean, squared. Then finally take that numerator and divide by 1 less than the sample size (n-1).
def my_var(L):
mean = float(sum(L) / Len(L))
numerator = 0
for i in range(0, len(L)):
numerator += (L[i]-mean)**2
return numerator / (len(L) - 1)
x1 = [1, 3, 4, -3, 8]
x2 = [1, -4, 7, 2]
v1 = my_var(x1)
v2 = my_var(x2)
print(v1)
print(v2)
Without using sum:
def my_var(L):
my_sum = 0
mean = 0
numerator = 0
for i in range(0, len(L)):
my_sum += L[i]
mean = float(my_sum / len(L))
for i in range(0, len(L)):
numerator += (L[i]-mean)**2
return numerator / (len(L) - 1)
x1 = [1, 3, 4, -3, 8]
x2 = [1, -4, 7, 2]
v1 = my_var(x1)
v2 = my_var(x2)
print(v1)
print(v2)
Try numpy.
import numpy as np
x1 = [1, 3, 4, -3, 8]
x2 = [1, -4, 7, 2]
v1 = np.var(x1)
v2 = np.var(x2)
Thank you #billy_ferguson. I have modified your code and it works. Execuse me, I am still an amateur but could you replace float and sum function and use simpler arithmetic operators as len(L) and += in this line mean = float(sum(L) / len(L))
def my_var(L):
mean = 0
numerator = 0
for i in range(0, len(L)):
mean = float(sum(L) / len(L))
numerator += (L[i]-mean)**2
return numerator / len(L)
x1 = [1, 3, 4, -3, 8]
x2 = [1, -4, 7, 2]
v1 = my_var(x1)
v2 = my_var(x2)
print(v1)
print(v2)

Resources