首页 > 其他分享 >5.4

5.4

时间:2024-10-15 11:37:02浏览次数:8  
标签:5.4 np fun 100 ineq type lambda

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,100,ineq,type,lambda
From: https://www.cnblogs.com/77i11/p/18467080

相关文章

  • py5.4-5.5
    importnumpyasnpfromscipy.optimizeimportminimize,Boundsdeffunc(x):returnnp.sum(np.sqrt(x))defcon(x):return1000-np.sum(x*np.arange(1,101))con1={'type':'ineq','fun':lambdax:10-x[0]}con2={'t......
  • 数学建模习题5.4
    importnumpyasnpfromscipy.optimizeimportminimizedefobjective(x):return-np.sum(np.sqrt(x)*np.arange(1,101))defconstraint1(x):returnx[1]-10defconstraint2(x):return20-(x[1]+2*x[2])defconstraint3(x):return30-(x[1]+2x[2]+3x......
  • 解决 Maven 插件报错:The plugin org.codehaus.mojo:flatten-maven-plugin:1.5.0 requi
    检查Maven版本:首先,确认当前使用的Maven版本是否与插件要求的版本一致。可以通过在命令行中输入 mvn-v 来查看当前Maven的版本信息。升级或降级Maven版本:如果当前Maven版本过低,需要升级到插件要求的版本;如果过高,可能需要降级。升级或降级Maven可以参考Maven的官......
  • 5.4
    fromscipy.optimizeimport*importnumpyasnpc=[-0.9,-0.45,0.05,-1.4,-0.95,-0.45,-1.9,-1.45,-0.95]A=[[-0.4,0,0,0.6,0,0,0.6,0,0],[0,-0.7,0,0,0.3,0,0,0.3,0],[-0.2,0,0,-0.2,0,0,0.8,0,0],[0,-0.5,0,0,-0.5,0,0,0.5,0],[0,0,-0.6,0,0,-0.6,0,0,0.4],[1,1,1,0,0,......
  • 5.4求解非凸非线性规划
    importnumpyasnpfromscipy.optimizeimportminimize#定义目标函数defobjective(x):return-np.sum(np.sqrt(x))#注意:scipy的minimize默认是最小化问题,所以这里取负号#定义约束条件constraints=[{'type':'ineq','fun':lambdax:10-x[0]}......
  • 5.4
    importnumpyasnpfromscipy.optimizeimportminimizedefobjective(x):return-np.sum(np.sqrt(x)*np.arange(1,101))defconstraint1(x):returnx[1]-10defconstraint2(x):return20-(x[1]+2x[2])defconstraint3(x):return30-(x[1]+2x[2]+3x[......
  • 5.4 5.5
    5.4求解下列非线性规划:点击查看代码importnumpyasnpfromscipy.optimizeimportminimizedefobjective(x):return-np.sum(np.sqrt(x)*np.arange(1,101))defconstraint1(x):returnx[1]-10defconstraint2(x):return20......
  • 【2024潇湘夜雨】WIN10_Pro_22H2.19045.4955软件选装纯净特别版9.20
    【系统简介】=============================================================1.本次更新母盘来自WIN10_Pro_22H2.19045.4955.2.全程离线精简、无人值守调用优化处理制作。部分优化适配系统可能要重启几次,即使显示适配失败也不要在意,可能部分优化不适用。3.OS版本号为19045.4955。......
  • 易优eyoucms网站php5.4版本,报错:Can't use method return value in write context
    当你在使用PHP5.4版本时遇到“Can'tusemethodreturnvalueinwritecontext”的错误,这通常是因为你在代码中错误地使用了方法返回值。这种错误通常发生在试图将方法返回值直接赋值给变量或用于其他上下文时。解决方案以下是一些常见的原因和解决方法:1.检查代码中的赋......
  • Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一
    截至目前,还剩eurekaclient和openfeign两个工程没有整合,但这两个工程本来就应该是一个工程。想一想,eurekaclient作为微服务的客户端,是真正的微服务业务处理模块;而openfeign工程作为服务间调用的例子,本就应该应用在微服务模块上,所以本章的内容不是单纯移植,而是创建两个服务模块serv......