I'm studying for my algorithms final and I came across a practice problem that I can't seem to figure out. Here's the problem:
Consider the following set of difference constraints:
x1 - x4 <= 1
x2 - x1 <= 2
x2 - x4 <= 0
x3 - x1 <= 1
x4 - x1 <= -1
x4 - x3 <= -2
What is the solution that will be provided by the shortest path based algorithm for this set of constraints?
The answer that I got when I first did this was that x1 = 0, x2 = 0, x3 = -2, and x4 = -1. Unfortunately, this doesn't work because of the third constraint.
Can someone walk me through how to get the right answer using the shortest path based algorithm for this question? Thanks!
Related
I am interested in solving this kind of problem (interpolation/midpoint) :
Suppose I have this kind of data (I randomly generated data, it might not exactly fit this set up):
x1 = runif(100)
x2 = x1+ runif(100)
y1 = runif(100)
y2 = y1 + runif(100)
z1 = y2 - (runif(100)/3)
interpolation_data = data.frame(x1, x2, y1, y2, z1)
> head(interpolation_data)
x1 x2 y1 y2 z1
1 0.98851249 1.8413195 0.5362105 0.6490267 0.4130697
2 0.87617261 0.9825167 0.5628463 0.9246420 0.7357799
3 0.05901131 0.4298880 0.2463369 1.1585020 1.0614156
4 0.26843176 0.6694523 0.9467490 1.1860874 1.0859276
5 0.86021705 1.4767606 0.9956568 1.2048599 1.1932450
6 0.19368972 0.2269496 0.7618794 0.9763212 0.8594290
I want to write to calculate a value of Z2 for each row. I did this myself:
interpolation_data$z2 = (interpolation_data$x1 + ((interpolation_data$z1 - interpolation_data$y1) * (interpolation_data$x2 - interpolation_data$x1)))/(interpolation_data$y2 - interpolation_data$y1)
Are there any built-in ways in R to do this? I tried looking for something and nothing came up that exactly matched this kind of problem.
Thank you!
This is a simplified version of my current problem. I need to create a model.matrix from 2 model matrices, without loosing the info in "assign". For example, consider data and formula
y<-rnorm(100); x1<-rnorm(100); x2<-rnorm(100); x3<-rnorm(100)
f1 <- y ~ x1 + x2 + x3
and 2 model matrices X1 and X2 created using
trms<-terms.formula(f1)
trms2<-drop.terms(trms, dropx = 2)
trms3<-drop.terms(trms, dropx = -2)
X1<-model.matrix(trms2)
X2<-model.matrix(trms3)
Is there an easy way to create from X1 and X2 a matrix X with 1 intercept column and with attr(,"assign") that would have been obtained from f1?
I'm not completly sure if this is what you are trying to do but cbind() seems to work fine in this case.
X <- cbind(X1, X2)
X <- X[, !duplicated(colnames(X))]
You can then concatenate the attributes from X1 and X2. In order not to get duplicates you can only take the assign info from X2 which isn't already present in X1:
attributes(X)$assign <- c(attr(X1,"assign"), attr(X2,"assign")[!attr(X2,"assign") %in% attr(X1,"assign")])
If this is not what you were trying to to let us know.
If I understand the question correctly, how about something simple and direct like:
X3 <- cbind(X1[,1:2], X2[,2], X1[,3])
attr(X3,"assign") <- c(0,1,2,3)
colnames(X3) <- c("Intercept",attr(trms, "term.labels"))
head(X3)
Intercept x1 x2 x3
1 1 -1.28372461 -0.2598796 0.3028496
2 1 0.56880875 0.2803302 0.7593734
3 1 -0.32480770 -1.6705911 -1.1750247
4 1 -1.02761734 -0.1405454 -0.6805033
5 1 0.84218452 -0.1224962 -1.3882420
6 1 0.07221231 0.5587801 -0.9042751
I am new to linear algebra and I am trying to solve a system of three equations with five unknowns. The system I have is the following:
x1 + x2 + x3 + x4 + x5 = 1
-x1 + x2 + x3 - 2x4 - 2x5 = 1
2x1 + 2x2 - x3 - x4 + x5 = 1
So what I did was set up the augmented matrix like this:
1 1 1 1 1 1
-1 1 1 -2 -2 1
2 2 -1 -1 1 1
Then I try to obtain an identity matrix on the left side and end up with the following:
1 0 0 3/2 3/2 0
0 1 0 -3/2 -5/6 2/3
0 0 1 1 1/3 1/3
So I think the answer is x1 = 0, x2 = 2/3 and x3 = 1/3
But when I look in my answer sheet it reads:
(x1, x2, x3, x4, x5) = (0, 2/3, 1/3, 0, 0) + s(−3/2, 3/2, −1, 1, 0) + t(−3/2, 5/6, −1/3, 0, 1)
I have no idea how to interpret this. My x1,x2,x3 seems to match the first three in the first five-tuple but what are the other two five-tuples? Can someone explain what I am missing here? I would highly appreciate it.
A system of equations can be represented in matrix form as
Ax = b
where A is the matrix of coefficients, x is the column vector (x1, ..., xn) and b is the column vector with as many entries as equations are.
When b is not 0 we say that the system is not homogeneous. The associated homogeneous system is
Ax = 0
where the 0 on the right is again a column vector.
When you have a non-homogeneous system, like in this case, the general solution has the form
P + G
where P is any particular solution and G is the generic solution of the homogeneous system.
In your case the vector
P = (0, 2/3, 1/3, 0, 0)
satisfies all the equations and is therefore a valid particular solution.
The other two vectors (−3/2, 3/2, −1, 1, 0) and (−3/2, 5/6, −1/3, 0, 1) satisfy the homogeneous equations (take a moment to check this). And since there are 3 (independent) equations with 5 unknowns (x1..x5), the space of homogenous solutions can be generated by these two vectors (again because they are independent).
So, to describe the space of all homogeneous solutions you need two scalar variables s and t. In other words
G = s(−3/2, 3/2, −1, 1, 0) + t(−3/2, 5/6, −1/3, 0, 1)
will generate all homogeneous solutions as s and t take all posible real values.
I'm about to learn all about the simplex method in R project, unfortunately I crashed in this case:
We're running a 24h shop and we need to know how many employees do we need if there are six shifts (8-12,12-16 etc.) during the day, and one employee can work a maximum of 8 hours. Limits of the employees at one shift are:
0:00-4:00 < 5 4:00-8:00 < 7 8:00-12:00< 15 12:00-16:00 <10
16:00-20:00 <15 20:00-24:00 <9
I tried this:
library(boot)
a=c(1,1,1,1,1,1)
w1=c(1,1)
w2=c(1,1)
w3=c(1,1)
w4=c(1,1)
w5=c(1,1)
w6=c(1,1)
A1=rbind(w1,w2,w3,w4,w5,w6)
b1=c(5,7,15,10,15,9)
simplex(a=a,A1=A1,b1=b1,maxi=TRUE)
Error in`[<-`(`*tmp*`, , basic, value = iden(M)) :
index is out of borders
But it doesn't work.
The error occurs because the dimensions of the input matrices and vectors are not correct.
Since the coefficients vector a in your example has dimension 6, also the vector x in the objective function must have dimension 6. And since the b1 that is supplied to simplex() also has dimension 6, it follows that A1 in the constraint
A1 * x <= b1
must be a 6 x 6 matrix. However, A in your example is only a 6 x 2 matrix. This triggers the error message. (It would have been nicer if simplex() checked its inputs first and issued a more user friendly message...)
Here is an example with the right dimensions, which does work:
library(boot)
a = rep(1, 6) # vector with 6 ones
A1 = matrix(1, nrow=6, ncol=6) # 6x6 matrix with all ones
b1 = c(5, 7, 15, 10, 15, 9)
simplex(a=a, A1=A1, b1=b1, maxi=TRUE)
Note that this corrected example does not try to actually solve your specific simplex problem, it only illustrates correct usage of simplex().
In general it is worth carefully checking the input dimensions of the inputs to simplex(). They are explained nicely in the help pages:
?simplex
OK, I got it after 4 days :)) I post results here if anybody else'd have same problem. The main difficulty here is to calculate "people" as "number of hours at one shift"
a=c(1,1,1,1,1,1)
> w1=c(4,0,0,0,0,4)
> w2=c(4,4,0,0,0,0)
> w3=c(0,4,4,0,0,0)
> w4=c(0,0,4,4,0,0)
> w5=c(0,0,0,4,4,0)
> w6=c(0,0,0,0,4,4)
> b1=c(20,28,60,40,60,36)
> library(boot)
> simplex(a=a,A1=rbind(w1,w2,w3,w4,w5,w6)
+ ,b1=b1,maxi=T)
Linear Programming Results
Call : simplex(a = a, A1 = rbind(w1, w2, w3, w4, w5, w6), b1 = b1, maxi = T)
Maximization Problem with Objective Function Coefficients
x1 x2 x3 x4 x5 x6
1 1 1 1 1 1
Optimal solution has the following values
x1 x2 x3 x4 x5 x6
5 2 10 0 9 0
The optimal value of the objective function is 26.
CLOSED, Deletle subject or leave for others. Thank you admins for edit and #WhiteViking !
The approximation to the function max(x) can be written as a "noisy-OR" as given below:
maxk x = 1 - Πk(1 - x)
Are there any way to approximate min(x)?
If your values range between 0 and 1, then max is similar to the OR operator and min is similar to the AND operator. Similarly, the NOT operator can be thought of as 1 - x. Using De Morgan's laws, they're related by
x1 ∧ x2 ∧ x3 ... ∧ xk = (not x1) ∨ (not x2) ∨ ... ∨ (not xk)
Therefore, you should be able to approximate min{x1, x2, ..., xk} by computing 1 - max{1 - x1, 1 - x2, ..., 1 - xk}.
Hope this helps!