首页 > 其他分享 >习题5.4

习题5.4

时间:2024-10-22 19:02:44浏览次数:7  
标签:5.4 np fun 习题 ineq type 100 lambda

习题5.4代码

import numpy as np

import math

from scipy.optimize import minimize, Bounds

def func(x):
return sum(math.sqrt(x[i]) for i in range(100))

def con(x):
return 1000 - np.sum(x[i] * (101 - i + 1) for i in range(100))

con1 = {'type': 'ineq', 'fun': lambda x: 10 - x[0]}

con2 = {'type': 'ineq', 'fun': lambda x: 20 - x[0] - 2 * x[1]}

con3 = {'type': 'ineq', 'fun': lambda x: 30 - x[0] - x[1] * 2 - x[2] * 3}

con4 = {'type': 'ineq', 'fun': lambda x: 40 - x[0] - x[1] * 2 - x[2] * 3 - x[3] * 4}

con5 = {'type': 'ineq', 'fun': con}

cons = [con1, con2, con3, con4, con5]

bounds = Bounds([0] * 100, np.inf * 100)

res = minimize(func, np.random.randn(100), constraints=cons, bounds=bounds)

print(res.x)

print(res.fun)

标签:5.4,np,fun,习题,ineq,type,100,lambda
From: https://www.cnblogs.com/DENWOSHUABAZONGSHI/p/18492481

相关文章

  • 循环结构程序设计之习题
    输入两个正整数m和n,求其最大公约数和最小公倍数//输入两个正整数m和n,求其最大公约数和最小公倍数#include<stdio.h>intmain(void){ intm,n,iMax,iMin,iGcd; scanf("%d%d",&m,&n); if(m>n) { iMax=m; iMin=n; } else { iMax=n; ......
  • 习题2.5
    习题2.5代码importnumpyasnpimportpandasaspdimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext......
  • python第六章课后习题
    点击查看代码print("学号:2023310143028")点击查看代码defprim(graph,start):num_nodes=len(graph)visited=[False]*num_nodesmin_heap=[(0,start,-1)]mst_cost=0mst_edges=[]whilemin_heap:......
  • python第四章课后习题
    点击查看代码importnumpyasnpimportcvxpyascpx=cp.Variable(6,pos=True)obj=cp.Minimize(x[5])a1=np.array([0.025,0.015,0.055,0.026])a2=np.array([0.05,0.27,0.19,0.185,0.185])a3=np.array([1,1.01,1.02,1.045,1.065])k=0.05;kk=[];qq=[]whil......
  • 二叉树习题其三-Java【力扣】【算法学习day.10】
    前言书接上篇文章二叉树习题其二,这篇文章我们将基础拓展###我做这类文档一个重要的目的还是给正在学习的大家提供方向(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!习题1.从中序与后序遍历序......
  • 习题2.3
    习题2.3代码importnumpyasnpimportpandasaspdimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext......
  • 习题2.2
    习题2.2代码importnumpyasnpimportpandasaspdimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext......
  • 习题2.1
    习题2.1代码importnumpyasnpimportpandasaspdimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext......
  • 习题2.9
    习题2.9代码importnumpyasnpimportpandasaspdimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext......
  • 习题2.8
    习题2.8代码importnumpyasnpimportpandasaspdimportsympyasspsp.init_printing(use_unicode=True)importmatplotlib.pyplotaspltplt.rcParams['font.sans-serif']=['TimesNewRoman+SimSun+WFMSansSC']plt.rcParams['mathtext......