linear programming algorithm for maximum proceeds - math

You can find the problem here
First, I would assign a variable (x or y) to each quantity that is being solved for, and likewise write an equation for the quantity that is being maximized or minimized (cost, profit, amount, etc.) and call this the maximization or minimization equation. Then, I would have to write each constraint as an inequality, and number each inequality and graph the system, numbering each line on the graph as its corresponding inequality.
This should yield a shaded solution region with several "corners." Each corner is the intersection of two constraint inequalities. I would have to find the coordinates of the corners by solving the systems of intersecting equations.
Using the found coordinates, I would then plug the coordinates of the corners into the maximization/minimization equation. The coordinates that give the largest or smallest value for this equation (depending on what the problem is looking for, and in this case the maximum proceeds) are the solution to the problem.
Could anyone provide a rough hint or pseudocode for it? I know the rough outline of what I have to do, but I just don't know how I could go about attacking it. For sure I would have to construct inequalities to act as the constraints, and in this case I'm solving for maximization.
Example: Jimmy is baking cookies for a bake sale. He is making chocolate chip and oatmeal raisin cookies. He gets 25 cents for each chocolate chip cookie and 30 cents for each oatmeal raisin cookie. He cannot make more than 500 cookies of each kind, and he cannot make more than 800 cookies total. He must make at least one-third as many chocolate chip cookies as oatmeal raisin cookies. How many of each kind of cookie should he make to get the most money?
Variables: x = number of chocolate chip cookies (in hundreds)
y = number of oatmeal raisin cookies (in hundreds)
Maximization equation: Profit = 25x + 30y
Constraints:
1.x≤5
2.y≤5
3.x + y≤8
4.x≥y
Corners:
1 and 3: x = 5, x + y = 8. (x, y) = (5, 3).
2 and 3: y = 5, x + y = 8. (x, y) = (3, 5).
2 and 4: y = 5, x = y. (x, y) = (, 5).
Plug into maximization equation:
(5, 3)Profit = 25(5) + 30(3) = 215.
(3, 5)Profit = 25(3) + 30(5) = 225.
(, 5)Profit = 25() + 30(5) = 191.67.
Thus, the x and y values which maximize the profit are (x, y) = (3, 5).
Jimmy should bake 300 chocolate chip cookies and 500 oatmeal raisin
cookies.

Ideally, you would've asked this problem on Math StackExchange since this is not a programming problem in the Computer Science sense, but instead a "linear program" in the mathematical sense. Anyways, to solve your problem. First assign variables for each of the ingredients:
x_1 := Sugar
x_2 := Flour
x_3 := Dried Fruit
x_4 := Chocolate Chips
The first four constraints are straightforward:
x_1 <= 20
x_2 <= 20
x_3 <= 5
x_4 <= 5
We want to maximize profit, so the objective is
max. z = 10(y_1) + 12(y_2) + 14(y_3) + 7(y_4) + 2(y_5)
where
y_1 = 0.5(x_1) + x_2 + 0.5(x_4)
y_2 = x_1 + 0.5(x_2) + x_3
y_3 = 0.5(x_1) + 2.5(x_3) + 0.25(x_4)
y_4 = 2(x_1)
y_5 = x_1 + 2(x_2) + x_3 + x_4
Now substitute y_1, ..., y_5 into the objective function z in order to obtain the objective function as an immediate function of x_1, ..., x_4.

Related

Line of Intersection between 2 Planes

I watched and tried to understand bunch of sites and videos about this, and I came into a weird conclusions and some questions. I need some help to explain which one of the method is right, or even both of them are right (but I got different result from each methods).
I'm sorry that I'm bad at explaining things, the first method is solve the equations normally. But, here the link for the video I tried to learn from
https://www.youtube.com/watch?v=o7CfCDkRwfY
Second method is to do cross product for the direction and find the point by set one of the variables as 0. https://www.youtube.com/watch?v=jozabh0lFmo
I tried for this example
x+2y+z−1=0
2x+3y−2z+2=0
and turned out for different answers. Is both of the method are correct, or which one? Thank you.
You have two equations with three unknowns.
You can eliminate one variable and solve for a relationship between the remaining two. Let's eliminate z.
Multiply the first equation by 2:
2x + 4y + 2z = 2
Add this to the second equation:
4x + 7y = 0
You can solve for y as a function of x:
y = -4x/7
Substitute this back into the first equation:
x - 8x/7 + z = 1
Simplify by combining the first and second terms:
-x/7 + z = 1
Solve for z:
z = 1 + x/7
Now you have an equation for the line in 3D space.
-inf <= x <= +inf
y = -4x/7
z = 1 + x/7
Both your equations are satisfied by these two points. Since two points are enough to define a line in Euclidean space I'd say I've got the correct answer.
This line goes through the point (0, 0, 1). It also goes through (7, -4, 2)
Here's a parametric representation of that line for -inf <= t <= +inf:
(x, y, z) = (0, 0, 1) + t*(7, -4, 1)
You can see that when t = 0 (x, y, z) = (0, 0, 1) (first point above) and when t = 1 (x, y, z) = (7, -4, 2) (second point above).
I didn't look at either of your videos. This is how I'd solve it.
This is high school algebra.

Avoiding bias in randomly generated subtraction problems

I'm writing a Python script to generate problems for mental arithmetic drills. The addition and multiplication ones were easy, but I'm running into trouble trying to generate unbiased problems for the subtraction ones.
I want to be able to specify a minimum and maximum value that the minuend (first number) will be -- e.g., for two-digit subtraction it should be between 20 and 99. The subtrahend should also have a range option (11-99, say). The answer needs to be positive and preferably also bounded by a minimum of, say, 10 for this situation.
So:
20 < Minuend < 99
11 < Subtrahend < 99
Answer = Minuend - Subtrahend
Answer >= 10
All the numeric values should be used as variables, of course.
I have these conditions met as follows:
ansMin, ansMax = 10, 99
subtrahendMin, minuendMax = 11,99
# the other max and min did not seem to be necessary here,
# and two ranges was the way I had the program set up
answer = randint(ansMin, ansMax)
subtrahend = randint(subtrahendMin, minuendMax - answer)
minuend = answer + subtrahend # rearranged subtraction equation
The problem here is that the minuend values wind up being nearly all over 50 because the answer and subtrahend were generated first and added together, and only the section of them that were both in the bottom 25% of the range will get the result below 50%. (Edit: that's not strictly true -- for instance, bottom 1% plus bottom 49% would work, and percentages are a bad way of describing it anyway, but I think the idea is clear.)
I also considered trying generating the minuend and subtrahend values both entirely randomly, then throwing out the answer if it didn't match the criteria (namely, that the minuend be greater than the subtrahend by a value at least greater than the answerMin and that they both be within the criteria listed above), but I figured that would result in a similar bias.
I don't care about it being perfectly even, but this is too far off. I'd like the minuend values to be fully random across the allowable range, and the subtrahend values random across the range allowed by the minuends (if I'm thinking about it right, this will be biased in favor of lower ones). I don't think I really care about the distribution of the answers (as long as it's not ridiculously biased). Is there a better way to calculate this?
There are several ways of defining what "not biased" means in this case. I assume that what you are looking for is that every possible subtraction problem from the allowed problem space is chosen with equal probability. Quick and dirty approach:
Pick random x in [x_min, x_max]
Pick random y in [y_min, y_max]
If x - y < answer_min, discard both x and y and start over.
Note the bold part. If you discard only y and keep the x, your problems will have an uniform distribution in x, not in the entire problem space. You need to ensure that for every valid x there is at least one valid y - this is not the case for your original choice of ranges, as we'll see later.
Now the long, proper approach. First we need to find out the actual size of the problem space.
The allowed set of subtrahends is determined by the minuend:
x in [21, 99]
y in [11, x-10]
or using symbolic constants:
x in [x_min, x_max]
y in [y_min, x - answer_min]
We can rewrite that as
x in [21, 99]
y = 11 + a
a in [0, x-21]
or again using symbolic constants
x in [x_min, x_max]
y = y_min + a
a in [0, x - (answer_min + y_min)].
From this, we see that valid problems exist only for x >= (answer_min + y_min), and for a given x there are x - (answer_min + y_min) + 1 possible subtrahents.
Now we assume that x_max does not impose any further constraints, e.g. that answer_min + y_min >= 0:
x in [21, 99], number of problems:
(99 - 21 + 1) * (1 + 78+1) / 2
x in [x_min, x_max], number of problems:
(x_max - x_min + 1) * (1 + x_max - (answer_min + y_min) + 1) / 2
The above is obtained using the formula for the sum of an arithmetic sequence. Therefore, you need to pick a random number in the range [1, 4740]. To transform this number into a subtraction problem, we need to define a mapping between the problem space and the integers. An example mapping is as follows:
1 <=> x = 21, y = 11
2 <=> x = 22, y = 12
3 <=> x = 22, y = 11
4 <=> x = 23, y = 13
5 <=> x = 23, y = 12
6 <=> x = 23, y = 11
and so on. Notice that x jumps by 1 when a triangular number is exceeded. To compute x and y from the random number r, find the lowest triangular number t greater than or equal to r, preferably by searching in a precomputed table; write this number as q*(q+1)/2. Then x = x_min + q-1 and y = y_min + t - r.
Complete program:
import random
x_min, x_max = (21, 99)
y_min = 11
answer_min = 10
triangles = [ (q*(q+1)/2, q) for q in range(1, x_max-x_min+2) ]
upper = (x_max-x_min+1) * (1 + x_max - (answer_min + y_min) + 1) / 2
for i in range(0, 20):
r = 1 + random.randrange(0, upper)
(t, q) = next(a for a in triangles if a[0] >= r)
x = x_min + q - 1
y = y_min + t - r
print "%d - %d = ?" % (x, y)
Note that for a majority of problems (around 75%), x will be above 60. This is correct, because for low values of the minuend there are fewer allowed values of the subtrahend.
I can see a couple of issues with your starting values - if you want the answer to always be greater than 10 - then you need to either increase MinuendMin, or decrease SubtrahendMin because 20-11 is less than 10... Also you have defined the answer min and max as 3,9 - which means the answer will never be more than 10...
Apart from that I managed to get a nice even distribution of values by selecting the minuend value first, then selecting the subtrahend value based on it and the answerMin:
ansMin = 10
minuendMin, minuendMax = 20,99
subtrahendMin = 9;
minuend = randint(minuendMin, minuendMax )
subtrahend = randint(subtrahendMin,(minuend-ansMin) )
answer = minuend - subtrahend
You say you've already got addition working properly. Assuming you have similar restrictions for the addends/sum you could rearrange the factors so that:
minuend <= sum
subtrahend <= first addend
answer <= second addend
A similar mapping can be made for multiplication/division, if required.

Calculating where two objects should meet when they have a set position at start

What I am trying to do is if I place two objects [A and B] at certain positions, how can I find where C should be? The velocities are not the same. Object A's velocity is 30 m/s, Object B's is 20 m/s.
In the picture, I have drawn that the velocities are the same. It should give you a general idea of what I am trying to do, though.
I have been messing around with this, but I do not even know where to start.
Thank you in advanced to all of you who reply.
First, write two equations expressing the x position of each point at time t.
xpos_a(t) = original_xpos_a + xvelocity_a * t
xpos_b(t) = original_xpos_b + xvelocity_b * t
when the two points collide, their x positions will be equal. set xpos_a equal to xpos_b and solve for t.
original_xpos_a + xvelocity_a * t = original_xpos_b + xvelocity_b * t
xvelocity_a * t - xvelocity_b * t = original_xpos_b - original_xpos_a
t * (xvelocity_a - xvelocity_b) = original_xpos_b - original_xpos_a
t = (original_xpos_b - original_xpos_a) / (xvelocity_a - xvelocity_b)
there are three possible outcomes when you solve for t:
both points have identical original position and velocity. t = 0/0; the collision might occur at any time.
the points have identical velocty but different original positions. t = [some nonzero number]/0; the collision can never occur.
the points have different velocity and different original positions. t = some real number. If the points do collide, the collision can only occur at this time.
Perform these same steps for Y (and Z, if the problem is three dimensional). Compare the t values from each dimension. There are four possible outcomes:
any of the t values are "the collision can never occur". The collision will never occur.
two or more of the t values are real numbers that are not equal. The collision will never occur.
all t values are "the collision could occur at any time". The points are constantly colliding at every point on their trajectories.
all of the t values that are real numbers are equal to one another. The collision will occur at that time. (if that time is negative, the collision occurs before you started your simulation; you may or may not want to count this as, "the collision will never occur")
If you end up in the final category, take the time of collision and plug it into the xpos_a, ypos_a, zpos_a functions to get the spatial coordinates of the collision.
Given points A and B, and Vectors C (velocity of A) and D (velocity of B).
A = (x1, y1) >start point of A
B = (x2, y2) >start point of B
C = (q1, w1) >constant velocity of A
D = (q2, w2) >constant velocity of B
NOTE: x1, y1, x2, y2, q1, q2, w1, w2 are all constants
EDIT: Follow works given that A & C and b& D aren't co-linear (just find time of collision if they do collide as Kevin points out)
Do linearization(can't find a good reference) of them:
EQ1 => (x-x1)(w1/q1)=(y-y1) >> (x-x1)(w1/q1) + y1 = y <br>
EQ2 => (x-x2)(w2/q2)=(y-y2) >> (x-x2)(w2/q2) + y2 = y <br>
EQ1 => (w1/q1) * x + y1 - x1(w1/q1) = y
m1 * x +( b1 ) = y
EQ2 => (w2/q2) * x + y2 - x2(w2/q2) = y
m2 * x +( b2 ) = y
Solve EQ1 and EQ2 for x and y
x = (b2 - b1)/(m1 - m2)
y = m1 * x + b1 OR m2 * x + b2
The (x,y) referred to later ^
Solve for t1 or t2
t1 = (x-x1) / q1
t2 = (x-x2) / q2
Check if t1 and t2 is true for the ys
t1 ?= (y-y1) / w1
t2 ?= (y-y2) / w2
If they are the same, then yes they collide, at (x,y)
NOTE: Due to round off error unless everything is perfectly calcualted before hand, most likely nothing will collide

How to find 10 values, exponentially distributed, which sum to a value, x

I have a value, for example 2.8. I want to find 10 numbers which are on an exponential curve, which sum to this value.
That is, I want to end up with 10 numbers which sum to 2.8, and which, when plotted, look like the curve below (exponential decay). These 10 numbers should be equally spaced along the curve - that is, the 'x-step' between the values should be constant.
This value of 2.8 will be entered by the user, and therefore the way I calculate this needs to be some kind of algorithm that I can program (hence asking this on SO not Math.SE).
I have no idea where to start with this at all - any ideas?
You want to have 10 x values equally distributed, i.e. x_k = a + k * b. They shall fulfill sum(exp(-x_k)) = v with v being your target value (the 2.8). This means exp(-a) * sum(exp(-b)^k) = v.
Obviously, there is a solution for each choice of b if v is positive. Set b to an arbitrary value, and calculate a from it.
E.g. for v = 2.8 and b = 0.1, you get a = -log(v / sum(exp(-b)^k)) = -log(2.8/sum(0.90484^k)) = -log(2.8/6.6425) = -log(0.421526) = 0.86387.
So for this example, the x values would be 0.86387, 0.96387, ..., 1.76387 and the y values 0.421526, 0.381412, 0.345116, 0.312274, 0.282557, 0.255668, 0.231338, 0.209324, 0.189404, 0.171380.
Update:
As it has been clarified that the curve can be scaled arbitrarily and the xs are preferred to be 1, 2, 3 ... 9, this is much more simple.
Assuming the curve function is r*exp(-x), the 10 values would be r*exp(-1) ... r*exp(-9). Their sum is r*sum(exp(-x)) = r*0.58190489. So to reach a certain value (2.8) you just have to adjust the r accordingly:
r = 2.8/sum(exp(-x)) = 4.81178294
And you get the 10 values: 1.770156, 0.651204, 0.239565, 0.088131, 0.032422, 0.011927, 0.004388, 0.001614, 0.000594.
If I understand your question correctly then you want to find x which solves the equation
It can be solved as
(just sum numbers as geometric progression)
The equation under RootOf will always have 1 real square different from 1 for 2.8 or any other positive number. You can solve it using some root-finding algorithm (1 is always a root but it does not solve original task). For constant a you can choose any number you like.
After computing the x you can easily calculate 10 numbers as .
I'm going to generalize and assume you want N numbers summing to V.
Since your numbers are equally spaced on an exponential you can write your sum as
a + a*x + a*x^2 + ... + a*x^(N-1) = V
Where the first point has value a, and the second a*x etc.
You can take out a factor of a and get:
a ( 1 + x + x^2 + ... + x^(N-1) ) = V
If we're free to pick x then we can solve for a easily
a = V / ( 1 + x + x^2 + .. x^(N-1) )
= V*(x+1)/(x^N-1)
Substituting that back into
a, a*x, a*x^2, ..., a*x^(N-1)
gives the required sequence

Simulating movement in 2D

I need assistance in simulating movement between 2 points in a plane. Consider two points P1:(x,y1) and P2:(x2,y2). I compute the distance between P1 and P2, say D, and I choose a random velocity, say V. Next, I compute the time required to move from P1 to P2, say T. Finally, I compute the equation of the straight line between P1 and P2 as y = mx + b.
For example, let T = 10 seconds. For the first 9 seconds, I would like to generate points per second on the straight line until I reach point P2 at the 10th second. Could you please assist me in doing so.
The best approach is to use parametric equations
x = x1 + t*(x2 - x1)
y = y1 + t*(y2 - y1)
where t is the "time" parameter going from 0 to 1 (0.5 means for example halfway).
If you also like your movement to be "soft" (starting from zero velocity, then accelerating then slowing down and stopping on the arrival point) you can use this modified equation
w = 3*t*t - 2*t*t*t
x = x1 + w*(x2 - x1)
y = y1 + w*(y2 - y1)
The following is a plot of the w curve compared to a linear distribution t with 11 points (t=0.0, 0.1, ... 0.9, 1.0):

Resources