python minimize_Python数学规划案例一
Python數(shù)學規(guī)劃案例一
問題、模型、數(shù)據(jù)、算法、結(jié)果,統(tǒng)一地表述,是習慣也是效率。
我的公眾號
數(shù)學規(guī)劃模型表述習慣
采用五個部分:Set, Data, Variable, Objective, Constraints;每個Notation,采用一個主字符,上標表示含義,下標來自Set;已知的大寫,未知的小寫,下標用小寫;妥善選擇Notation中的每一個字符;對Notation、Objective、Constratins進行分組。模型即代碼、即注釋、即文章。
數(shù)學規(guī)劃模型編碼習慣
Ptyhon程序設計代碼與模型表述嚴格一致 Set, Data, Variable, Objective, Constraints盡量相應“翻譯”;將模型、數(shù)據(jù)預處理、模型結(jié)果后處理,分離開來,保持模型的干凈、純粹。代碼即模型、即文檔、即文章。
一個例子
預算約束下營養(yǎng)配餐。取自:
...\IBM\ILOG\CPLEX_Studio129\python\examples\mp\modeling\diet.py
模型
采用五個部分:Set, Data, Variable, Objective, Constraints建模。
代碼
根據(jù)diet.py改寫的代碼如下。
# -*- coding: utf-8 -*-
from collections import namedtuple
from docplex.mp.model import Model
from docplex.util.environment import get_environment
# ----------------------------------------------------------------------------
# Initialize the problem data
# ----------------------------------------------------------------------------
FOODS = [
("Roasted Chicken", 0.84, 0, 10),
("Spaghetti W/ Sauce", 0.78, 0, 10),
("Tomato,Red,Ripe,Raw", 0.27, 0, 10),
("Apple,Raw,W/Skin", .24, 0, 10),
("Grapes", 0.32, 0, 10),
("Chocolate Chip Cookies", 0.03, 0, 10),
("Lowfat Milk", 0.23, 0, 10),
("Raisin Brn", 0.34, 0, 10),
("Hotdog", 0.31, 0, 10)
]
NUTRIENTS = [
("Calories", 2000, 2500),
("Calcium", 800, 1600),
("Iron", 10, 30),
("Vit_A", 5000, 50000),
("Dietary_Fiber", 25, 100),
("Carbohydrates", 0, 300),
("Protein", 50, 100)
]
FOOD_NUTRIENTS = [
("Roasted Chicken", 277.4, 21.9, 1.8, 77.4, 0, 0, 42.2),
("Spaghetti W/ Sauce", 358.2, 80.2, 2.3, 3055.2, 11.6, 58.3, 8.2),
("Tomato,Red,Ripe,Raw", 25.8, 6.2, 0.6, 766.3, 1.4, 5.7, 1),
("Apple,Raw,W/Skin", 81.4, 9.7, 0.2, 73.1, 3.7, 21, 0.3),
("Grapes", 15.1, 3.4, 0.1, 24, 0.2, 4.1, 0.2),
("Chocolate Chip Cookies", 78.1, 6.2, 0.4, 101.8, 0, 9.3, 0.9),
("Lowfat Milk", 121.2, 296.7, 0.1, 500.2, 0, 11.7, 8.1),
("Raisin Brn", 115.1, 12.9, 16.8, 1250.2, 4, 27.9, 4),
("Hotdog", 242.1, 23.5, 2.3, 0, 0, 18, 10.4)
]
Food = namedtuple("Food", ["name", "unit_cost", "qmin", "qmax"])
Nutrient = namedtuple("Nutrient", ["name", "qmin", "qmax"])
# ----------------------------------------------------------------------------
# Build the model
# ----------------------------------------------------------------------------
def build_diet_model(**kwargs):
# Create tuples with named fields for foods and nutrients
F = [f[0] for f in FOODS]
C = {f[0]:f[1] for f in FOODS}
Fmin = {f[0]:f[2] for f in FOODS}
Fmax = {f[0]:f[3] for f in FOODS}
N = [n[0] for n in NUTRIENTS]
Nmin = {n[0]:n[1] for n in NUTRIENTS}
Nmax = {n[0]:n[2] for n in NUTRIENTS}
D = {(F[f],N[n]): FOOD_NUTRIENTS[f][n+1] for f in range(len(F)) for n in range(len(N))}
# Model
mdl = Model(name='diet', **kwargs)
# Decision variables, limited to be >= Food.qmin and <= Food.qmax
x = mdl.continuous_var_dict(F, lb=Fmin, ub=Fmax, name=F) # 2,4
# Limit range of nutrients, and mark them as KPIs
for n in N:
y = mdl.sum(x[f] * D[f, n] for f in F)
mdl.add_range(Nmin[n], y, Nmax[n]) # 3
mdl.add_kpi(y, publish_name="Total %s" % n)
for f in F:
mdl.add_kpi(x[f], publish_name="Food %s" % f)
# Minimize cost
mdl.minimize(mdl.sum(x[f] * C[f] for f in F)) # 1
return mdl
# ----------------------------------------------------------------------------
# Solve the model and display the result
# ----------------------------------------------------------------------------
if __name__ == '__main__':
mdl = build_diet_model()
mdl.print_information()
mdl.export_as_lp()
if mdl.solve():
mdl.float_precision = 3
print("* model solved as function:")
mdl.print_solution()
mdl.report_kpis()
# Save the CPLEX solution as "solution.json" program output
with get_environment().get_output_stream("diet2.json") as fp:
mdl.solution.export(fp, "json")
else:
print("* model has no solution")
擴展閱讀
Python數(shù)學規(guī)劃之Cplex之旅
Python利器
Python哲學
Python研究生
總結(jié)
以上是生活随笔為你收集整理的python minimize_Python数学规划案例一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 福建省考计算机专业,2020福建省考,这
- 下一篇: java调用hbase_Java调用Hb