how to make K different elements in bool array with or-tools? - or-tools

I want make K different elements in bool array,I use code: model.Add(len(set([shifts[(i)] for i in range(10)]))==4) ,but it not work! How can I do this?
model = cp_model.CpModel()
solver = cp_model.CpSolver()
shifts = {}
ones = [model.NewBoolVar("") for _ in range(10)]
for i in range(10):
shifts[(i)] = model.NewIntVar(0, 10, "shifts(%i)" % i)
for i in range(10):
model.Add(shifts[(i)] >0).OnlyEnforceIf(ones[(i)])
model.Add(shifts[(i)] == 0).OnlyEnforceIf(ones[(i)].Not())
model.Add(sum(ones[(i)] for i in range(10)) == 5)
# I want make 4 different but it not work!
#model.Add(len(set([shifts[(i)] for i in range(10)]))==4)
status = solver.Solve(model)
print("status:",status)
res=[]
for i in range(10):
res.append(solver.Value(shifts[(i)]))
print(res)

Encode your integers using booleans and add another boolean for each value to mark it as used
from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.CpSolver()
shifts = {}
used = [model.NewBoolVar("") for j in range(10)]
for i in range(10):
for j in range(10):
shifts[i, j] = model.NewBoolVar(f"shifts({i}, {j})")
# shifts[i,j] => used[j]
model.AddImplication(shifts[i, j], used[j])
model.Add(sum(shifts[i, j] for j in range(10)) == 1)
model.Add(sum(shifts[i, 0].Not() for i in range(10)) == 5)
for j in range(10):
# all(shifts[_, j] == 0) => used[j].Not()
model.AddBoolOr([shifts[i, j] for i in range(10)] + [used[j].Not()])
model.Add(sum(used) == 4)
status = solver.Solve(model)
print("status:", status)
res = []
for i in range(10):
for j in range(10):
if solver.Value(shifts[i, j]):
res.append(j)
print(res)

Related

NULLSPACE - RREF Command in Matlab bugs

Can someone help me debug this Matlab code? Like I have a matrix A, and I need to find A^k by diagonalization method. Below is my original code, but there is a problem with this part:
m = (M - R(i,1)*eye(NumRowsR));
disp(rref(m));
t = null(rref(m));
disp(t);
This part can't give me the nullspace of matrix t after reduced for some reasons (I recently did some research on the Internet and see some people said about the bug of rref and null). The problem is that it keeps showing me elementary matrix
1 0 0
0 1 0
0 0 1
for the eigenvalues. Does anyone know how to fix it? I will be very much appreciated!
Below is my original code, for a better view:
syms x NumRowsM NumColsM NumRowsR NumColsR NumRowst NumColst k numeigen
M = input('Input a square matrix M: ');
k = input('Input the power of matrix M: ');
[NumRowsM, NumColsM]=size(M);
if (NumRowsM ~= NumColsM)
disp('Not valid input. Matrix must be a square matrix!')
else
%Find eigenvalues:
R = solve(det(M - x*eye(NumRowsM)), x);
[NumRowsR, NumColsR] = size(R);
if (or(NumRowsR == 0,NumColsR == 0))
disp('No eigenvalues. The matrix is not diagonalizable')
else
numeigen = 0;
F = zeros(NumRowsR, NumRowsR);
d = zeros(NumRowsR,1);
for i = 1:NumRowsR
m = (M - R(i,1)*eye(NumRowsR));
disp(rref(m));
t = null(rref(m));
disp(t);
[NumRowst, NumColst] = size(t);
if (NumColst == 0)
if (i == NumRowsR && numeigen > NumRowsR)
disp('Matrix not diagonalizable due to invalid eigenvalue');
return
else
continue
end
else
numeigen = numeigen + 1;
if (NumColst == 1)
for j = 1:NumRowsR
[n, d(j)] = numden(sym(t(j,1)));
end
for j = 1:NumRowsR
F(j,i) = sym(t(j,1)) * lcm(sym(d));
end
else
for k = 1:NumColst
for j = 1:NumRowsR
[n, d(j)] = numden(sym(t(j,k)));
end
for j = 1:NumRowsR
F(j,k) = sym(t(j,k)) * lcm(sym(d));
end
end
end
end
end
disp(F);
D = (F\M)*F;
disp('The power k of the matrix M is: ');
T = F*(D^k)/(F);
disp(T)
end
end

How to display column vector x in Cramer's rule in MATLAB?

I am trying to code the Cramer's Rule on MATLAB for a square matrix. I understand the Rule perfectly and also believe the logic behind my code is alright. However, could you please check where I might be going wrong that my result is not being displayed correctly? Any help would be appreciated! Thanks :)
function x = cramer(A, b)
r = size(A, 1);
c = size(A, 2);
n = size(b, 1);
if r ~= c
disp('Oops! Please enter a square matrix');
end
if r == c == n
D = det(A);
if D == 0
disp('Oops! Either, there are a family of soultions or no unique solution')
end
if D ~= 0
result = zeros(n, 1);
for (i = 1:n)
A_x = A;
A_x(:, i) = b;
Dx = det(A_x);
result(i,1) = Dx/D;
end
x = result;
end
end
end
You have an error in your if r == c == n check.
The expression r == c == n can be expanded as (r == c) == n
So it compares the value of logical(r == c) with n.
As an example:
>> 2 == 2 == 1
ans =
logical
1
Rewrite your check as r == c && c == n or isequal(r,c,n) and you should be good.
EDIT: You can simplify your logic quite some, say for example by:
function x = cramers(A, b)
if diff(size(A)) && size(A,1) == n
D = det(A);
if D == 0
disp('Oops! Either, there are a family of solutions or no unique solution')
return
else
result = zeros(n, 1);
for i = 1:n
A_x = A;
A_x(:, i) = b;
Dx = det(A_x);
result(i,1) = Dx/D;
end
x = result;
end
else
disp('Dimension error')
return
end
end

coffeescript prime number generation

I am trying to generate prime numbers like this:
generatePrimeNumbersBelowN = (n) ->
for i in [2..n-1]
isPrime = true
for j in [2..i-1]
isPrime = false if i % j == 0
break if not isPrime
console.log(i, "is Prime Number.") if isPrime
generatePrimeNumbersBelowN(100);
I am getting prime numbers from 3 to 97, inclusive. I am new to JavaScript/CoffeeScript, so please tell me what's happening to 2?
Here's the generated JS code:
var generatePrimeNumbersBelowN;
generatePrimeNumbersBelowN = function(n) {
var i, isPrime, j, k, l, ref, ref1, results;
results = [];
for (i = k = 2, ref = n - 1; 2 <= ref ? k <= ref : k >= ref; i = 2 <= ref ? ++k : --k) {
isPrime = true;
for (j = l = 2, ref1 = i - 1; 2 <= ref1 ? l <= ref1 : l >= ref1; j = 2 <= ref1 ? ++l : --l) {
if (i % j === 0) {
isPrime = false;
}
if (!isPrime) {
break;
}
}
if (isPrime) {
results.push(console.log(i, "is Prime Number."));
} else {
results.push(void 0);
}
}
return results;
};
generatePrimeNumbersBelowN(100);
generatePrimeNumbersBelowN = (n) ->
for i in [2..n-1]
isPrime = true
for j in [2..i-1]
isPrime = false if i % j == 0
break if not isPrime
console.log(i, "is Prime Number.") if isPrime
When i is 2, j ranges from 2 down to 1. You then check i % j which is 2 % 1 which is zero and claim that 2 is not a prime.
Because for x in [b..a] delivers a downward loop from b to a, while loop construct solves this problem.
generatePrimeNumbersBelowN = (n) ->
i = 2
while i < n
isPrime = true
j = 2
while j < i
isPrime = false if i % j == 0
break if not isPrime
j++
console.log(i, "is Prime Number.") if isPrime
i++
generatePrimeNumbersBelowN(100);

How can I save my result to workspace in matlab?

Here is my dummy code to create M and V.
function [M,V] = likelihood(xTrain, yTrain)
M = zeros(5, 5)
V = zeros(5, 5)
rows = size(xTrain, 1)
classCount = zeros(5, 1)
for i = 1 : 5
for j = 1 : rows
class = yTrain(j)
M(class, i) = M(class, i) + xTrain(j, i)
end
end
for i = 1:5
classCount(i) = sum(yTrain == i)
end
for k = 1 : 5
M(:, k) = M(:, k) ./ classCount
end
for i = 1:5
for j = 1:rows
class = yTrain(j)
V(class, i) = V(class, i)+ abs(xTrain(j, i) - M(class, i))
end
end
for k = 1 : 5
V(:, k) = V(:, k) ./ classCount
end
end
The problem I meet here is since I want to use M and V later, I want to save it in the workspace and I suppose it would create M and V in workspace but it didn't. Instead, matlab continuously update the variable ans which is not what I expected.
M and V wont change unless you close the MATLAB or use them again as a new variables.
when and where do you exactly want these two?

Filter points using hist in matlab

I have a vector. I want to remove outliers. I got bin and no of values in that bin. I want to remove all points based on the number of elements in each bin.
Data:
d1 =[
360.471912914169
505.084636471948
514.39429429184
505.285068055647
536.321181755858
503.025854206322
534.304229816684
393.387035881967
396.497969729985
520.592172434431
421.284713703215
420.401106087984
537.05330275495
396.715779872694
514.39429429184
404.442344469518
476.846474245118
599.020867750031
429.163139144079
514.941744277933
445.426761656729
531.013596812737
374.977332648255
364.660115724218
538.306752697753
519.042387479096
1412.54699036882
405.571202133485
516.606049132218
2289.49623498271
378.228766753667
504.730621222846
358.715764917016
462.339366699398
512.429858614816
394.778786157514
366
498.760463549388
366.552861126468
355.37022947906
358.308526273099
376.745272034036
366.934599077274
536.0901883079
483.01740134285
508.975480745389
365.629593988233
536.368800360349
557.024236456548
366.776498701866
501.007025898839
330.686029339009
508.395475983019
429.563732174866
2224.68806802212
534.655786464525
518.711297351426
534.304229816684
514.941744277933
420.32368479542
367.129404978681
525.626188464768
388.329756778952
1251.30895065927
525.626188464768
412.313764019587
513.697381733643
506.675438520558
1517.71183364959
550.276294237722
543.359917550053
500.639590923451
395.129864728041];
Histogram computation:
[nelements,centers] = hist(d1);
nelements=55 13 0 0 1 1 1 0 0 2
I want to remove all points apearing less than 5 (in nelements). It means only first 2 elements in nelements( 55, 13 ) remains.
Is there any function in matlab.
You can do it along these lines:
threshold = 5;
bin_halfwidth = (centers(2)-centers(1))/2;
keep = ~any(abs(bsxfun(#minus, d1, centers(nelements<threshold))) < bin_halfwidth , 2);
d1_keep = d1(keep);
Does this do what you want?
binwidth = centers(2)-centers(1);
centersOfRemainingBins = centers(nelements>5);
remainingvals = false(length(d1),1);
for ii = 1:length(centersOfRemainingBins )
remainingvals = remainingvals | (d1>centersOfRemainingBins (ii)-binwidth/2 & d1<centersOfRemainingBins (ii)+binwidth/2);
end
d_out = d1(remainingvals);
I don't know Matlab function for this problem, but I think, that function with follow code is what are you looking for:
sizeData = size(data);
function filter_hist = filter_hist(data, binCountRemove)
if or(max(sizeData) == 0, binCountRemove < 1)
disp('Error input!');
filter_hist = [];
return;
end
[n, c] = hist(data);
sizeN = size(n);
intervalSize = c(2) - c(1);
if sizeData(1) > sizeData(2)
temp = transpose(data);
else
temp = data;
end
for i = 1:1:max(sizeN)
if n(i) < binCountRemove
a = c(i) - intervalSize / 2;
b = c(i) + intervalSize / 2;
sizeTemp = size(temp);
removeInds = [];
k = 0;
for j = 1:1:max(sizeTemp)
if and(temp(j) > a, less_equal(temp(j), b) == 1)
k = k + 1;
removeInds(k) = j;
end
end
temp(removeInds) = [];
end
end
filter_hist = transpose(temp);
%Determines when 'a' less or equal to 'b' by accuracy
function less_equal = less_equal(a, b)
delta = 10^-6; %Accuracy
if a < b
less_equal = 1;
return;
end
if abs(b - a) < delta
less_equal = 1;
return;
end
less_equal = 0;
You can do something like this
nelements=nelements((nelements >5))