You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
from scipy . optimize import linprog
import time
import numpy as np
'''
目标函数:
min z= -x1 + 4 x2
约束条件:
-3x1 + x2 < = 6
x1 + 2x2 <= 4
x2 >= -3
'''
begin = time . time ( )
c = [ - 1 , 4 ]
A = [ [ - 3 , 1 ] , [ 1 , 2 ] ]
b = [ [ 6 ] , [ 4 ] ]
bounds = ( ( None , None ) , ( - 3 , None ) )
res = linprog ( c , A , b , None , None , bounds )
end = time . time ( )
print ( " 结果为: " , res . fun )
print ( " x的值位 " , res . x , " " , type ( res . x ) )
print ( end - begin , " \n " , ' ---------------------------------------- ' )
'''
线性规划问题
max z = x1 - 2x2 - 3x3
约束条件
-2x1 + x2 + x3 <= 9
-3x1 + x2 + 2x3 >= 4
4x1 -2x2 -x3 = -6
x1 >= -10 ,x2 >=0
'''
'''
scipy标准形式:
min w= -x1 + 2x2 + 3x3
约束:
-2x1 + x2 + x3 <= 9
3x1 - x2 - 2x3 <= -4
'''
c = ( - np . array ( [ 1 , - 2 , - 3 ] ) ) . tolist ( )
A = [ [ - 2 , 1 , 1 ] , [ - 3 , 1 , 2 ] ]
A [ 1 ] = ( - np . array ( A ) [ 1 ] ) . tolist ( )
b = [ [ 9 ] , [ 4 ] ]
b [ 1 ] = ( - np . array ( b [ 1 ] ) ) . tolist ( )
Aeq = [ [ 4 , - 2 , - 1 ] ]
beq = [ [ - 6 ] ]
LB = [ - 10 , 0 , None ]
UB = [ None ] * len ( c )
bounds = tuple ( zip ( LB , UB ) )
res = linprog ( c , A , b , Aeq , beq , bounds )
end = time . time ( )
print ( " 结果为: " , res . fun )
print ( " x的值位 " , res . x , " " , type ( res . x ) )
print ( end - begin , " \n " , ' ---------------------------------------- ' )
print ( res )