#5 Buying a motorocycle – how to create rationalization for irrational decisions? Use genetic algorithms!

I am a guy who tries to make rational decisions in life, or at least that’s what I like to think. But in last days I made a few irrational decisions and buying motorocycle is one of them. Here is my first motorocycle:


I was thinking about buying bike many times in my life. I have had motorocycle license since 2013, but my rational brain always had an excuse for purchasing one. Finally my brother convinced me that it is a rational, good, giving a lot of fun decision.

Why you reading about motorocycles on a software blog?

I had 2 discussions with other DSP17 participants about genetic algorithms. I wrote a few simple ones, when I was studying and I was charmed with the ‘magic’ of this stochastic methods of optimizations. I don’t want to give any lecture about GA, because I am not an expert and internet is full of useful information. You can also visit guys from DSP17:

My intention is to show you example of primitive GA working on a simple problem. There are probably better methods to decide which motorocycle is the best choice, but creating a software is not always about making things easier.


Problem definition:

If you are interested how this code has been created start with the source code – it is really simple right now: moto-genetic-algs. I will explain all the technicalities of the code in next post.

Here is Moto class:

public class Moto implements MapableToGene{
  int power;
  int ccm;
  int price;
  int weight;
  int gasUsage;
  int equipmentQuality;
  boolean canMyWifeUseIt;
  boolean doesMyWifeLikeItsDesign;

These fields were important for me in this decision process, especially last 2 – canMyWifeUseIt and doesMyWifeLikeItsDesign.

In GA you need to define fitness function. I created a simple, linear one, which shows whats important for me. To be honest I didn’t spent a lot of time on it, but I will improve this before next post:

public class Moto implements MapableToGene{
  public int value() {
    return power/4 + ccm/10 - price/1000 - weight/10 - gasUsage
    + equipmentQuality/2 + (canMyWifeUseIt?5:0) + (canMyWifeUseIt?3:0);

You can be much more sophisticated in creating fitness function. Correct definition of it is a key thing in every optimization problem and in GA.

How it works?

I will explain how the process goes:

  1. Population has few Phenotypes/Individuals randomly created.
  2. All the phenotypes are mapped to motorocycles and then valued based on fitness function.
  3. New population is created by crossing over the best phenotypes.
  4. Go back to 2 and repeat few times.

It is really simple if you know some basics of GA.


Here is simplified result of execution of the current code:

0: [101, 80, 73, 72, 60, 49, 49, 46, 40, 29, 27, 18, 14, 12, 9, 7, -2, -5, -18, -18]
1: [111, 111, 111, 111, 100, 100, 100, 100, 77, 77, 77, 77, 61, 61, 61, 61, 36, 36, 36, 36]
2: [106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 105, 105, 105, 105]
3: [106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106]
Best moto:Moto [power=73, ccm=1000, price=2666, weight=132, gasUsage=5, equipmentQuality=1, canMyWifeUseIt=true, doesMyWifeLikeItsDesign=true]

Results containing more information are available here. Each row shows the values of every phenotype in the current population. In the generation 0 we have a lot of different phenotypes, because they were randomly created. In the generation 1 you can see that “stronger/better” individuals survived. After 4 iterations the results are stable. Not too good, because my implementation needs to be improved, but it shows at least the basic behaviour of the algorithm.


As you see, the best motorocycle is the one with high power and engine displacement, low price but what is really important – my wife should be able to use it, and she needs to like the look of the motorocycle;-)

I hope that you liked this example. I will improve it definitely and post a little bit about GA in between main project posts.


5 thoughts on “#5 Buying a motorocycle – how to create rationalization for irrational decisions? Use genetic algorithms!

  1. Interesting way to apply “scientific” multi-criteria optimization approach to real life. Actually this could be interesting way to rationalize many other choices, if only we could easily estimate weights of criteria that match our real preferences.

    I’m not sure if dynamic nature of ‘doesMyWifeLikeItsDesign’ wouldn’t be a difficulty here, however 😉

    Nice bike by the way. I can’t wait for the start of the season, too.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s