If-then constraints in integer linear programming - if-statement

I have a 30*40 matrix. Lets say the components in the matrix are specified with "P" and the related number of the row and column of each "P" is specified by "X" and "Y" accordingly. I have a model that the output should give us the P, X and Y. How can I define constraints (for solving a simplex) which connect P with it's exact X and Y?
I want to say for example:
if X=1 and Y=1 then
P= 0.1
if X=1 and Y=2 then
P= 0.5
if X=1 and Y=3 then
P= 0.8
and so on.
I dont want the model to return a P that does not match it's location in the matrix. How can I achieve this?
This is the objective function

Related

Inserting variable's values into a matrix - Stata

I generated a 10x10 matrix, and a 10-observation variable (its values don't matter).
I want to insert all the variable values into the first column of the matrix.
I'm struggling with "subsetting" both the variable and the matrix.
matrix M = J(10, 10, .)
egen V = seq(), f(1) t(10)
matrix M[1:_N, 1] = V[1:_N]
If the expression on the right evaluates to a matrix (not a scalar), this will replace the submatrix with the top left element given, so only the the top left element on the left side of the equation will be enough.
As for the variable subscripting, I'm afraid you can refer to one observation only. You will need an extra step to put data from a variable into a matrix, using mkmat.
clear
set obs 10
matrix M = J(10, 10, .)
egen V = seq(), f(1) t(10)
mkmat V in 1/10 // Put observations 1 to 10 from variable V into matrix V
matrix M[1,1] = V // Replace submatrix of M with top left element 1,1 with V

Using LAPACK's DORMQR with non-square Q

I want use LAPACK to calculate Q * x and Q^T * x, where Q comes from the reduced QR factorization of an m by n matrix A (m > n), stored in the form of Householder reflectors and a vector tau, as obtained from DGEQRF and x is a vector of length n in the case of Q * x and length m in the case of Q^T * x.
The documentation of DORMQR states that x is overwritten with the result, which already confuses me, since x and Q * x obviuosly have different dimensions if the original matrix A and subsequently its reduced Q are not square. Furthermore it states that
"Q is of order M if SIDE = 'L' and of order N if SIDE = 'R'."
In my case, only the first half applies and M refers to the length of x. What do they mean by order? I have rarely ever heard the term "order" in the context of non-square matrices, and if so, it would be something like m by n, and not just a single number. Do they mean rank?
Can I even use DORMQR to calculate both Q * x and Q^T * x for a non-square Q, or is it not designed for this? Do I need to pad x with zeros?
DORMQR applies only to Q a square matrix. Although the input A to the procedure relates to elementary reflectors, such as output of DGEQRF which can be more general, the documentation has the additional restriction that Q "is a real orthogonal matrix".
Of course, to be orthogonal, Q must be square.

Confusion about formula for Linear regression with gradient descent, (Pseudocode)

I'm made a program that calculates the line of best fit of a set of data points using gradient descent. I generate a 1000 random points and then it calculates the line of best fit training on these 1000 points. My confusion lies in the theory of my code.
In the part of my code where the training function is, by using the current m and b values for y= mx +b, the function makes a guess of the y values when it goes through the training points x values. This is supervised learning, so I know what the actual y value is, the function calculates the error and using that error adjusts the m and b values. <-- What is happening in the program when adjusting the line of best fit
I get everything above ^. what I'm confused about is the part of the code that calculates how to adjust these m and b values. Here it is:
guess = m * x + b;
error = y - guess;
m = m + (error * x) * learningrate;
b = b + error * learningrate;
Im confused about why we add instead of subtract that delta m (the (error * x) *learningrate)) part. Ignoring the learningrate, the error * x part is the partial derivative of the error with respect to m. But if we took the partial derivative of something with respect to something, wouldn't it give us the direction of the steepest ascent? Shouldn't we go the opposite direction (subtract the delta m) to get the proper m value? Isn't our goal to reduce the error?
Surprisingly to me, the above code works, if you add the delta m, it adjusts the m and b values in the right direction. So basically my question is: Why aren't we subtracting the delta m part (error *x) as it is pointing in the direction of steepest ascent, and we want to get the opposite of that?
Thanks!

How to only sum up values of a list in Prolog?

I've been given this code below:
returnValues(Value):-
collection(X,Y,Z),
total([X,Y|Z],Value).
This does work, but it sums up X,Y and Z. I'm looking for it to sum up Z only.
Z is a list and I want to sum up the Values of Z.
Total is:
total([],0).
total([Item|List],Sum):-
salary(Item,A),
total(List,Rest),
Sum is B + Rest.
I've tweaked the code a few times so that it only gives me values of X and Z, and only values of Y and Z, but I haven't been able to get values of Z only.
Sample input:
questionChildrenIncome(Name,Surname,CombinedIncome):-
family(Husband,person(Name,Surname,_,_),Children),
total(Children,CombinedIncome),
CombinedIncome<100000,
member(Child,Children),
salary(Child,Salary),
Salary<30000.
You could just ignore X,Y like:
returnValues(Value):-
collection(_,_,Z),
total(Z,Value).
For your second comment you could write:
exclude([],[]).
exclude([H|T],[H|T1]):-salary(H,Salary),Salary<30000,exclude(T,T1).
exclude([H|T],T1):- salary(H,Salary),Salary>=30000,exclude(T,T1).
and change to :
questionChildrenIncome(Name,Surname,CombinedIncome):-
family(Husband,person(Name,Surname,_,_),Children),
total(Children,CombinedIncome),
CombinedIncome<100000,
exclude(Children,Children)
.
You could also write more simply exclude predicate like:
exclude([]).
exclude([H|T]):-salary(H,Salary),Salary<30000,exclude(T).
which succeeds only if all children have salary<30000.

Python: Solving equation system (coefficients are arrays)

I can solve a system equation (using NumPY) like this:
>>> a = np.array([[3,1], [1,2]])
>>> b = np.array([9,8])
>>> y = np.linalg.solve(a, b)
>>> y
array([ 2., 3.])
But, if I got something like this:
>>> x = np.linspace(1,10)
>>> a = np.array([[3*x,1-x], [1/x,2]])
>>> b = np.array([x**2,8*x])
>>> y = np.linalg.solve(a, b)
It doesnt work, where the matrix's coefficients are arrays and I want calculate the array solution "y" for each element of the array "x". Also, I cant calculate
>>> det(a)
The question is: How can do that?
Check out the docs page. If you want to solve multiple systems of linear equations you can send in multiple arrays but they have to have shape (N,M,M). That will be considered a stack of N MxM arrays. A quote from the docs page below,
Several of the linear algebra routines listed above are able to compute results for several matrices at once, if they are stacked into the same array. This is indicated in the documentation via input parameter specifications such as a : (..., M, M) array_like. This means that if for instance given an input array a.shape == (N, M, M), it is interpreted as a “stack” of N matrices, each of size M-by-M. Similar specification applies to return values, for instance the determinant has det : (...) and will in this case return an array of shape det(a).shape == (N,). This generalizes to linear algebra operations on higher-dimensional arrays: the last 1 or 2 dimensions of a multidimensional array are interpreted as vectors or matrices, as appropriate for each operation.
When I run your code I get,
>>> a.shape
(2, 2)
>>> b.shape
(2, 50)
Not sure exactly what problem you're trying to solve, but you need to rethink your inputs. You want a to have shape (N,M,M) and b to have shape (N,M). You will then get back an array of shape (N,M) (i.e. N solution vectors).