From 7b70265a0d6b3a5ad3090e08a016339f5c9b5c71 Mon Sep 17 00:00:00 2001 From: JCHPJP <2858829498@qq.com> Date: Mon, 15 Jul 2024 16:51:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E7=BA=BF=E6=80=A7=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9A=84acipy=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../optimiaze模块/scipy函数.py | 56 +++++++++++++++++++ .../线性规划/optimiaze模块/例 5.3.py | 23 ++++++++ .../线性规划/插值和拟合/gray.py | 0 3 files changed, 79 insertions(+) create mode 100644 盘荣博/线性规划/optimiaze模块/scipy函数.py create mode 100644 盘荣博/线性规划/optimiaze模块/例 5.3.py create mode 100644 盘荣博/线性规划/插值和拟合/gray.py diff --git a/盘荣博/线性规划/optimiaze模块/scipy函数.py b/盘荣博/线性规划/optimiaze模块/scipy函数.py new file mode 100644 index 0000000..56aa404 --- /dev/null +++ b/盘荣博/线性规划/optimiaze模块/scipy函数.py @@ -0,0 +1,56 @@ +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) \ No newline at end of file diff --git a/盘荣博/线性规划/optimiaze模块/例 5.3.py b/盘荣博/线性规划/optimiaze模块/例 5.3.py new file mode 100644 index 0000000..0e7092d --- /dev/null +++ b/盘荣博/线性规划/optimiaze模块/例 5.3.py @@ -0,0 +1,23 @@ +import time + +import numpy as np +from scipy.optimize import linprog +start = time.time() +c = [-110,-120,-130,-110,-115,150] +c = (-np.array(c)).tolist() +A=[[1,1,0,0,0,0], + [0,0,1,1,1,0], + [8.8,6.1,2.0,4.2,5.0,-6], + [8.8,6.1,2.0,4.2,5.0,-3] + ] +A[3]=(-np.array(A[3])).tolist() +b=[[200],[250],[0],[0]] +Aeq =[[1,1,1,1,1,-1]] +beq = [[0]] +LB= [0]*len(c) +UB= [None]*len(c) +bounds= tuple(zip(LB,UB)) +res = linprog(c,A,b,Aeq,beq,bounds) +end = time.time() +print("最优解:\n",res.x) +print("目标函数最小值:\n",-res.fun) \ No newline at end of file diff --git a/盘荣博/线性规划/插值和拟合/gray.py b/盘荣博/线性规划/插值和拟合/gray.py new file mode 100644 index 0000000..e69de29