# Let’s Program A Swarm Intelligence 3: All Your Max Are Min

A Closer Look At Optimization

We’ve got a problem and we’ve got a programming language. Anything else we need to get this Let’s Program going?

Well, we really probably should spend a little more time talking about exactly what it means to optimize an equation. Just saying that we want our swarm to find “better” answers, hopefully the “best” answer, doesn’t really tell us all that much. What does a better answer look like?

The definition of good and better answers actually depends a lot on the problem being solved. If you’re trying to optimize your company’s profits you probably want the biggest value you can find. A two million dollar profit is more optimal than a one million dollar profit.

On the other hand imagine you are trying to optimize a chemical factory that produces toxic waste. Toxic waste is expensive to dispose of so in this case the optimal answer is the smallest answer you can come up with. Producing one ton of waste per month is more optimal than producing ten tons.

Now let’s imagine that in order to minimize your toxic waste you figure out that you’re going to need exactly one thousand gallons of fresh Chemical X every hour. So you start developing a Chemical X production system. Optimizing this system means producing as close to one thousand gallons per hour as possible. Making too little Chemical X slows down your system. Making too much Chemical X wastes money. You have an exact target to hit.

Summary: The Three Types Of Optimization

All numeric optimization problems can be classified as one of the following three types:

Minimum: Optimizing towards the smallest result possible.

Maximum: Optimizing towards the biggest result possible.

Target: You already have a specific, ideal result in mind. Optimization is the process of finding out how close you can get to that desired result.

Quick example. After collecting some data you find five possible solutions with the following answers: 50, 75, 100, 120 and 160.

If you are optimizing towards a minimum then 50 is the most optimal answer of the five.

If you are optimizing towards a maximum then 160 is the most optimal answer of the five.

If you are optimizing towards a target of 90 then 100 is the most optimal answer. If your target was 130 instead then 120 would be the most optimal answer.

Three In One Optimization: Everything Is A Minimum

If there are three different kinds of optimization then you might think that means we need to write three different optimization algorithms. But with just a little cleverness you can get away with only one optimization function. This is because all three types of optimization can be reduced to different kinds of minimums.

Minimum’s can be treated as minimums because that’s what they actually are. That was easy.

A maximum can be turned into a minimum by multiplying all values by -1. So instead of looking for the max between 1, 2 and 3 (which would be 3) we look for the minimum between -1, -2 and -3 (answer is still the 3).

A target goal can be turned into a minimum by focusing not on the answers themselves but on the absolute difference between the answers you find and the result you want. If your target is 100 and your answers are 90, 105 and 115 then you would reduce them to differences of 10, 5 and 15. We minimize to find the smallest difference (5) which lets us know which value was closest to our target (105).

Conclusion: We Need To Focus On Minimums

Now that we know that every optimization we want to do can be modeled as a minimization problem we’re ready to jump into actual problem solving. Check back in a few days and I’ll start outlining my design approach and share my fist bits of skeleton code.

Note to readers from the future: You don’t have to check back in a few days. You can just click on the next post whenever you want. Aren’t you lucky?