import numpy as np
from scipy.optimize import minimize
def objective(x):
x1, x2, x3 = x
return -(2 * x1 + 3 * x1 ** 2 + 3 * x2 + x2 ** 2 + x3)
# 定義約束條件
def constraint1(x):
x1, x2 = x[:2] # 只取前兩個變數x1和x2
return 10 - (x1 + 2 * x1 ** 2 + x2 + 2 * x2 ** 2) # 假設沒有x5
def constraint2(x):
x1, x2, x3 = x
return 10 - (x1 + x1 ** 2 + x2 + x2 ** 2 - x3)
def constraint3(x):
x1, x3 = x[:2] # 只取前兩個變數x1和x3(注意這裡的x是切片,但x3實際上是第三個元素)
return x3 - (x1 ** 2 + x3)
def constraint4(x):
x1, x2 = x[:2] # 只取前兩個變數x1和x2
return x1 + 2 * x2 - 1
# 初始猜測值(只包含目標函式和約束中使用的變數)
x0 = [0.5, 0.5, 0.5]
# 定義約束條件的字典
con1 = {'type': 'ineq', 'fun': constraint1}
con2 = {'type': 'ineq', 'fun': constraint2}
con3 = {'type': 'eq', 'fun': constraint3} # 這是一個等式約束
con4 = {'type': 'ineq', 'fun': constraint4}
cons = [con1, con2, con3, con4]
# 求解最佳化問題
# 注意:bounds現在只針對x1, x2, x3
solution = minimize(objective, x0, method='SLSQP', constraints=cons,
bounds=[(None, None), (None, None), (None, None)])
# 輸出結果
print('Optimal solution:', solution.x)
print('Objective function value at optimal solution:', -solution.fun) # 注意取負值以得到最大化結果
print('學號:3015')
5.5
相關文章
- New in Mysql 5.5MySql
- 5.5py
- Centos5.5中安裝Mysql5.5過程分享CentOSMySql
- 5.5(衝刺七)
- Ubuntu14.04LAMP搭建(Apache2.47+MySQL5.5+PHP5.5)UbuntuLAMPApacheMySqlPHP
- 數字與字串5.5字串
- MySQL 5.5 mysqlimport介紹MySqlImport
- Laravel5.5 整合 ucenterLaravel
- Laravel 5.5 路由新特性Laravel路由
- php5.5yum源PHP
- mysql5.5安裝MySql
- 安裝mysql5.5MySql
- hacmp 5.5配置步驟ACM
- MySQL 5.5 模式匹配LIKEMySql模式
- MySQL 5.5 Master/Slave 配置MySqlAST
- Laravel 5.5 郵件支援多主題啦!(Laravel 5.5 新功能早知道)Laravel
- lumen5.5學習(二)
- lumen5.5學習(一)
- Mysql 5.5 重置root密碼MySql密碼
- MySQL 5.5 複製搭建流程MySql
- MySQL 5.5 mysqlbinlog 介紹MySql
- MySQL 5.5 統計資訊收集MySql
- CentOS升級MySQL到5.5CentOSMySql
- CentOS 5.5下安裝ActiveMQCentOSMQ
- CMAKE安裝mysql5.5MySql
- MySQL 5.5新特性詳解MySql
- 誰有borland OptimizeIt 5.5破解。
- MySQL 5.5常用資訊函式MySql函式
- Index of /Downloads/MySQL-5.5/IndexMySql
- virtualboxcentos7虛擬機器安裝lamp(apache+mysql5.5+php5.5+redis)CentOS虛擬機LAMPApacheMySqlPHPRedis
- redhat 5.5 配置網路yum源Redhat
- 【計組5.5】指令流水線
- vSphere Replication 5.5 安裝配置
- Laravel 5.5 validator 使用 request fromLaravel
- mysql server 5.5 version版本初識MySqlServer
- MySQL 5.5儲存引擎介紹MySql儲存引擎
- MySQL 5.5 mysqldump備份說明MySql
- CentOS 6.5下安裝MySQL 5.5CentOSMySql