Saturday, June 23, 2012

Warehouse simulation


Today I'll share with you a warehouse simulation I had to do a month ago.

Let's consider a warehouse of a specific product whose sale price is 2€ per unit. Customer visits are distributed like a Poisson process (0.5 customers per hour) and the amount of products that each customer wants follows this distribution:

Request 1 unit 2 units 3 units 4 units
Probability 0.3 0.4 0.2 0.1


In order to satisfy the demand, the owner of the warehouse keeps a stock of products. When the inventory is low, he asks the distributor for more units. The orders policy is periodic. When the inventory is fewer than 30 and there aren't any pending orders, the owner asks for more units in a way that the inventory would end up having 100 units.

In each order there's a base price of 10€ (it doesn't matter the amount of units the owner asked). Additionally the cost per unit included in the order will depend on the amount of units asked. If the amount of units requested is less than 50, the price will be 1€/unit. However if the owner asks 50 or more units, the price would be 75 cents per unit.

The delivery time follows a normal distribution (with a mean of 48 hours and a standard deviation of 0.8 hours). The delivery will be paid as soon as it arrives. An agreement with the provider entitles us with a discount of 0.01% for every three hours of delay in the delivery. On the other hand, it will be 0.01% more expensive for every three hours it arrives sooner.

The owner of the warehouse spends 0.1€ per product each hour (due to physical storage, refrigeration, …).

If the customer asks for a greater amount than what is available, the warehouse sells all it has.

a) Simulate the behaviour of the warehouse during 5 months. Estimate the expected profits, the proportion of completely satisfied customers and the percentage of time the inventory remains zero. Let's assume the initial inventory is 70 units of product.

b) Represent graphically the evolution of the inventory level during the mentioned 5 months.

c) Try to identify what changes you could implement to improve the behaviour of the inventory model (get more profits and more satisfied customers).

For the first question, I've programmed a Python script. In order to run it, you need to have installed the NumPy library. I'll show you the output of a few simulations:


This is the graph that represents the evolution of the inventory. For this question, I commented the final part of the code (when the results of the simulation are printed) and put at the end of each iteration a line of code that printed in one line the stock available. Then, I used LibreOffice to plot the graphic. Maybe I should use better tools and techniques the next time.

06-23-2012_2


For the last question. There's an obvious way to reduce the losses: sell all the stock and forget about new orders. That way, we don't have order expenses and we keep maintenance to a minimum. Of course, we'll have a lot of potential losses (a lot of unsatisfied clients), but it's the best way to solve this problem that I've found so far (maintenance is too expensive!). Nevertheless, I challenge the reader to get better results. Mine are these:

06-23-2012_3

Don't change the product sale price, that's cheating.

Sunday, June 17, 2012

Organization and Time Management

This post is a little bit unusual. I won't speak about anything technical today. I've just finished reading "Getting Things Done" by David Allen. I've adopted and modified his system and I want to share my thoughts about it.

Why did I decide to read this book?

I had lots of things to do at the moment and I thought that knowing something about time management could help me. I googled "time management" and found this Wikipedia entry. The book was referenced there in the "Further reading" section.

I kept browsing and I realised that this man had created an organizational system. I thought it was interesting, so I decided to read his book.

First experiences

Once I had the book in my hands I realised I had to make 4 command line programs in C in only 10 days! It was a stressful time because those programs were mandatory if I wanted to pass the exam of that subject.

In those stressful moments, I read the first two chapters and applied their concepts. I used an empty notebook I had. It's not magic, but the system works. I felt less stress and I was starting to program. I didn't know at the time whether or not I was going to finish on time, but I was occupied with the task instead of complaining and feeling like a victim. By the way, I succeeded and believe me, it feels great.

What is the GTD method?

I suggest you read this Wikipedia entry first. The core principle of GTD is that if you put all the "stuff" (as the author writes) in a system out of your mind, your mind will be freer and then you feel less stress and work in a more efficient way.

In order to accomplish that, GTD divides workflow into several stages. The first stage, "collection", deals with this principle. The following stages will tell you how to manage every piece of information that you've introduced into your system.

My implementation

I'm not an executive, I'm just a student. The book is targeted towards executives. The book assumes that the reader receives hundreds of emails a day. I usually receive up to 12 or so. So all the email management thing is useless for me (at least for the time being).

It also assumes everyone has an incredible amount of things that require their attention. It talks about making an "in" tray in which you stack all the stuff for later processing. Until now, I have never needed such an amount of space, and I put everything down in my notebook.

I've really enjoyed applying the "next action" principle. For example, I want to buy a present for someone. In order to accomplish this, the book suggests you have to question yourself "what's the next action?" If X is a girl, maybe I should ask her sister for advice... That's the idea. That way, things that seem too big to be executed in a particular moment are split into several smaller actions and that way the task is more manageable.

I joined two stages of the workflow: "process" and "organize" because my mind thinks that way.

I also have noticed that I have activities that I do everyday, those are out of the system. If you pray, brush your teeth, have a shower and sleep everyday I don't see the point in putting those activities into the organizational system. They're already automatic and thus handled.

I have also applied another principle that I read about in another place. Start every activity you can. It's the best way to avoid procrastination. If you have an activity that lasts 3 hours but you only have 1, it is better to start as soon as you can. That way, the following day when you're going to tackle the activity, there will be only 2 hours left.

I like the approach that the book gives to brainstorming. It says something like "first capture all the ideas without judging them, even the silliest one. Once there aren't more ideas, start judging". Spanish people (me included) definitely try to accomplish every step at once. I hope I'll be able to overcome this bad habit.

Finally, before I read this book, when I was going though a crisis, I started to write all that occurred to me in my whiteboard. I keep doing this even now and I also use it as a "collection" tool. That way, I have the ideas in front of me without forgetting anything.

Saturday, June 9, 2012

Risk: attacker VS defender. Real probabilities.

Quick post.

A couple of days ago I posted a simulation program about Risk. I've made today a script that returns the true probabilities of the simulated events. That way we'll be able to see how close to reality the obtained results are.

The script is based on the classical definition of probability (count favourable cases and divide them by the total possible cases).

Screen capture:

06-09-2012_1


Download the script here.

Thursday, June 7, 2012

Risk simulation: attacker vs defender

Risk is a very famous strategy board game. In the game, you conquer your opponent's territories by attacking them with armies. However, you can lose them in the attempt. In order to represent this uncertainty, the game is played with dice.

Quoting Wikipedia, these are the rules for attacking a territory:

When it's a player's turn to attack, he or she can only attack territories that are adjacent or connected by a sea-lane to his or her own territory. A battle's outcome is decided by rolling die. The attacking player attacks with one, two, or three armies, rolling a corresponding one, two or three dice. At least one army must remain behind in the attacking territory not involved in the attack, as a territory may never be left unoccupied. Before the attacker rolls, the defender must choose to resist the attack with either one or two armies (using at most the number of armies currently occupying the defended territory) by rolling one or two dice. Each player's highest die is compared, as is their second-highest die (if both players roll more than one). In each comparison, the highest number wins. The defender wins in the event of a tie. With each die comparison, the loser removes one army from his territory from the game board. Any extra dice are disregarded and do not affect the results.

I've made a Python script that simulates a single attack. Download it here.

Run the script like this:
python risk.py -a number1 -d number2 [-n number3]

Examples:
python risk.py -a 2 -d 1
Simulate a situation in which the attacker will use two armies and the defender only one.

python risk.py -a 2 -d 1 -n 1000
The same as previous, but this time the experiment will be repeated 1000 times and the program will report to the user the number of wins as well as it's percentage. Remember, the more you repeat the experiment, the more accurate the results will be.

Since I want solid information, I will simulate each one of the 6 cases 1000000 times. I takes a few seconds in my computer (Intel® Core™2 Duo CPU T5750 @ 2.00GHz × 2 and 3.0 GiB of memory).

Cases in which the defender uses a single die:

06-07-2012_1


Cases in which the defender uses two dice:

06-07-2012_2



NOTE 1: In the output of the program, a "tie" is when both players lose one army. Remember that in each die comparison, the defender will win the ties. So rolling 5 for the attacker and 5 for the defender counts as a win for the defender. What I call "ties" in the simulation would be something like this:
Attacker roll 6 and 1.
Defender roll 5 and 2.
Defender loses 1 army (6 beats 5) and attacker loses 1 army (2 beats 1).

NOTE 2: The simulation doesn't tell you if you've conquered the territory. For example, if you have 4 armies and you lose one trying to conquer the defender, you still will be able to use two armies to strike again. This situation is not contemplated. It only simulates the first attack.

In conclusion:

- When the defender uses 2 dice, there's a great chance for both opponents to lose one army (at least more than I was expecting, for me this is a surprise). Never defend with one army if you have the chance. In the past, I used two dice to make the game more dynamic, although I had doubts about if it was always the right decision. Now science tells me I have to use two armies to defend my territory. The probability of a tie is so high that it significantly whips away the security of the enemy.

- Attack the weak with all the might of your empire! If you can attack with three armies and the enemy territory is protected only by one, smash it!

- I've lived the situation in which both opponents start putting armies to threaten an enemy territory and the other one puts the same amount only to return the threat, but no one attacks (possibly in the belief that defending is more profitable than attacking). Wrong. If the situation arises, start attacking with three armies. Obviously, stop when you lose the possibility of attacking with three armies if the defender can still cast two dice.

- Don't attack with only one army.

- Always use as many dice as you can (unless you're splitting your army for some reason, but keep in mind chances are you're going to be less efficient).

These information is very poor and it won't increase significantly the chances of wining the entire game. However, it's a start. I might post more about this in the future.

Saturday, June 2, 2012

New Program: Filmometer

Filmometer captures data from a file in the specified format (read below) and tells you from the captured data how many films you have seen, how much time you have spent watching films and what film you have watched the most.

I had the idea of programming this when I started to take English seriously. Then I heard from the teacher of the method I was using that you need at least 3000 hours to master a new language. So, I thought that I could watch films in English until I overcome that number. In order to keep track of this information, I started making a file that contained all the data (I didn't know anything about databases). When I learnt how to program in Python, I made a first script that could capture the data I already had collected and process it. Sadly, a couple of weeks ago I lost all the information stored in my hard drive, so I'm afraid that Python script is lost.

Since I've being programming in C lately, I've decided to recode the same program, but this time it will be written in C.

So, here are some screen captures of the program running on Linux:

Help option:
06-02-2012_1

Summary option:
06-02-2012_2

Most watched film option:
06-02-2012_3

There are a few errors that the program can handle without crashing, but if you don't put the file in the correct format, the behaviour will be unpredictable and the error won't be handled.

About the file format:
Empty lines are not allowed. This is the line format:

title_(year) times_viewed durationmin
or if it's the extended version of the film:
title_(year)_(Extended_Version) times_viewed durationmin
or:
title_(Extended_Version)_(year) times_viewed durationmin

- "title" is a word or a few words, if the real title of the film is composed by two words or more, the spaces must be represented with an underscore character in the file format. The "(" symbol is not allowed.
- "year" is an integer number. It's the year in which the film was released on cinemas for the first time. Only possitive integer number are allowed here.
- "Extended_Version". If you want to register an extended version of a film you must put this. It must be written like that; although, it will work if you just write "E" without quotes.
- "times_viewed". An integer number. The number of times you've watched the film.
- "duration". An integer number. The number of minutes the film lasts. It must be right before the word "min", without any spaces between each other.

This is an example of line in the correct format:
A_Night_at_the_Opera_(1935) 1 96min

Download links:
- Source code
- Windows executable (tested on Windows 7)
- Linux executable (tested on Ubuntu 12.04)
- Sample file (with the Windows new line character)
- Sample file (with the Linux new line character)