I want solve this problem : i have a number n and i want to have an array with all pair
(i,j) for all i,j in [1,n]
I write this for solve the problem:
include "globals.mzn";
int:n=2;
var 1..:ArrayLenght;
array[1..ArrayLenght,1..2] of var 1..n:X;
constraint forall(i,j in 1..n)(exists(r in 1..ArrayLenght) (X[r,..] == [i,j]));
solve minimize ArrayLenght;
but i have a type error type error: type-inst must be par set but is 'var set of int' on this line array[1..ArrayLenght,1..2] of var 1..size:X
So how i can do to have an array with a variabe size ? (i don't see anything about this in the official documentation)
NB : for this specific example, it would be better to set the arrayLenght to n*n but it's a minimal example, I have to add constraints that make the size of array cannot be fixed.
MiniZinc does not support variable length arrays. The dimensions of all arrays must be known at compile time. One common approach to handle this is to create a multi-dimensional array of dimension - say - n x m (where n and m are the largest possible values in each dimension) - and then set 0 (or some other value) as a dummy variable for the "invalid" cells.
However, it seems that what you want here is just to create an array of pairs of numbers. It's quite easy to create this without using any decision variables (i.e. without var ...).
Here are two different approaches to generate the pairs:
pairs1 is the pairs with i < j
pairs2 is the all possible pairs
The loop variablek in the list comprehensions is used as a counter to select either the i value or the j in the appropriate places.
int: n = 5;
int: num_pairs1 = n*(n-1) div 2;
int: num_pairs2 = n*n;
array[1..num_pairs1,1..2] of int: pairs1 = array2d(1..num_pairs1,1..2, [ if k == 1 then i else j endif | i,j in 1..n, k in 1..2 where i < j]);
array[1..num_pairs2,1..2] of int: pairs2 = array2d(1..num_pairs2,1..2, [ if k == 1 then i else j endif | i,j in 1..n, k in 1..2]);
output ["pairs1:\n", show2d(pairs1)];
output ["\n\npairs2:\n", show2d(pairs2)];
The output is
pairs1:
[| 1, 2
| 1, 3
| 1, 4
| 1, 5
| 2, 3
| 2, 4
| 2, 5
| 3, 4
| 3, 5
| 4, 5
|]
pairs2:
[| 1, 1
| 1, 2
| 1, 3
| 1, 4
| 1, 5
| 2, 1
| 2, 2
| 2, 3
| 2, 4
| 2, 5
| 3, 1
| 3, 2
| 3, 3
| 3, 4
| 3, 5
| 4, 1
| 4, 2
| 4, 3
| 4, 4
| 4, 5
| 5, 1
| 5, 2
| 5, 3
| 5, 4
| 5, 5
|]
----------
==========
Hope this helps. If not, please describe in more detail what you are looking for.
Related
Get-TransportRule "Gmail Block" | Select-Object "ExceptIfFrom" | Format-List
returns the results
ExceptIfFrom : {terer.nolt#gmail.com, calendar-notification#google.com, brianqfaanur#gmail.com, cced1rley657#gmail.com...}
How would I request the entire list?
Update $formatenumerationlimit to a value equal to or larger than your collection size:
# -1 is unlimited
$formatenumerationlimit = -1
When an object's property value is a collection and you are using a view that displays the property/value pair, $formatenumerationlimit automatic variable determines how many items in the collection are visible before being truncated. The default value is 4.
You can easily replicate this situation with a simple object:
$obj = [pscustomobject]#{property = 1,2,3,4,5,6,7,8,9}
$obj
Output:
property
--------
{1, 2, 3, 4...}
Now update $formatenumerationlimit
$formatenumerationlimit = 9
$obj
$formatenumerationlimit = -1
$obj
Output:
property
--------
{1, 2, 3, 4, 5, 6, 7, 8, 9}
property
--------
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Alternatively, retrieving only the property's value will likely display all list items and is not impacted by $formatenumerationlimit.
$obj.property
$obj | Select-Object -ExpandProperty property
Output:
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
I am trying to run linear regressions by group and add the results to a new column in the dataframe.
Here is what I'm trying to do.
df2 = pd.DataFrame.from_dict({'case': ['foo', 'foo', 'foo', 'bar', 'bar'],
'cluster': [1, 1, 1, 1, 1],
'conf': [1, 2, 3, 1, 4],
'conf_1': [11, 12, 13, 11, 14]})
def ols_res(df, xcols, ycol):
results = sm.OLS(df[ycol], sm.add_constant(df[xcols])).fit()
return results.get_influence().cooks_distance[0]
df3 = df2.groupby(['case', 'cluster'])
df3.apply(ols_res, xcols='conf', ycol='conf_1')
output I got is :
case cluster
bar 1 [nan, nan]
foo 1 [0.42857142857143005, 0.09642857142857146, 10....
dtype: object
The size of results for each group is same as number of rows in the group.
I need the above output in following format. Can some one please help me?
case cluster conf conf_1 result
0 foo 1 1 11 0.42857142857143005
1 foo 1 2 12 0.09642857142857146
2 foo 1 3 13 10....
4 bar 1 1 11 nan
5 bar 1 4 14 nan
following worked for me.
def ols_res_mod(df, xcols, ycol):
results = sm.OLS(df[ycol], sm.add_constant(df[xcols])).fit()
results.get_influence().cooks_distance[0]
print(df)
df['distance'] = results.get_influence().cooks_distance[0]
return df
not sure, whether this is an efficient way or not.
To find how many ways we have of making change for the amount 4 given the coins [1,2,3], we can create a DP algorithm that produces the following table:
table[amount][coins.count]
0 1 2 3 4
-----------
(0) 1 | 1 1 1 1 1
(1) 2 | 1 1 2 2 3
(2) 3 | 1 1 2 3 4
The last position being our answer. The answer is 4 because we have the following combinations: [1,1,1,1],[2,1],[2,2],[3,1].
My question is, is it possible to retrieve these combinations from the table I just generated? How?
For completeness, here's my algorithm
func coinChange(coins: [Int], amount: Int) -> Int {
// int[amount+1][coins]
var table = Array<Array<Int>>(repeating: Array<Int>(repeating: 0, count: coins.count), count: amount + 1)
for i in 0..<coins.count {
table[0][i] = 1
}
for i in 1...amount {
for j in 0..<coins.count {
//solutions that include coins[j]
let x = i - coins[j] >= 0 ? table[i - coins[j]][j] : 0
//solutions that don't include coins[j]
let y = j >= 1 ? table[i][j-1] : 0
table[i][j] = x + y
}
}
return table[amount][coins.count - 1];
}
Thanks!
--
Solution
Here's an ugly function that retrieves the combinations, based on #Sayakiss 's explanation:
func getSolution(_ i: Int, _ j: Int) -> [[Int]] {
if j < 0 || i < 0 {
//not a solution
return []
}
if i == 0 && j == 0 {
//valid solution. return an empty array where the coins will be appended
return [[]]
}
return getSolution(i - coins[j], j).map{var a = $0; a.append(coins[j]);return a} + getSolution(i, j - 1)
}
getSolution(amount, coins.count-1)
Output:
[[1, 3], [2, 2], [1, 1, 2], [1, 1, 1, 1]]
Sure you can. We define a new function get_solution(i,j) which means all solution for your table[i][j].
You can think it returns an array of array, for example, the output of get_solution(4,3) is [[1,1,1,1],[2,1],[2,2],[3,1]]. Then:
Case 1. Any solution from get_solution(i - coins[j], j) plus coins[j] is a solution for table[i][j].
Case 2. Any solution from get_solution(i, j - 1) is a solution for table[i][j].
You can prove Case 1 + Case 2 is all possible solution for table[i][j](note you get table[i][j] by this way).
The only problem remains is to implement get_solution(i,j) and I think it's good for you to do it by yourself.
If you still got any question, please don't hesitate to leave a comment here.
I am trying to find the odd numbers and a multiple of 7 between a 1 to 100 and append them into an array. I have got this far:
var results: [Int] = []
for n in 1...100 {
if n / 2 != 0 && 7 / 100 == 0 {
results.append(n)
}
}
Your conditions are incorrect. You want to use "modular arithmetic"
Odd numbers are not divisible by 2. To check this use:
if n % 2 != 0
The % is the mod function and it returns the remainder of the division (e.g. 5 / 2 is 2.5 but integers don't have decimals, so the integer result is 2 with a remainder of 1 and 5 / 2 => 2 and 5 % 2 => 1)
To check if it's divisible by 7, use the same principle:
if n % 7 == 0
The remainder is 0 if the dividend is divisible by the divisor. The complete if condition is:
if n % 2 != 0 && n % 7 == 0
You can also use n % 2 == 1 because the remainder is always 1. The result of any mod function, a % b, is always between 0 and b - 1.
Or, using the new function isMultiple(of:, that final condition would be:
if !n.isMultiple(of: 2) && n.isMultiple(of: 7)
Swift 5:
Since Swift 5 has been released, you could use isMultiple(of:) method.
In your case, you should check if it is not multiple of ... :
if !n.isMultiple(of: 2)
Swift 5 is coming with isMultiple(of:) method for integers , so you can try
let res = Array(1...100).filter { !$0.isMultiple(of:2) && $0.isMultiple(of:7) }
Here is an efficient and concise way of getting the odd multiples of 7 less than or equal to 100 :
let results: [Int] = Array(stride(from: 7, through: 100, by: 14))
You can also use the built-in filter to do an operation on only qualified members of an array. Here is how that'd go in your case for example
var result = Array(1...100).filter { (number) -> Bool in
return (number % 2 != 0 && number % 7 == 0)
}
print(result) // will print [7, 21, 35, 49, 63, 77, 91]
You can read more about filter in the doc but here is the basics: it goes through each element and collects elements that return true on the condition. So it filters the array and returns what you want
I need to eliminate alternate rows of an array, like i have an array of 23847X1 and i need the odd rows and finally making it into 11924X1. It is in .mat file and i want the resultant in the .mat file as well.
Try yourMatrix(1:2:size(yourMatrix, 2)).
The 1:2:N selects all elements from 1 to N with step 2.
A more complete example:
> M=[1, 2, 3, 4, 5, 6, 7]
M =
1 2 3 4 5 6 7
> OddM = M(1:2:size(M, 2))
OddM =
1 3 5 7
To load / store data in data.mat, follow H.Muster's advice below:
load('data.mat'); x = x(1:2:end,:); save('data.mat', 'x')