Minimising Time to Rescue in the Beach Business with GPT4
I dialogued for 2 days with GPT 4 in this session. The results are the flawless mathematical derivations (!) and the fully working Python code (!) below in this article. 80% of code and derivations was GPT4 generated. I tweaked and curated and rewrote about 40% of that and added 20% of my own doing. As for the article’s English text itself, GPT4 could generate anything I asked, but I still wrote/rewrote most of it myself. donfrodo supplied the pictures using Midjourney.
This experiment is meant as an example case of how AI LLMs and humans can collaborate and achieve correct, valuable and more appealing results faster compared to working alone. GPT4 is truly astounding in many areas: natural language comprehension and production, a high degree of reasoning, mathematical derivation, computer code understanding, production and manipulation.
GPT4 certainly passes the Turing test in the context of this article. Or rather, fails it! Indeed, the Turing test was originally formulated as “should be indistinguishable from a human”. Clearly, Turing, in his era, only expected under-performance and not over-performance of machines. But in the sense that GPT4's answers are clearly better and come quicker compared to a human respondent, its non-human nature is easily detected. Turing test failure is a plus here.
1. Beach Business Introduction (not GPT generated)
At sea, a damsel is in distress. A lifeguard on the beach or already in the water wants to save her.
Always inclined to solve any problem to full optimality, the lifeguard realises that, given his different speeds on sand (higher) and in water (lower), the straight line from him to her is not always the quickest route.
He reasons he will spend less total time if he spends some more time running and some less time swimming, compared to that one straight line.
The point where to ideally transition from sand to water first has to be determined. He approaches the problem mathematically to determine the best strategy.
This is his story.
2. Sand to Water Case
2.1 Deriving the Route and its Total Time (maths derivation was GPT4 generated)
Given the scenario where the lifeguard would transition from sand to water, the total time taken can be derived based on the distance covered on sand and water, as well as their respective speeds.
Let’s assume the following:
The time taken for the sand portion from (x1, y1) to (x3, 0) is:
and for the water portion from (x3, 0) to (x2, y2):
The total time is:
2.2 Derivation of the Derivative and its Roots (maths derivation was GPT4 generated
By differentiating Ttotal with respect to x3 and setting the result to zero, one can find the optimal value of x3 that minimizes the total time.
Starting from:
Multiplying through by the common denominator gives:
Now, we’ll square both sides to eliminate the square roots:
Expanding both sides and equating them:
Grouping terms by their power of x3:
From the above grouping, the coefficients are derived as:
Putting it all together, we have the quartic equation in x3:
Given the complexity of quartic equations, finding its roots can be challenging, especially if it doesn’t factor conveniently. Hence, we employ numerical methods to find the optimal value of x3.
This can be seen in the python function find_optimal_x3 below, which calls the minimize function from the Python library scipy.optimize. So this results in a single variable optimisation.
2.3 Some Sand to Water Example Scenario’s (Python code to generate figures was GPT4 generated)
Figure 1 displays some sand to water scenario’s.
In columns 3 of figure 1, we assume the lifeguard can swim as quickly as he can run on the beach. In that case, the path with the least time is a straight line.
Compared to that scenario 3, in the two leftmost columns, we assume that the lifeguard runs faster than he can swim. The results is that for the least-time-path in those cases, he spends more time on the beach compared to the scenario 3 in columns 3.
In column 4, we assume the probably somewhat unrealistic case that our lifeguard swims faster than he runs. In that case he avoids spending too much time on the sand, where he is slower than in the water.
3 Water to Water Case
3.1 Deriving the Routes and their Total Time
3.1.1 Direct Swim Scenario (latex for maths was GPT4 generated)
Given the scenario where a person swims directly to the target point in the water, the time taken can be expressed as:
Where x1, y1 are the coordinates of the starting point in water, x2, y2 are the coordinates of the target point in water, and vwater is the speed of the person in water. This time does not have to be optimised as it is not a function of any variable, rather a function of constants only. However, its time value will be compared to the time taken for another route, as described in the next section.
3.1.2 Swim to Coast, then Run, then Swim Scenario (latex for maths was GPT4 generated)
Given the scenario where a person first swims to the coast, runs on the sand, and then swims again to reach the target point, the total time taken for the total route from (x2, y2) to (x3, 0) to (x4, 0) to (0, y1) is:
where:
- x1, y1 are the coordinates of the starting point in water.
- x2, y2 are the coordinates of the target point in water.
- x3 is the horizontal coordinate where the person exits the water onto the coast.
- x4 is the horizontal coordinate where the person enters the water again after running on the sand.
- vsand is the speed of the person on sand.
- vwater is the speed of the person in water.
The optimization problem aims to find the values of x3 and x4 (where the person enters the water again) that minimize Tcombined. This problem can’t be easily solved symbolically, necessitating the use of numerical and here also multivariable optimization techniques. This can be seen in the python function find_optimal_x3_x4 below, which calls the minimize function from the Python library scipy.optimize on a vector x. This vector contains the 2 variables x3 and x4. So this results in a multivariable optimisation.
3.2 Some Water to Water Example Scenario’s (Python code to generate figures was GPT4 generated)
Figure 2 displays some water to water scenario’s.
In the left two columns of figure 2, the lifeguard’s running speed over sand is larger than his swimming speed in water, so he benefits from swimming to land, then running along the shoreline and then swimming the last bit to his damsel.
In the right two columns his swimming speed is assumed equal respectively larger than his running speed. In those cases, the indirect route going over the beach will avoid running any distance on the beach and x3 and x4 will be equal. This then means that the direct swimming route is the faster option.
4. Python Code, 90% GPT 4 Generated
The following python code implements all scenario’s and also produces the figures 1 and 2 as output pdfs and pngs.
5. The Making of this Article
As mentioned, I used the OpenAI’s chatGPT website, configured with the GPT 4 model. I used this chat session where you can follow all my prompts and each chat GPT4's answer.
Note that at least today on August 22th 2023, you need to pay 20 USD a month for a chatGPT Plus account. This account type allows you to also use chatGPT with the GPT 4 i.o. only the free GPT 3.5 model.
I could not do the mathematical derivation with GPT 3.5. For example, it made this derivation error:
and I could not resolve it prompting with GPT 3.5. GPT 4 also made a sign error once, but I could resolve it with some more prompts. See these more general benchmarks comparison between GTP 3.5 and GPT4.
Almost all LaTeX maths snippets were generated by chat GPT 4. I then pasted them in https://quicklatex.com/ , pressed the render button there, and copy-pasted the generated image in medium article here.
The files: BeachBiz26.py, BeachBiz26.tex and compile_latex.sh can all be found in the git repo at: https://github.com/PeterSels/OnComputation/tree/master/BeachBiz
Figures 1 and 2 are generated by running: python BeachBiz26.py
A file BeachBiz.pdf, containing more or less the same as this article but in pdf form is generated by running: ./compile_latex.sh
Python code was pasted in this article via first making a gist from BeachBiz26.py on GitHub.
As for the producing the code and maths derivations, given that I am more productive (I estimate by a factor 2 to 3 in time) using GPT4 than not using it, this way of producing code and maths is simply unstoppable. Forbidding it would be equivalent to protesting against gravity.
For generating the natural language in the article itself, GPT4 is helpful and did produce a story that is syntactically, semantically and grammar-wise correct, written in a coherent, balanced and consistent but neutral writing style. (I did not ask it for a particular style.) However, I still rewrote most of it to make it more attractive to human readers. That includes the structuring into sections and paragraphs.
Peter Sels, August 22nd, 2023.
Copyright © 2023 Logically Yours BV.