Initialize / warm-start search [duplicate] - or-tools

I am using the CP-Sat solver to optimise a timetable I am making. However, this now takes a long time to solve. Is it possible to seed the solver with an old result, to act as a starting point, with the goal of reducing the time required to find the optimal result?

Take a look at this solution hinting example:
https://github.com/google/or-tools/blob/stable/ortools/sat/docs/model.md#solution-hinting
num_vals = 3
x = model.NewIntVar(0, num_vals - 1, 'x')
y = model.NewIntVar(0, num_vals - 1, 'y')
z = model.NewIntVar(0, num_vals - 1, 'z')
model.Add(x != y)
model.Maximize(x + 2 * y + 3 * z)
# Solution hinting: x <- 1, y <- 2
model.AddHint(x, 1)
model.AddHint(y, 2)
Edit: you should also try to
Reduce the amount of variables.
Reduce the domain of the integer variables.
Run the solver with multiples threads usingsolver.parameters.num_search_workers = 8.
Prefer boolean over integer variables/contraints.
Set redundant constraints and/or symmetry breaking constraints.
Segregate your problem and merge the results.

Related

Matlab: Vectorizing 4 nested for loops

So, I need to vectorize some for loops into a single line. I understand how vectorize one and two for-loops, but am really struggling to do more than that. Essentially, I am computing a "blur" matrix M2 of size (n-2)x(m-2) of an original matrix M of size nxm, where s = size(M):
for x = 0:1
for y = 0:1
m = zeros(1, 9);
k = 1;
for i = 1:(s(1) - 1)
for j = 1:(s(2) - 1)
m(1, k) = M(i+x,j+y);
k = k+1;
end
end
M2(x+1,y+1) = mean(m);
end
end
This is the closest I've gotten:
for x=0:1
for y=0:1
M2(x+1, y+1) = mean(mean(M((x+1):(3+x),(y+1):(3+y))))
end
end
To get any closer to a one-line solution, it seems like there has to be some kind of "communication" where I assign two variables (x,y) to index over M2 and index over M; I just don't see how it can be done otherwise, but I am assured there is a solution.
Is there a reason why you are not using MATLAB's convolution function to help you do this? You are performing a blur with a 3 x 3 averaging kernel with overlapping neighbourhoods. This is exactly what convolution is doing. You can perform this using conv2:
M2 = conv2(M, ones(3) / 9, 'valid');
The 'valid' flag ensures that you return a size(M) - 2 matrix in both dimensions as you have requested.
In your code, you have hardcoded this for a 4 x 4 matrix. To double-check to see if we have the right results, let's generate a random 4 x 4 matrix:
rng(123);
M = rand(4, 4);
s = size(M);
If we run this with your code, we get:
>> M2
M2 =
0.5054 0.4707
0.5130 0.5276
Doing this with conv2:
>> M2 = conv2(M, ones(3) / 9, 'valid')
M2 =
0.5054 0.4707
0.5130 0.5276
However, if you want to do this from first principles, the overlapping of the pixel neighbourhoods is very difficult to escape using loops. The two for loop approach you have is good enough and it tackles the problem appropriately. I would make the size of the input instead of being hard coded. Therefore, write a function that does something like this:
function M2 = blur_fp(M)
s = size(M);
M2 = zeros(s(1) - 2, s(2) - 2);
for ii = 2 : s(1) - 1
for jj = 2 : s(2) - 1
p = M(ii - 1 : ii + 1, jj - 1 : jj + 1);
M2(ii - 1, jj - 1) = mean(p(:));
end
end
The first line of code defines the function, which we will call blur_fp. The next couple lines of code determine the size of the input matrix as well as initialising a blank matrix to store out output. We then loop through each pixel location in the matrix that is possible without the kernel going outside of the boundaries of the image, we grab a 3 x 3 neighbourhood with each pixel location serving as the centre, we then unroll the matrix into a single column vector, find the average and store it in the appropriate output. For small kernels and relatively large matrices, this should perform OK.
To take this a little further, you can use user Divakar's im2col_sliding function which takes overlapping neighbourhoods and unrolls them into columns. Therefore, each column represents a neighbourhood which you can then blur the input using vector-matrix multiplication. You would then use reshape to reshape the result back into a matrix:
T = im2col_sliding(M, [3 3]);
V = ones(1, 9) / 9;
s = size(M);
M2 = reshape(V * T, s(1) - 2, s(2) - 2);
This unfortunately cannot be done in a single line unless you use built-in functions. I'm not sure what your intention is, but hopefully the gamut of approaches you have seen here have given you some insight on how to do this efficiently. BTW, using loops for small matrices (i.e. 4 x 4) may be better in efficiency. You will start to notice performance changes when you increase the size of the input... then again, I would argue that using loops are competitive as of R2015b when the JIT has significantly improved.

Perceptron - MatLab Serious Confusion

This is my first stab at machine learning, and I can implement the code anyway that I want. I have Matlab access, which I think will be simpler than Python, and I have pseudo code for implementing a PLA. The last part of the code, however, absolutely baffles me, though it is simpler than the code I have seen on here thus far. It seems to be calling for the use of variables not declared. Here's what I have. I'll point out the number line at which I get stuck.
1) w <- (n + 1) X m (matrix of small random nums)
2) I <- I augmented with col. of 1s
3) for 1 = 1 to 1000
4) delta_W = (N + 1) X m (matrix of zeros) // weight changes
5) for each pattern 1 <= j <= p
6) Oj = (Ij * w) > 0 // j's are subscript/vector matrix product w/ threshold
7) Dj = = Tj - Oj // diff. between target and actual
8) w = w + Ij(transpose)*Dj // the learning rule
Lines 1 thru 4 are coded.
My questions are on line 5: What does "for each pattern mean" (i.e., how does one say it in code). Also, which j are they interested in, I have a j in the observation matrix and a j in the target matrix. Also, where did "p" come from (I have i's, j's, m's and n's but no p's)? Any thoughts would be appreciated.
"for each pattern" refers to the inputs. All they are saying is to run that loop where Ij is the input to the perceptron.
To write this in MATLAB, it really depends on how your data is oriented. I would store your inputs as a mXn matrix, where m is the number of inputs and n is the size of each input.
Say our inputs look like :
input = [1 5 -1;
2 3 2;
4 5 6;
... ]
First 'augment' this with a column of ones for the bias input:
[r c] = size(input);
input = [input ones(r,1)];
Then, your for loop will simply be:
for inputNumber = 1:r
pattern = input(inputNumber,:);
and you can continue from there.

Explain MATLAB function smooth(x,y,span,'moving')

I recently came across the Matlab smooth function used as follows:
ans = smooth(x, y, span, 'moving');
The Matlab documentation states
yy = smooth(x,y,...) additionally specifies x data. If x is not provided, methods that require x data assume x = 1:length(y). You should specify x data when it is not uniformly spaced or sorted. If x is not uniform and you do not specify method, lowess is used. If the smoothing method requires x to be sorted, the sorting occurs automatically.
However, I am unclear as to what this actually means for the 'moving' average case. Is x an index for the y data, and if so how do non-integer values of x affect the 'moving' average of y?
Moving average means each value of the yy (or ans in your case) is an average of the n closest points. https://en.wikipedia.org/wiki/Moving_average
There are several methods to calculate it - it depends on WHICH POINTS we will use. For example:
( (i-1) + (i-2) + ... + (i-n) )/n;
where n - is a span or linear filtration level.
This mean that first three points can't be calculated (there are no data for it). And sometimes result must be shifted (because really - average of the first 4 points not really corresponds to 4th elements).
So Matlab use another method:
yy(1) = y(1)
yy(2) = (y(1) + y(2) + y(3))/3
yy(3) = (y(1) + y(2) + y(3) + y(4) + y(5))/5
yy(4) = (y(2) + y(3) + y(4) + y(5) + y(6))/5
...
It's more useful.
About x and y - it is usual 2d-data: each x corresponds to each y. you can avoid setting of x, then matlab will use [1, 2, 3, ..., length(y)] sequence for this. But if you have some non-uniformly distributed data you have to set it for getting right result. So if you have non-integer values it will works correct, scaling it for x axis. Here the easiest example from my head:
Say you have data y corrupted by noise and let's assume y = [2.1, 3.2, 1.7, 4.5, 5.8, 6.9]. Let's say that you have decided to use moving average of 3 window filter to smooth y.
smoothedY1 = (2.1 + 3.2 + 1.7)/3 = 2.3333
smoothedY2 = (3.2 + 1.7 + 4.5)/3 = 3.1333
smoothedY3 = (1.7 + 4.5 + 5.8)/3 = 4.0000
smoothedY3 = (4.5 + 5.8 + 6.9)/3 = 5.7333
Pay attention to the way corrupted data is being shifted to the left by one element per iteration. Now let's use smooth() in Matlab.
y = [2.1, 3.2, 1.7, 4.5, 5.8, 6.9];
smooth(y, 3, 'moving')
The above script yields the following result
ans =
2.1000
2.3333 <----
3.1333 | (smoothed data)
4.0000 |
5.7333 <----
6.9000
To answer your original question, the "x" data is just used for sorting, but is otherwise ignored when the method is 'moving':
>> x = rand(10, 1);
>> y = (1:10)' + 0.1*randn(10,1);
>> isequal(smooth(x,y,'moving'), smooth(y,'moving'))
ans =
0
>> z = sortrows([x y], 1);
>> isequal(smooth(z(:,1),z(:,2),'moving'), smooth(z(:,2),'moving'))
ans =
1
The "x" values aren't actually taken into account for the averaging, they are just used to sort "y" by increasing index.

Matlab : How to represent a real number as binary

Problem : How do I use a continuous map - The Link1: Bernoulli Shift Map to model binary sequence?
Concept :
The Dyadic map also called as the Bernoulli Shift map is expressed as x(k+1) = 2x(k) mod 1. In Link2: Symbolic Dynamics, explains that the Bernoulli Map is a continuous map and is used as the Shift Map. This is explained further below.
A numeric trajectory can be symbolized by partitioning into appropriate regions and assigning it with a symbol. A symbolic orbit is obtained by writing down the sequence of symbols corresponding to the successive partition elements visited by the point in its orbit. One can learn much about the dynamics of the system by studying its symbolic orbits. This link also says that the Bernoulli Shift Map is used to represent symbolic dynamics.
Question :
How is the Bernoulli Shift Map used to generate the binary sequence? I tried like this, but this is not what the document in Link2 explains. So, I took the numeric output of the Map and converted to symbols by thresholding in the following way:
x = rand();
y = mod(2* x,1) % generate the next value after one iteration
y =
0.3295
if y >= 0.5 then s = 1
else s = 0
where 0.5 is the threshold value, called the critical value of the Bernoulli Map.
I need to represent the real number as fractions as explained here on Page 2 of Link2.
Can somebody please show how I can apply the Bernoulli Shift Map to generate symbolized trajectory (also called time series) ?
Please correct me if my understanding is wrong.
How do I convert a real valued numeric time series into symbolized i.e., how do I use the Bernoulli Map to model binary orbit /time series?
You can certainly compute this in real number space, but you risk hitting precision problems (depending on starting point). If you're interested in studying orbits, you may prefer to work in a rational fraction representation. There are more efficient ways to do this, but the following code illustrates one way to compute a series derived from that map. You'll see the period-n definition on page 2 of your Link 2. You should be able to see from this code how you could easily work in real number space as an alternative (in that case, the matlab function rat will recover a rational approximation from your real number).
[EDIT] Now with binary sequence made explicit!
% start at some point on period-n orbit
period = 6;
num = 3;
den = 2^period-1;
% compute for this many steps of the sequence
num_steps = 20;
% for each step
for n = 1:num_steps
% * 2
num = num * 2;
% mod 1
if num >= den
num = num - den;
end
% simplify rational fraction
g = gcd(num, den);
if g > 1
num = num / g;
den = den / g;
end
% recover 8-bit binary representation
bits = 8;
q = 2^bits;
x = num / den * q;
b = dec2bin(x, bits);
% display
fprintf('%4i / %4i == 0.%s\n', num, den, b);
end
Ach... for completeness, here's the real-valued version. Pure mathematicians should look away now.
% start at some point on period-n orbit
period = 6;
num = 3;
den = 2^period-1;
% use floating point approximation
x = num / den;
% compute for this many steps of the sequence
num_steps = 20;
% for each step
for n = 1:num_steps
% apply map
x = mod(x*2, 1);
% display
[num, den] = rat(x);
fprintf('%i / %i\n', num, den);
end
And, for extra credit, why is this implementation fast but daft? (HINT: try setting num_steps to 50)...
% matlab vectorised version
period = 6;
num = 3;
den = 2^period-1;
x = zeros(1, num_steps);
x(1) = num / den;
y = filter(1, [1 -2], x);
[a, b] = rat(mod(y, 1));
disp([a' b']);
OK, this is supposed to be an answer, not a question, so let's answer my own questions...
It's fast because it uses Matlab's built-in (and highly optimised) filter function to handle the iteration (that is, in practice, the iteration is done in C rather than in M-script). It's always worth remembering filter in Matlab, I'm constantly surprised by how it can be turned to good use for applications that don't look like filtering problems. filter cannot do conditional processing, however, and does not support modulo arithmetic, so how do we get away with it? Simply because this map has the property that whole periods at the input map to whole periods at the output (because the map operation is multiply by an integer).
It's daft because it very quickly hits the aforementioned precision problems. Set num_steps to 50 and watch it start to get wrong answers. What's happening is the number inside the filter operation is getting to be so large (order 10^14) that the bit we actually care about (the fractional part) is no longer representable in the same double-precision variable.
This last bit is something of a diversion, which has more to do with computation than maths - stick to the first implementation if your interest lies in symbol sequences.
If you only want to deal with rational type of output, you'll first have to convert the starting term of your series into a rational number if it is not. You can do that with:
[N,D] = rat(x0) ;
Once you have a numerator N and a denominator D, it is very easy to calculate the series x(k+1)=mod(2*x(k), 1) , and you don't even need a loop.
for the part 2*x(k), it means all the Numerator(k) will be multiplied by successive power of 2, which can be done by matrix multiplication (or bsxfun for the lover of the function):
so 2*x(k) => in Matlab N.*(2.^(0:n-1)) (N is a scalar, the numerator of x0, n is the number of terms you want to calculate).
The Mod1 operation is also easy to translate to rational number: mod(x,1)=mod(Nx,Dx)/Dx (Nx and Dx being the numerator and denominator of x.
If you do not need to simplify the denominator, you could get all the numerators of the series in one single line:
xn = mod( N.*(2.^(0:n-1).'),D) ;
but for visual comfort, it is sometimes better to simplify, so consider the following function:
function y = dyadic_rat(x0,n)
[N,D] = rat(x0) ; %// get Numerator and Denominator of first element
xn = mod( N.*(2.^(0:n-1).'),D) ; %'// calculate all Numerators
G = gcd( xn , D ) ; %// list all "Greatest common divisor"
y = [xn./G D./G].' ; %'// output simplified Numerators and Denominators
If I start with the example given in your wiki link (x0=11/24), I get:
>> y = dyadic_rat(11/24,8)
y =
11 11 5 2 1 2 1 2
24 12 6 3 3 3 3 3
If I start with the example given by Rattus Ex Machina (x0=3/(2^6-1)), I also get the same result:
>> y = dyadic_rat(3/63,8)
y =
1 2 4 8 16 11 1 2
21 21 21 21 21 21 21 21

how to eliminate complex number in a vector in Matlab

In Matlab, suppose there is a vector whose elements can be complex or real. I was wondering how to remove the nonreal elements, and consequently reduce the size of the vector? Thanks and regards!
Use the REAL and IMAG functions:
>> x = [1+i; 4+3i; 5+6i]
x =
1 + 1i
4 + 3i
5 + 6i
>> real(x)
ans =
1
4
5
>> imag(x)
ans =
1
3
6
EDIT
The above doesn't answer the poster's question. This does.
Use the FIND and REAL functions:
>> v = [1+i; 2; 3]
v =
1 + 1i
2
3
>> v(v == real(v))
ans =
2
3
You can also avoid testing in a loop using Matlab's vector syntax:
x = [1, 2, 3+1i, 4+2i, 5+3i, 6, 7, 8+4i, 9+0.000001i]
y = x(imag(x) == 0);
z = real(x(abs(imag(x)) < 0.00001));
y should be [1,2,6,7] and z should be [1,2,6,7,9]
imag(x) == 0 yields a logical vector with true values whenever the equality condition holds. x(<logical vector>) yields a new vector with only the elements of x where <logical vector> was true.
That's a very unusual thing to ask. Since the imaginary part is a floating point number, you can't really tell which number is a real number and which number is very close to a real number. Such function doesn't exist in Matlab since it's not very clear how it would be useful for anything (it doesn't make much sense to remove those numbers). Specifying your actual purpose here might help you get better answers.
If you want to ensure that only real numbers are left in the vector, you can use the following (it doesn't work with matrices and vertical rows, but you've got the idea):
x = [1, 2, 3+1i, 4+2i, 5+3i, 6, 7, 8+4i, 9+0i]
z = []
for k = [1:length(x)]
if imag(x(k)) == 0
z = [z, real(x(k))]
endif
endfor
If you want to keep all numbers that are close to a real number, but could have some small non-zero imaginary part, you can use the following:
x = [1, 2, 3+1i, 4+2i, 5+3i, 6, 7, 8+4i, 9+0.000001i]
z = []
for k = [1:length(x)]
if abs(imag(x(k))) < 0.00001
z = [z, real(x(k))]
endif
endfor
Of course, if you tell us what your actual criterion is, it would be much easier to give you a better idea. Are you looking for the real solutions to some sort of equation or system of equations, real roots of a polynomial? In this case, the first one might miss a real solution because of the approximation error, and the second one can give you things that aren't solutions.