你希望通过几种常见算法的实现,了解python在数学建模中的能力。
python除了丰富的原生数据结构外,拥有强大的第三方软件包支持,例如矩阵运算库Numpy,数据处理库Pandas、机器学习库Sklearn、深度学习库Tenserflow&Pytorch、科学计算库Scipy、图形绘制库matplotlib、网络算法库Networkx。此外几乎针对任何领域,都有第三方软件包的支持,这归功于python优秀的社区。使用者需要使用好pip这一软件包管理工具,发掘前人造好的轮子,尽量减少自己编程的难度。我们将在后面的问题讨论中介绍以下几种常用数学建模算法的python实现:
4.单源多宿最短路算法
我们的重点在于代码实现而非数学推导
1.数据拟合算法
我们这里介绍通过最小二乘法拟合线性函数
#我们使用最小二乘法拟合一个三次函数,选取了5个参数importnumpyasnpimportmatplotlib.pyplotaspltSAMPLE_NUM =100M =5x = np.arange(0, SAMPLE_NUM).reshape(SAMPLE_NUM,1) / (SAMPLE_NUM -1) *10y =2*x3+3*x2+x+1plt.plot(x, y,'bo')X = xforiinrange(2, M +1):X = np.column_stack((X, pow(x, i)))X = np.insert(X,0, [1],1)W=np.linalg.inv((X.T.dot(X))).dot(X.T).dot(y)y_estimate = X.dot(W)plt.plot(x, y_estimate,'r')plt.show()

importnumpyasnpfromscipyimportinterpolateimportpylabasplx=np.linspace(0,10,11)y=2*x3+3*x2+x+1xInset=np.linspace(0,10,101)pl.plot(x,y,"ro")forkindin["nearest","zero","slinear","quadratic","cubic"]:f=interpolate.interp1d(x,y,kind=kind)y_estimate=f(xInset)pl.plot(xInset,y_estimate,label=str(kind))pl.legend(loc="lower right")pl.show()

importnumpyasnpfromscipy.optimizeimportminimizedeffunc(x):return(2*x[0]*x[1]+2*x[0]-x[0]2+2*x[1]2+np.sin(x[0]))cons=({"type":"eq","fun":lambdax:np.array([x[0]3-x[1]]),"jac":lambdax:np.array([3*(x[0]2),-1.0])},{"type":"ineq","fun":lambdax:np.array([x[1]-1]),"jac":lambdax:np.array([0,1])})#定义函数的多个约束条件res=minimize(func,[-1.0,1.0],constraints=cons,method="SLSQP",options={"disp":True})print(res)

classDisNode:def__init__(self,node,dis):self.node=nodeself.dis=disdef__lt__(self, other):returnself.disclassDisPath:def__init__(self,end):self.end=endself.path=[self.end]self.dis=0def__str__(self):nodes=self.path.copy()return"->".join(list(map(str,nodes)))+" "+str(self.dis)classHeap:def__init__(self):self.size=0self.maxsize=10000self.elements=[0]*(self.maxsize+1)defisEmpty(self):returnself.size==0definsert(self,value):ifself.isEmpty():self.elements[1]=valueelse:index=self.size+1while(index!=1andvalue2]): self.elements[index]=self.elements[index//2]index=index//2self.elements[index]=valueself.size+=1defpop(self):deleteElement=self.elements[1]self.elements[1]=self.elements[self.size]self.size-=1temp=self.elements[1]parent,child=1,2while(child<=self.size):ifchildandself.elements[child]>self.elements[child+1]: child+=1iftempbreakelse:self.elements[parent]=self.elements[child]parent=childchild*=2self.elements[parent]=tempreturndeleteElementdefDijkstraWithHeap(nodes,start,GetNeighbors):dis=defaultdict(int)paths=defaultdict(DisPath)heap=Heap()visit=set()fornodeinnodes:dis[node]=sys.maxsizepaths[node]=DisPath(node)dis[start]=0heap.insert(DisNode(start,0))while(notheap.isEmpty()):now=heap.pop().nodeifnowinvisit:continuevisit.add(now)paths[now].dis=dis[now]foredgeinGetNeighbors(now):end=edge.Endifdis[now]+edge.valuedis[end]=dis[now]+edge.valuepaths[end].path=paths[now].path+[end]heap.insert(DisNode(end,dis[end]))returnpaths











