0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PuLPで連立一次方程式を解く

Last updated at Posted at 2023-08-19

PuLPで連立一次方程式を解く

前提

あらかじめpulpをインストールしておく。

問題1

2x + 3y = 8

4x + 5y = 14

pulpで解く

import pulp

# 問題の宣言
problem = pulp.LpProblem('test', pulp.LpMinimize)

# 変数の宣言
x = pulp.LpVariable('x', lowBound=0)
y = pulp.LpVariable('y', lowBound=0)

# 制約条件の宣言
problem += 2 * x + 3 * y == 8
problem += 4 * x + 5 * y == 14

# 解く
status = problem.solve()

# 結果の表示
print(pulp.LpStatus[status])
print(x.value(), y.value())

結果1

Optimal
1.0 2.0

確かに解けました。

問題2

2x + 3y = 8

2x + 3y = 14

pulpで解く

import pulp

# 問題の宣言
problem = pulp.LpProblem('test', pulp.LpMinimize)

# 変数の宣言
x = pulp.LpVariable('x', lowBound=0)
y = pulp.LpVariable('y', lowBound=0)

# 制約条件の宣言
problem += 2 * x + 3 * y == 8
problem += 2 * x + 3 * y == 14

# 解く
status = problem.solve()

# 結果の表示
print(pulp.LpStatus[status])
print(x.value(), y.value())

結果2

Infeasible
7.0 0.0

答えがあるように見えますが、statusがInfeasibleになっています。これは、制約条件が矛盾していることを示しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?