日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python中do的用法,如何使用docplex(python)对优化问题中的约束进行建模?

發(fā)布時(shí)間:2025/3/15 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中do的用法,如何使用docplex(python)对优化问题中的约束进行建模? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我需要解決一個(gè)優(yōu)化問題,類似于背包問題。我在這篇文章中詳細(xì)介紹了優(yōu)化問題:

knapsack optimization with dynamic variables

實(shí)際上,我需要使用python而不是OPL,所以為了使用cplex優(yōu)化框架,我安裝了docplex和clpex包。在

下面是我想用docplex轉(zhuǎn)換成python的OPL代碼{string} categories=...;

{string} groups[categories]=...;

{string} allGroups=union (c in categories) groups[c];

{string} products[allGroups]=...;

{string} allProducts=union (g in allGroups) products[g];

float prices[allProducts]=...;

int Uc[categories]=...;

float Ug[allGroups]=...;

float budget=...;

dvar boolean z[allProducts]; // product out or in ?

dexpr int xg[g in allGroups]=(1<=sum(p in products[g]) z[p]);

dexpr int xc[c in categories]=(1<=sum(g in groups[c]) xg[g]);

maximize

sum(c in categories) Uc[c]*xc[c]+

sum(c in categories) sum(g in groups[c]) Uc[c]*Ug[g]*xg[g];

subject to

{

ctBudget:

sum(p in allProducts) z[p]*prices[p]<=budget;

}

{string} solution={p | p in allProducts : z[p]==1};

execute

{

writeln("solution = ",solution);

}

這是我的第一次代碼嘗試:

^{pr2}$

實(shí)際上我不知道如何正確地建模OPL代碼中的xg、xc和z變量?在

關(guān)于如何正確建模的任何想法。

提前謝謝你

編輯:這是@HuguesJuille建議后的編輯,我已經(jīng)清理了代碼,現(xiàn)在它可以正常工作了。在from docplex.mp.model import Model

from docplex.util.environment import get_environment

# ----------------------------------------------------------------------------

# Initialize the problem data

# ----------------------------------------------------------------------------

Categories_groups = {"Carbs": ["Meat","Milk"],"Protein":["Pasta","Bread"], "Fat": ["Oil","Butter"]}

Groups_Products = {"Meat":["Product11","Product12"], "Milk": ["Product21","Product22","Product23"], "Pasta": ["Product31","Product32"],

"Bread":["Product41","Product42"], "Oil":["Product51"],"Butter":["Product61","Product62"]}

Products_Prices ={"Product11":1,"Product12":4, "Product21":1,"Product22":3,"Product23":2,"Product31":4,"Product32":2,

"Product41":1,"Product42":3, "Product51": 1,"Product61":2,"Product62":1}

Uc={"Carbs": 1,"Protein":1, "Fat": 0 }

Ug = {"Meat": 0.8, "Milk": 0.2, "Pasta": 0.1, "Bread": 1, "Oil": 0.01, "Butter": 0.6}

budget=3;

def build_userbasket_model(**kwargs):

allcategories = Categories_groups.keys()

allgroups = Groups_Products.keys()

allproducts = Products_Prices.keys()

# Model

mdl = Model(name='userbasket', **kwargs)

z = mdl.binary_var_dict(allproducts, name='z([%s])')

xg = {g: 1 <= mdl.sum(z[p] for p in Groups_Products[g]) for g in allgroups}

xc = {c: 1 <= mdl.sum(xg[g] for g in Categories_groups[c]) for c in allcategories}

mdl.add_constraint(mdl.sum(Products_Prices[p] * z[p] for p in allproducts) <= budget)

mdl.maximize(mdl.sum(Uc[c] * xc[c] for c in allcategories) + mdl.sum(

xg[g] * Uc[c] * Ug[g] for c in allcategories for g in Categories_groups[c]))

mdl.solve()

return mdl

if __name__ == '__main__':

"""DOcplexcloud credentials can be specified with url and api_key in the code block below.

Alternatively, Context.make_default_context() searches the PYTHONPATH for

the following files:

* cplex_config.py

* cplex_config_.py

* docloud_config.py (must only contain context.solver.docloud configuration)

These files contain the credentials and other properties. For example,

something similar to::

context.solver.docloud.url = "https://docloud.service.com/job_manager/rest/v1"

context.solver.docloud.key = "example api_key"

"""

url = None

key = None

mdl = build_userbasket_model()

# will use IBM Decision Optimization on cloud.

if not mdl.solve(url=url, key=key):

print("*** Problem has no solution")

else:

mdl.float_precision = 3

print("* model solved as function:")

mdl.print_solution()

# Save the CPLEX solution as "solution.json" program output

with get_environment().get_output_stream("solution.json") as fp:

mdl.solution.export(fp, "json")

我希望這能幫助像我這樣有同樣問題的初學(xué)者。在

總結(jié)

以上是生活随笔為你收集整理的python中do的用法,如何使用docplex(python)对优化问题中的约束进行建模?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。