Introduction to Python Mathematical Optimization Solving junior high school math problems with pulp

I tried to solve a junior high school math problem with Python.

problem 4X+3Y=9 2X-3Y=9

from pulp import *
#Mathematical model creation
prob = LpProblem(sense=LpMinimize) 

#variable
x = LpVariable('x', cat='Continious') 
y = LpVariable('y', cat='Continious')

#Definition of constraint expression(Problem part)
prob += 4* x + 3*y ==9
prob += 2* x -  3*y ==9

#Solution
status = prob.solve()
print(LpStatus[status])
print('x:%.if y:%if' % (value(x), value(y)))

スクリーンショット 2019-12-18 16.36.44.png

Yes. I got the answer. If you use programming for simultaneous equations in junior high school mathematics, some people may not understand it at a cram school.

Next is the word problem.

If you buy 22 apples for 120 yen each and mandarin oranges for 150 yen each. The total was 2711 yen. How many apples and oranges did you buy?

prob = LpProblem(sense=LpMinimize)


#variable
x = LpVariable('x', cat='Continious') 
y = LpVariable('y', cat='Continious')

#Definition of constraint expression
prob += 120* x + 150*y ==2711
prob += x + y ==22

#Solution
status = prob.solve()
print(LpStatus[status])
print('x:%.if y:%if' % (value(x), value(y)))

スクリーンショット 2019-12-18 16.37.12.png

Yes I got the answer. There are 19 apples and 2 oranges. It is convenient to be able to solve such annoying problems in an instant.

Recommended Posts

Introduction to Python Mathematical Optimization Solving junior high school math problems with pulp
Try to solve a set problem of high school math with Python
Solving school district organization problems with combinatorial optimization
Solving Mathematical Optimization Model Exercises with Google's OR-Tools (3) Production Optimization Problems
How to write offline real-time Solving E05 problems with Python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Precautions when solving DP problems with Python
[Introduction to Python] Let's use foreach with Python
Mathematical optimization that can be used for free work with Python + PuLP
[Python] Introduction to CNN with Pytorch MNIST
Solving nurse scheduling problems with combinatorial optimization
Mathematical Optimization Modeler (PuLP) Cheat Sheet (Python)
[Python] A junior high school student implemented Perceptron and tried to classify irises.
I tried to refactor the code of Python beginner (junior high school student)
Markov Chain Chatbot with Python + Janome (1) Introduction to Janome
Markov Chain Chatbot with Python + Janome (2) Introduction to Markov Chain
Introduction to Tornado (1): Python web framework started with Tornado
Introduction to formation flight with Tello edu (Python)
Introduction to Python with Atom (on the way)
Introduction to Generalized Linear Models (GLM) with Python
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Python] How to iterate with the range function?
I want to handle optimization with python and cplex
[Chapter 5] Introduction to Python with 100 knocks of language processing
An introduction to Python distributed parallel processing with Ray
Introduction to Mathematics Starting with Python Study Memo Vol.1
Reading Note: An Introduction to Data Analysis with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
From zero knowledge of Python to making AI in the first grade of junior high school
Introduction to Python language
Introduction to OpenCV (python)-(2)
Junior high school committee
Python optimization library Pulp
Introduction to Machine Learning with scikit-learn-From data acquisition to parameter optimization
Introduction to her made with Python ~ Tinder automation project ~ Episode 6
20200329_Introduction to Data Analysis with Python Second Edition Personal Summary
Introduction to her made with Python ~ Tinder automation project ~ Episode 5
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
[Introduction to Python] How to get data with the listdir function
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
I tried to remodel the code of Python beginner (junior high school student) into object-oriented crunchy
I tried to make my own high school girl BOT with Rinna style with LINE BOT (Python & Heroku)