Recently, I watched a cool mind your decsions video on an inscribed circle and rectangle puzzle. In the video they showed a diagram that was not scale. I wanted to get a sense of how these differently shaped areas will match.
There was a cool ratio between the outer and inner circle radii that is expressed as
Recently I came across a nice Ted-Ed video presenting a Fish Riddle.
I thought it would be fun to try solving it using Julia’s award winningJuMP package. Before we get started, please watch the above video-you might want to pause at 2:24 if you want to solve it yourself.
To attempt this problem in Julia, you will have to install the JuMP package.
JuMP provides an algebraic modeling language for dealing with mathematical optimization problems. Basically, that allows you to focus on describing your problem in a simple syntax and it would then take care of transforming that description in a form that can be handled by any number of solvers. Those solvers can deal with several types of optimization problems, and some solvers are more generic than others. It is important to pick the right solver for the problem that you are attempting.
The problem premises are:
1. There are 50 creatures in total. That includes sharks outside the tanks and fish
2. Each SECTOR has anywhere from 1 to 7 sharks, with no two sectors having the same number of sharks.
3. Each tank has an equal number of fish
4. In total, there are 13 or fewer tanks
5. SECTOR ALPHA has 2 sharks and 4 tanks
6. SECTOR BETA has 4 sharsk and 2 tanks
We want to find the number of tanks in sector GAMMA!
Here we identify the problem as mixed integer non-linear program (MINLP). We know that because the problem involves an integer number of fish tanks, sharks, and number of fish inside each tank. It also non-linear (quadratic to be exact) because it involves multiplying two two of the problem variables to get the total number or creatures. Looking at the table of solvers in the JuMP manual. pick the Bonmin solver from AmplNLWriter package. This is an open source solver, so installation should be hassle free.
We are now ready to write some code.
using JuMP, AmplNLWriter
# Solve model
m = Model(solver=BonminNLSolver())# Number of fish in each tank
@variable(m, n>=1, Int)# Number of sharks in each sector
@variable(m, s[i=1:3], Int)# Number of tanks in each sector
@variable(m, nt[i=1:3]>=0, Int)
@constraints m begin
# Constraint 2
sharks[i=1:3], 1<= s[i]<= 7
numfish[i=1:3], 1<= nt[i]# Missing uniqueness in restriction# Constraint 4
sum(nt)<= 13# Constraint 5
s == 2
nt == 4# Constraint 6
s == 4
nt == 2end# Constraints 1 & 3
@NLconstraint(m, s+s+s+n*(nt+nt+nt) == 50)# Solve it
status = solve(m)
@printf("We have %d fishes in each tank.\n", fish_in_each_tank)
@printf("We have %d tanks in sector Gamma.\n",tanks_in_each_sector)
@printf("We have %d sharks in sector Gamma.\n",sharks_in_each_sector)
In that representation we could not capture the restriction that “no two sectors having the same number of sharks”. We end up with the following output:
We have 4 fishes in each tank.
We have 4 tanks in sector Gamma.
We have 4 sharks in sector Gamma.
Since the problem domain is limited, we can possible fix that by adding a constrain that force the number of sharks in sector Gamma to be greater than 4.
This will result in an answer that that does not violate any of the stated constraints.
We have 3 fishes in each tank.
We have 7 tanks in sector Gamma.
We have 5 sharks in sector Gamma.
However, this seems like a bit of kludge. The proper way go about it is represent the number of sharks in the each sector as binary array, with only one value set to 1.
# Number of sharks in each sector
@variable(m, s[i=1:3,j=1:7], Bin)
We will have to modify our constraint block accordingly
After the model has been solved, we extract our output for the number of sharks.
…and we get the correct output.
This problem might have been an overkill for using a full blown mixed integer non-linear optimizer. It can be solved by a simple table as shown in the video. However, we might not alway find ourselves in such a fortunate position. We could have also use mixed integer quadratic programming solver such as Gurobi which would be more efficient for that sort of problem. Given the small problem size, efficiency hardly matters here.
Recently, I came across a fascinating blog and video from Mind you Decisions. It is about how a fraction
would show the Fibonacci numbers in order when looking at its decimal output.
On a spreadsheet and most standard programming languages, such output can not be attained due to the limited precision for floating point numbers. If you try this on R or Python, you will get an output of 1e-48. Wolfram alpha,however,allows arbitrary precision.
In Julia by default we get a little better that R and Python
We observe here that we are getting the first few Fibonacci numbers . We need more precision to get more numbers. Julia has arbitrary precision arithmetic baked into the language. We can crank up the precision of the BigFloat type on demand. Of course, the higher the precision, the slower the computation and the greater the memory we use. We do that by setprecision.
That is looking much better. However it we be nice if we could extract the Fibonacci numbers that are buried in that long decimal. Using the approach in the original blog. We define a function
and calculate the decimal
Here we use the non-standard string literal big"..." to insure proper interpretation of our input. Using BigFloat(1e-24)) would first construct at floating point with limited precision and then do the conversion. The initial loss of precision will not be recovered in the conversion, and hence the use of big. Now we extract our Fibonacci numbers by this function
Here we first convert our very long decimal number of a string and they we exploit the fact the Fibonacci numbers occur in blocks that 24 digits in length. We get out output in an array of BigInt. We want to compare the output with exact Fibonacci numbers, we just do a quick and non-recursive implementation.
Now we compare…
fib_frac=extract_fib(a);for i in eachindex(fib_frac)println(fib_exact[i], " ", fib_exact[i]-fib_frac[i])end
We get a long sequence, we just focused here on when the discrepancy happens.
The output shows that just before the extracted Fibonacci number exceeds 24 digits, a discrepancy occurs. I am not quite sure why, but this was a fun exploration. Julia allows me to do mathematical explorations that would take one or even two orders of magnitude of effort to do in any other language.