# Modeling the World in a Deterministic Way with GEKKO | by Daniel Kulik | Jun, 2023

## A soft guide to solving differential algebraic equations and applying dynamic optimization

With our world becoming more and more digitized, data collection is expanding rapidly. This data has allowed us to create more accurate models that have helped us to solve problems and find optimized solutions in many fields of business and technology. Since these models are built on real world data, which is stochastic by nature, things such as randomness, noise, and anomalies within the datasets are often inevitable. Much time is often spent doing EDA, data preprocessing, and outlier detection. These methods have become commonplace to understand and refine the data prior to fitting a model. However, any model built using such data is always an approximate to the real solution. This is due to data again being stochastic (Aleatoric uncertainty) and subject to many unknown influences (Knightian uncertainty). Along with these, a lack of sufficient data or having biased imbalanced datasets can further degrade a model’s accuracy (Epistemic uncertainty). Even against these odds, models built in this manner are invaluable and the inherent stochasticity can sometimes be a tool to help better understand the intricacies of the real world. However… an alternative approach exists: enter deterministic modeling.

Deterministic modeling is the antithesis to modeling with randomness. It is a method to simplify certain aspects of the modeling process by removing the messiness introduced by the real world. These models are idealistic in nature, meaning that they represent an ideal solution based on specific assumptions. These assumptions can stem from a set of opinionated, probabilistic, or generalized rules, and assumes that a well-defined pattern exists between the inputs and outputs. While this may sound similar to modeling with stochastic data, the main difference is that the model now is determined by the these predefined assumptions rather than training data. A question might now be popping into your head asking, “well if I’m not training the model on the data, where do I get these model assumptions from?”. The answer to this is quite simple yet sometimes overlooked, domain knowledge. Domain knowledge is a set of rules and concepts known on a specific topic. It is through domain knowledge that the assumptions are derived, serving as the foundation for deterministic modeling. In any programming language these deterministic models can be written using custom code, but as models become more complex, libraries have been written to specifically tackle this task. In python one such library called GEKKO will be explored.

GEKKO is a Python library to facilitate the execution of the modeling language Advanced process monitor (APMonitor). It offers a range of features and functions tailored to the needs of deterministic modeling, facilitating the creation and analysis of complex models. It can solve both mixed-integer and differential algebraic equations, and is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming. GEKKO has 9 problem types with which to define a deterministic model setup. However, for this article, only a Moving Horizon Estimation (MHE) that solves all equations simultaneously will be considered. The MHE mode is used to estimate the states of a dynamic system by minimizing the discrepancy between the measured outputs and the model predictions formulated by an optimization problem with an objective. This is done by applying a recursive estimation or a moving window estimation.

Whew! That was quite a lot a jargon, but it should all make sense soon with the following practical example.

Space flight is an incredible human achievement. The feat of sending something man-made into space is a marvel of both engineering and ingenuity. But to send something into space is pretty expensive. So, building rockets with confidence of their ability to perform as expected is crucial. Collecting data of a real live launch is great to help improve a future rocket’s performance. But a rocket had to be launched before that to get the data, and therein lies the problem. No stochastic data no stochastic based model. Enter the deterministic model and GEKKO. The assumptions for a deterministic model of a rocket are quite simply, you guessed it, rocket science! That is to say, that without even having to launch a rocket one inch off the ground, we can instead use the laws of physics to determine how a specific rocket setup will perform in an idealistic scenario. For example, let’s replicate SpaceX’s Falcon 1 Stage 1 rocket setup using GEKKO.

To start, let’s use publicly known information [1][2] for the Falcon 1 Stage 1 rocket setup and begin by defining some simulation constants and known values.