Simulate a good Christmas date with a Python optimized model

Purpose

I want to have a fun date without spending too much money and time.

conditions

Budget: Up to 5,000 yen because it is before salary at the end of the month. Time required: Weekdays and early morning the next day, so if you meet at 7:00, it will be until 12:00. Course: If possible, a cooking date at your home condominium is short and easy.

reference data
Government Statistics e-Stat Day of the week, men and women, life stage, people who were together, time division Different actor rate

I summarized the popular courses from this statistical data, and added the cost per hour and the required time from the rule of thumb, and made the following table in CSV format. ninki Number of women who do 2-3 times a month (thousands) jikan, cost Add the normally required time and budget (¥ / h) as a rule of thumb キャプチャ2.PNG Get this as a DataFrame with pandas and incorporate it into the optimization model. In addition, ninki was treated as a categorical variable by binning as a feature rather than measuring a little as a numerical value.

import pandas as pd
from pulp import LpVariable,LpStatus,value
from ortoolpy import model_min

syuko = pd.read_csv('C:\StaticData\syuko.csv',index_col='estat')
ninki = []
syumi = []
for i in syuko.columns:
    ninki.append(syuko.iloc[0][i])
# binning
bin=pd.cut(ninki,3,labels=False,precision=0,duplicates='drop')+1
syumi=list(syuko.columns)
dic = dict(zip(syumi,bin))

Now let's start creating an optimization model. Let the variables x, y, and z be the scheduled times for movies, karaoke, and cooking, respectively. I want to focus on cooking, so let's increase the variable Z.

#Minimization model definition
mod = model_min()

#Variable setting
x=LpVariable('x',lowBound=0)
y=LpVariable('y',lowBound=0)
z=LpVariable('z',lowBound=0)

#Objective function setting
mod += syuko.iloc[2][0]*x + syuko.iloc[2][1]*y + syuko.iloc[2][2]*z

#Constraints
mod += x + y <= 3
mod += y + z >= 3.5
mod += z + x >= 4
mod += x + y + z <= 5

#Solver execution
mod.solve()
mst = mod.status
st = LpStatus[mst]

After changing the constraints, if Optimal is returned from LpStatus, the optimum solution has been obtained. So it's interesting to use it as a simulator. By the way Impossible if Infensible is returned Unbounded is unbounded (unlimited and optimal solution can be improved) Optimal outputs the combination of the cost and required time of the optimal solution.

#Optimal solution output
if st=='Optimal' and value(mod.objective) < 5000:
    print("Optimal budget" + str(value(mod.objective))+"Circle")
    plist=[]
    con=0
    for k,v in dic.items():
        print(k,"Recommendation level",v,"Efficient travel time",value(mod.variables()[con]),"h")
        con += 1

Output result キャプチャ.PNG It seems that cooking will cost 3,320 yen for 4 hours at home apartment! (Does that work?)

Recommended Posts

Simulate a good Christmas date with a Python optimized model
Get date with python
I made a Christmas tree lighting game with Python
[python] Create a date array with arbitrary increments with np.arange
Make a fortune with Python
Create a directory with python
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Create a python machine learning model relearning mechanism with mlflow
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
[Python] Mixed Gauss model with Pyro
Solve ABC167 A ~ C with Python
How to calculate date with python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
Make a model iterator with PySide
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
[Python] A program that creates stairs with #
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
Add a Python data source with Redash
Create a dummy image with Python + PIL.
I made a character counter with Python
[Python] Drawing a swirl pattern with turtle
I drew a heatmap with seaborn [Python]
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
Map rent information on a map with python
Search the maze with the python A * algorithm
Daemonize a Python web app with Supervisor
Let's make a voice slowly with Python
Created a darts trip with python (news)
I tried a functional language with Python
Receive date type (datetime) with ArgumentParser [python]
[Python] A quick web application with Bottle!
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Write a TCP client with Python Twisted
What I did with a Python array
Solve AtCoder ABC168 with python (A ~ D)
Create a word frequency counter with Python 3.4
Try HTML scraping with a Python library