Techno Blender
Digitally Yours.

Using Python and Operational Research to Optimize your Happiness | by François St-Amant | Sep, 2022

0 56


Using Gurobi and the knapsack problem to find an optimal solution

Source: https://unsplash.com/photos/u0vgcIOQG08

Without knowing, people are constantly trying to optimize their lives. For instance, by going on vacation, you decide that the trip is worth it, even if it comes at a financial cost.

Sadly, you cannot spend all your time seeking only pleasure. Most people must think about their longterm wellbeing as well.

Indeed, on a given morning, an individual could prefer going skydiving instead of going to work. However, eventually, if this individual never goes to work and goes skydiving instead, he will soon run out of money, lose his job and will not be able to afford to go skydiving anymore.

This balance between work and play can be hard to find. How can I determine which actions to take in order to maximize my happiness?

This all sounds a lot like the knapsack problem, a classic of operational research.

The knapsack problem is a combinatorial optimization problem where you have a set of items that have a certain value and volume that you want to put in a backpack. Considering that the backpack has a limited capacity, you have to decide which items to put in the bag to maximize the value.

Source: https://en.wikipedia.org/wiki/Knapsack_problem

In our case, instead of objects, values and volumes (like in the knapsack problem), we can think in terms of activities, the pleasure they bring and their cost (both in terms of time and money).

In the following example, I will be showing a fictional example of how an individual could decide on his weekly schedule to maximize his wellbeing and pleasure.

The article will also show you how you can solve an Operational Research problem using Python. I will be using Gurobi (and most specifically the Gurobi Optimizer), an amazing product that allows you to easily model and solve all types of Operational Reasearch problems.

1. The variables

The first step in the problem is to determine what activities need to be done. For each activity, you also need to specify:

  1. The cost of doing it (for 1 hour in this example)
  2. The pleasure it brings (grade from 1 to 10)
  3. How beneficial it is to you health wise (financial, physical, mental, etc.). Grade from 1 to 10 again.
  4. The minimal and maximal amount of time you could spend on this specific activity.
  5. Your total budget, i.e. how much you can spend weekly on “activities”.
  6. How much time you have, removing sleep, transportation, etc.

So for instance, the individual here does not enjoy going to the gym much (pleasure = 1). It is however deemed very beneficial for general wellbeing. On the other hand, gaming is pleasurable but does not contribute to wellbeing as much.

Of course, the challenge here is to fill out all of this. How much pleasure does an activity bring? How good is it for you? This is very subjective and hard to quantify. Only the person doing the exercice knows what the right values are! It depends what you want in life, what your goals are, etc.

2. The constraints

Once we have our variables, we can add constraints to the model. This is where we ensure that our final solution will respect the rules specified earlier.

  • Line 4: we specify that our variables are integers, i.e. numbers
  • Line 7: the total amount of money spent must be inferior or equal to our budget
  • Lines 10 and 13: we must respect the minimum and maximum for each variable. In other words, if the person said he must work between 35 and 45 hours, it must be respected
  • Line 16: The sum of time spent on all our different activities must be equal to the time specified

3. The objective function

Now, it is time to optimize our objective. This objective is different for everybody! Some just want to enjoy life as much as possible. Others want to work more to get a promotion. Others want a perfect balance. The function will be different for everyone because everyone has a different definition of happiness.

Here is the objective function used in this example.

As you can see, the goal here is to maximize the sum of pleasure and wellbeing. However, pleasure is given more importance than wellbeing, since the latter is multiplied by 0.75.

4. The solution

We can finally optimize our problem and get our optimal solution!

Here are the results.

So the person below, to optimize his objective function (and so his happiness), must work 45 hours a week, play golf for 2 hours, watch netflix for 15, etc.

Based on all that the person has specified, this is what will optimize his objective function and make him happiest!

First of, this is a very general problem, but you can always add more constraints, variables, etc. to make it more complex and more representative of the real world. I used the example of a week but it could also be a month, a year…whatever you want! Gurobi makes it very easy to do so. Look it up here! https://www.gurobi.com/documentation/9.5/refman/refman.html


Using Gurobi and the knapsack problem to find an optimal solution

Source: https://unsplash.com/photos/u0vgcIOQG08

Without knowing, people are constantly trying to optimize their lives. For instance, by going on vacation, you decide that the trip is worth it, even if it comes at a financial cost.

Sadly, you cannot spend all your time seeking only pleasure. Most people must think about their longterm wellbeing as well.

Indeed, on a given morning, an individual could prefer going skydiving instead of going to work. However, eventually, if this individual never goes to work and goes skydiving instead, he will soon run out of money, lose his job and will not be able to afford to go skydiving anymore.

This balance between work and play can be hard to find. How can I determine which actions to take in order to maximize my happiness?

This all sounds a lot like the knapsack problem, a classic of operational research.

The knapsack problem is a combinatorial optimization problem where you have a set of items that have a certain value and volume that you want to put in a backpack. Considering that the backpack has a limited capacity, you have to decide which items to put in the bag to maximize the value.

Source: https://en.wikipedia.org/wiki/Knapsack_problem

In our case, instead of objects, values and volumes (like in the knapsack problem), we can think in terms of activities, the pleasure they bring and their cost (both in terms of time and money).

In the following example, I will be showing a fictional example of how an individual could decide on his weekly schedule to maximize his wellbeing and pleasure.

The article will also show you how you can solve an Operational Research problem using Python. I will be using Gurobi (and most specifically the Gurobi Optimizer), an amazing product that allows you to easily model and solve all types of Operational Reasearch problems.

1. The variables

The first step in the problem is to determine what activities need to be done. For each activity, you also need to specify:

  1. The cost of doing it (for 1 hour in this example)
  2. The pleasure it brings (grade from 1 to 10)
  3. How beneficial it is to you health wise (financial, physical, mental, etc.). Grade from 1 to 10 again.
  4. The minimal and maximal amount of time you could spend on this specific activity.
  5. Your total budget, i.e. how much you can spend weekly on “activities”.
  6. How much time you have, removing sleep, transportation, etc.

So for instance, the individual here does not enjoy going to the gym much (pleasure = 1). It is however deemed very beneficial for general wellbeing. On the other hand, gaming is pleasurable but does not contribute to wellbeing as much.

Of course, the challenge here is to fill out all of this. How much pleasure does an activity bring? How good is it for you? This is very subjective and hard to quantify. Only the person doing the exercice knows what the right values are! It depends what you want in life, what your goals are, etc.

2. The constraints

Once we have our variables, we can add constraints to the model. This is where we ensure that our final solution will respect the rules specified earlier.

  • Line 4: we specify that our variables are integers, i.e. numbers
  • Line 7: the total amount of money spent must be inferior or equal to our budget
  • Lines 10 and 13: we must respect the minimum and maximum for each variable. In other words, if the person said he must work between 35 and 45 hours, it must be respected
  • Line 16: The sum of time spent on all our different activities must be equal to the time specified

3. The objective function

Now, it is time to optimize our objective. This objective is different for everybody! Some just want to enjoy life as much as possible. Others want to work more to get a promotion. Others want a perfect balance. The function will be different for everyone because everyone has a different definition of happiness.

Here is the objective function used in this example.

As you can see, the goal here is to maximize the sum of pleasure and wellbeing. However, pleasure is given more importance than wellbeing, since the latter is multiplied by 0.75.

4. The solution

We can finally optimize our problem and get our optimal solution!

Here are the results.

So the person below, to optimize his objective function (and so his happiness), must work 45 hours a week, play golf for 2 hours, watch netflix for 15, etc.

Based on all that the person has specified, this is what will optimize his objective function and make him happiest!

First of, this is a very general problem, but you can always add more constraints, variables, etc. to make it more complex and more representative of the real world. I used the example of a week but it could also be a month, a year…whatever you want! Gurobi makes it very easy to do so. Look it up here! https://www.gurobi.com/documentation/9.5/refman/refman.html

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment