Linear Programming for an Imaginary Strategy Game

In this article I will try to explain how to make a linear model, giving different examples.

In my previous article, i tried to explain you what a linear model is and told you that while it is almost impossible to find a true linear model in real life, an online strategy game (or any strategy game in that manner) is all about linear models. And now, I will disregard my lame simple example of that previous post and give you a better and hopefully more intriguing one to teach you about a mathematical method called linear programming to make the best use of your resources in an online strategy game.

The Factors at Hand

Assume that you are playing an online strategy game with a medieval concept and you have the option to train a swordsman or a knight. Now, the attack power of the swordsman is 8 points, and the attack power of the knight is 24, exactly three times more than the swordsman.

Keep in mind that this is a game, not real life, so each of your swordsmen are identical, and three swordsmen equals to one knight no matter what. There is a direct relation with the numbers and the actual outcome so this is a perfect linear model with no room for the surprise element. So any mathematical tool we are going to use will work as long as we understand the main idea here. And that’s what I am going to try to tell you.

These soldiers don’t come for free, and the cost of training a swordsman is 10 coins, while training a knight costs 33 coins. You see, this fact changes all the equation. Because if you look at the attack power, you see that training one knight is three times better than training one swordsman. But when it comes to the cost, it clearly turns out that training three swordsmen is cheaper than training a knight, and you get the same attack power.  

But now lets make things more intriguing and put the “defense power” intıo the equation. Now, the defense power of a swordsman is 9, and the defense power of a knight is 35. With these new numbers given, we now know that 3 swordsmen are not enough to defeat a knight, so maybe there is no point in making this kind of simple math. Our source of coins are limited, and if we want to make the best use of our valuable coins, we have to consider all the elements in the game.

Now consider these: additionally, you need 5 units of iron to train a swordsman and 12 units of iron for the knight. And what’s more, each knight consumes 2 units of grain per hour, and each swordsman consumes only 1 unit of grain. So you see, things are rather complicated even if they are perfectly linear.

So can we exactly know what to produce to make best of the resources which we spend on our soldiers? A regular player would choose to spend all his money on a knight because they offer more attack power and defense power. So there is nothing much to think about it. But is it really? Perhaps, making greater numbers of swordsman will gain you more hit power? How can we know it for sure?

Enter Linear Programming

Don’t worry, I won’t bother you with complex formulas. You really don’t need to know how the calculations are done. There are free tools both online and offline which will do the hard work for you. But you need to understand the logic behind it so your calculations make any sense.  

 So what do we have?

A swordsman: 8 attack power, 9 defense power, 10 coins, 5 iron, 1 grain per hour.

A knight: 24 attack power, 35 defense power, 33 coins, 12 iron, 2 grains per hour.

I will tell you this, the odds seems severely against the knight, and it really seems like a knight is more costly, but we will see about it.

Building Your Model

Notations

Before getting to the calculations, we have to build our model. I will not use any of the mathematical notations. It doesn’t matter as long as you know what you are doing. After all, you won’t present your model to the world, are you? So you don’t need to bother with the universal notations and stuff. But nevertheless, we have to agree on some basic notations so that we can understand each other.

And it is simple. Each of the units take place in our equations are represented by their first letter. So swordsman is  S, and knight is K.

Define your aim

First, you need an aim, a purpose. The logic here is, you first define your aim and then you try to find out the optimum amount of sources you will spend to reach your aim. This aim pretty much depends on your command on the particular game you are playing. If you feel like this is a defensive game, then you should build your model to maximize your defense. Or alternatively, you may choose to maximize your offensive power.

For this particular example, I will choose to maximize my offensive power. In fact, deciding on the right strategy is crucial but I can’t teach you how to choose the best strategy. This is something you should determine depending on your gaming experience. This method I’m trying to explain can only guide you after you make your mind about the path you will follow. So, on this imaginary game I’m playing, I prefer an offensive strategy and I want to maximize the attack power I will get.

No notations here, just write it down: AIM = MAXIMIZING ATTACK POWER

Your Linear Model

Now it is time to build your model. Just keep in mind that we are not calculating anything for now, and only trying to build a logical model which fits to the game. And it is unbelievably easy. Trust me.

All we got to do is to define the restrictions we have without any errors, and we are all done. So lets assume that we have the resources in the amounts of:

COINS: 450

IRON = 215

GRAIN (hourly production)=70

So, if K and S represent the number of knights and swordsmen we are going to build respectively, then:

10S + 33K <= 450; which identifies the amount of maximum coins we can spend for our soldiers. Alternatively, if you think you should spare some coins for some reason and want to spend only 350 coins for example, then our equation would be like:

10S + 33K <= 350

Same wise, our equation for the iron would be:

5S + 12K <= 215; which means we can’t spend more then 215 iron while training our soldiers.

And for the grain, we should consider the hourly consumption and the production, and it would be:

S + 2K <= 70.

So as you, the cost of each unit is the coefficient of that unit, and the maximum amount we will spend on our soldiers cannot be more than we already have.

And done, we have our model.

AIM = MAXIMIZING ATTACK POWER = 8S + 24K

10S + 33K <= 450

5S + 12K <= 215

S + 2K <= 70.

If you don’t think this is clear enough, just leave a comment and I will try to explain it further. If you think you are OK with it, then it means you are ready for the next step where I will explain you how you can do the actual calculating to get some numeric solution other than just formulas.

Leave Your Response