单纯形法——Python实现(一)

1 介绍

  本文所提供的单纯形法Python实现,基于sympynumpy库。使用时请先安装相关库。

  优点:可以直接按目标函数和不等式约束的原形式输入。
  缺点(BUG):

  • 所有变量必须>=0
  • 未解决约束条件全为等式情况

  注意:对于等式约束,如 x 1 + x 2 = 5 x_1 + x_2 = 5 x1+x2=5 其代码输入格式为c0 = (x1+x2, 5)

2 安装相关库

	pip install numpy 
	pip install sympy

3 单纯形法Python实现

simplex.py 代码内容参见 我的上传(点这里),代码测试后上传,没得错误~

4 算例


from sympy import *
from simplex import *


def example_0():

    x1, x2, x3 = symbols('x1, x2, x3')
    obj = -3 * x1 + x2 + x3
    variables = [x1, x2, x3]

    c0 = -4 * x1 + x2 + 2 * x3 >= 3
    c1 = x1 - 2 * x2 + x3 <= 11
    c2 = (-2 * x1 + x3, 1)    # 等式约束
    constrs = [c0, c1, c2]
    constrs_com = [GEQ, LEQ, EQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', simplex.optimal_y)


def example_1():

    x1, x2 = symbols('x1, x2')
    obj = -(2*x1 + 3*x2)
    variables = [x1, x2]

    c0 = x1 + 2*x2 <= 8
    c1 = 4*x1 <= 16
    c2 = 4*x2 <= 12
    constrs = [c0, c1, c2]
    constrs_com = [LEQ, LEQ, LEQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', -simplex.optimal_y)


def example_2():

    # X=[21/5, 6/5], min_f=-18
    x1, x2 = symbols('x1, x2')
    obj = -4*x1 - x2
    variables = [x1, x2]

    c0 = -x1 + 2*x2 <= 4
    c1 = 2*x1 + 3*x2 <= 12
    c2 = x1 - x2 <= 3
    constrs = [c0, c1, c2]
    constrs_com = [LEQ, LEQ, LEQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', simplex.optimal_y)


def example_3():

    x1, x2, x3, x4, x5 = symbols('x1, x2, x3, x4, x5')
    obj = 2*x1 + 3*x2 + 5*x3 + 2*x4 + 3*x5
    variables = [x1, x2, x3, x4, x5]

    c0 = x1 + x2 + 2*x3 + x4 + 3*x5 >= 4
    c1 = 2*x1 - x2 + 3*x3 + x4 + x5 >= 3
    constrs = [c0, c1]
    constrs_com = [GEQ, GEQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', simplex.optimal_y)


def example_4():

    # X=[3, 0], max_f=6
    x1, x2 = symbols('x1, x2')
    obj = -(2*x1 - x2)
    variables = [x1, x2]

    c0 = x1 + x2 >= 2
    c1 = x1 - x2 >= 1
    c2 = x1 <= 3
    constrs = [c0, c1, c2]
    constrs_com = [GEQ, GEQ, LEQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', -simplex.optimal_y)


def example_5():

    # X=[0, 12, 5, 8], min_f=-19
    x1, x2, x3, x4 = symbols('x1, x2, x3, x4')
    obj = x1 - 2*x2 + x3
    variables = [x1, x2, x3, x4]

    c0 = 2*x1 - x2 + 4*x3 <= 8
    c1 = -x1 + 2*x2 - 4*x3 <= 4
    c2 = (x1 + x2 - 2*x3 + x4, 10)
    constrs = [c0, c1, c2]
    constrs_com = [LEQ, LEQ, EQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', simplex.optimal_y)


def example_6():

    # X=[0,1, 0], min_f=-1
    x1, x2, x3 = symbols('x1, x2, x3')
    obj = x1 - x2
    variables = [x1, x2, x3]

    c0 = -x1 + 2*x2 + x3 <= 2
    c1 = (-4*x1+4*x2-x3, 4)
    c2 = (x1-x3, 0)
    constrs = [c0, c1, c2]
    constrs_com = [LEQ, EQ, EQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', simplex.optimal_y)


def example_7():

    x1, x2, x3 = symbols('x1, x2, x3')
    obj = 3*x1 - 2*x2 + x3
    variables = [x1, x2, x3]

    c0 = 2*x1 + 3*x2 >= 8
    c1 = (2*x1 - 3*x2 + x3, 1)
    constrs = [c0, c1]
    constrs_com = [GEQ, EQ]

    simplex = Simplex(obj, variables, constrs, constrs_com)
    simplex.start()
    print('==> X=', simplex.optimal_X)
    print('==> y=', simplex.optimal_y)


def main():

    print('-'*25, '算例', '-'*25)
    print('- 算例0 - 约束含有">=", "<="和“=”, 所有变量均>=0, 求min')
    print('- 算例1 - 约束全为"<=", 所有变量均>=0, 求max')
    print('- 算例2 - 约束全为"<=", 所有变量均>=0, 求mix')
    print('- 算例3 - 约束全为">=", 所有变量均>=0, 求min')
    print('- 算例4 - 约束含有">="和"<=", 所有变量均>=0, 求max')
    print('- 算例5 - 约束含有"<="和“=”, 所有变量均>=0, 求min')
    print('- 算例6 - 约束含有"<="和“=”, 所有变量均>=0, 求min')
    print('- 算例7 - 约束含有">="和“=”, 所有变量均>=0, 求min')
    print('-' * 56)

    ex = input('输入算例编号(0~7):')
    if int(ex) == 0:
        example_0()
    elif int(ex) == 1:
        example_1()
    elif int(ex) == 2:
        example_2()
    elif int(ex) == 3:
        example_3()
    elif int(ex) == 4:
        example_4()
    elif int(ex) == 5:
        example_5()
    elif int(ex) == 6:
        example_6()
    elif int(ex) == 7:
        example_7()


if __name__ == "__main__":
    main()

你可能感兴趣的:(最优化)