r/dailyprogrammer • u/Garth5689 • Oct 23 '17
[2017-10-23] Challenge #337 [Easy] Minimize & Maximize
Description
This challenge is about finding minimums/maximums. The challenge consists of two similar word problems where you will be asked to maximize/minimize a target value.
To make this more of a programming challenge rather than using programming as a calculator, I encourage you to try to find a generic minimize/maximize function rather than just calculating the answer directly.
Challenge
A piece of wire 100 cm in length is bent into the shape of a sector of a circle. Find the angle of the sector that maximizes the area A of the sector. sector_image
A and B are towns 20km and 30km from a straight stretch of river 100km long. Water is pumped from a point P on the river by pipelines to both towns. Where should P be located to minimize the total length of pipe AP + PB? river_image
Challenge Outputs
The accuracy of these answers will depending how much precision you use when calculating them.
- ~114.6
- ~40
Credit
This challenge was adapted from The First 25 Years of the Superbrain. If you have an idea for a challenge please share it on /r/dailyprogrammer_ideas and there's a good chance we'll use it.
4
u/[deleted] Oct 23 '17
Is there a mixup in the challenge outputs?
25
u/[deleted] Oct 23 '17
How can I get good at these.....
6
u/thestoicattack Oct 23 '17
Both these problems have closed-form Calc I solutions. It's a nice example where a little thinking ahead of time can save you a lot of computation. I gave the derivations in comments. Bonus: thanks to C++17 constexpr, the results are allowed to be computed at compile time if the inputs are known. Disassembling my executable essentially gives (pseudocode)
print "114.592"; print "40";5
u/Scara95 Oct 23 '17 edited Oct 23 '17
Common Lisp Recursive golden-ratio-search implementation, minimize the calls to the function parameter in case it's long to compute
2
u/LagashNight Oct 23 '17 edited Oct 23 '17
Recursive implementation of the Golden Section Search in Haskell, taking a generalized unimodal function be minimized.
Output: (114.59109737089062,39.999993127429065)
More precise results can be obtained by lowering the tolerance parameter.
There's an easy visualization for both problems. For the first, the solution is then the sum of the lengths of the radii is equal to the length of the arc, which then yields the answer of 2 radians. The second has a solution when the angle of incidence is equal to the angle of reflection. This is because the problem is still the same if we reflect B over the river, and in that case the solution is clearly a straight line, which then yields 40 as the answer.
2
u/Shamoneyo Oct 23 '17
It bothers me that you wrote AP + PB
2
u/lpreams Nov 09 '17
Part 1
C = angle (degrees) (this is what we're looking for)
a = area (maximize this)
r = radius of circle
l = arc length
L = total length (this is fixed at 100, but it actually drops out after differentiation, so any value of L will give the same result for C)
l = 2*pi*r*C/360 // arc length formula
L = 2*r+l // the total length is sum of arc length and two radii
L = 2*p\i*r*C/360 + 2*r // Combine those last two, getting rid of l
r = 180*L / (pi*C + 360) // Rearrange previous equation so we have r in terms of C
a = pi*r*r*(C/360) // circle sector area formula
a = 90*L2 *pi*C / ((pi*C + 360)2 ) // plug in r and simplify
a' = -90*pi*L2 *(pi*C - 360) / ((pi*C + 360)3 ) // here's the calculus bit, if we're being honest, I plugged the last line into wolframalpha because I'm too lazy to do the differentiation myself
0 = -90*pi*L2 *(pi*C - 360) / ((pi*C + 360)3 ) // set the derivative equal to zero to find the maximum
C = 360/pi ~ 114.6 // solve for C
Notice that L totally dropped out of the math, so the angle 360/pi degrees will always maximize the area, no matter the length of wire
Part 2
A = distance from river to A
B = distance from river to B
R = length of river
P = pumping station location along river
AP = distance from A to P
BP = distance from B to P
L = total length of pipeline (minimize this)
L = AP + PB // total pipeline is sum of two individual pipelines
AP = sqrt(A*A + P*P) // pythagorean
PB = sqrt(B*B + (R-P)2) // pythagorean
L = sqrt(A*A + P*P) + sqrt(B*B + (R-P)2) // substitute
L' = P / sqrt(A*A+P*P) + (P-R) / (B*B + (P-R)2 ) // Again, wolframalpha is my friend
// After setting the above to zero, and another run through wolframalpha, we get
P = (A*R) / (A + B)
Plugging in, P = (20*100)/(20+30) = 40
The result here depends on A, B, and R
1
u/Zigity_Zagity Oct 23 '17
Python 3, #2. Using scipy as an import is kind of like cheating.
Output: 40.0
1
u/mn-haskell-guy 1 0 Oct 23 '17 edited Oct 23 '17
Using automatic differentiation and Newton's method:
Edit: Updated to display the number of iterations.
Output:
1
u/g00glen00b Oct 23 '17
Using JavaScript:
Challenge 1:
Challenge 2:
Output:
1
u/Scroph 0 0 Oct 23 '17 edited Oct 24 '17
Brute force solution for the second problem because I'm too much of a brainlet to understand the first. Wouldn't increasing the angle mean that the area increases as well ?
Edit : updated the solution to include the first problem thanks to /u/mn-haskell-guy's explanation.
1
u/[deleted] Oct 23 '17
Well i super lazily converted a program i wrote years ago for class so that it could solve the second problem. It uses brent's method and is probably one of the fastest numerical solutions available for 1D optimization problems (although you do waste time calculating the derivative....).
Here is the output when run on my laptop compiled with icc -std=c++11 -O3 test.cpp -o test.
Output:
Really really bad source code:
1
u/MattieShoes Oct 23 '17
go... Implemented golden section search, which is passed a function. Used that to answer both questions.
Output:
1
u/capitalpm Oct 24 '17
Done in Python 3.5. Tries to ensure the minimum is actually bracketed before beginning the golden section minimization, but is a little sloppy.
1
u/__dict__ Oct 24 '17
Ruby. I'm sure there's a better way to pass these functions as blocks, but I couldn't figure it out.
1
u/kandidate Oct 26 '17 edited Oct 26 '17
Python 3 Brand new to programming. Would love feedback.
Edit: I put in the second problem
1
u/Sickysuck Oct 26 '17
These are pretty easy to do by hand with a little calculus.
1
u/Williamboyles Nov 04 '17
My quickly-coded Python solution:
1
u/limeeattack Dec 28 '17 edited Dec 28 '17
C++
It looks like shit, however using Newton's method and bit of high school maths one can find an approximate solution quite easily. It's a nice problem though, being new to C++(and programming in general) I learned that one can pass functions as arguments, which is bloody awesome!
1
u/ironboy_ Oct 23 '17
JavaScript