线性规划可以采用python库PULP,http://packages.python.org/PuLP/index.html
例如解
# Import PuLP modeler functions
from pulp import *
# Create the 'prob' variable to contain the problem data,第一个参数是描述这个问题的参数,可以自己取,第二个是描述最大化还是最小化
prob = LpProblem("The Whiskas Problem",LpMinimize)
# The 2 variables Beef and Chicken are created with a lower limit of zero,第一个参数的变量名,第二个是下界,3是上届,4是表示数据类型,默认是连续
x1=LpVariable("ChickenPercent",0,None,LpInteger)
x2=LpVariable("BeefPercent",0)
# The objective function is added to 'prob' first首先加目标函数
prob += 0.013*x1 + 0.008*x2, "Total Cost of Ingredients per can"
# The five constraints are entered其次加约束
prob += x1 + x2 == 100, "PercentagesSum"
prob += 0.100*x1 + 0.200*x2 >= 8.0, "ProteinRequirement"
prob += 0.080*x1 + 0.100*x2 >= 6.0, "FatRequirement"
prob += 0.001*x1 + 0.005*x2 <= 2.0, "FibreRequirement"
prob += 0.002*x1 + 0.005*x2 <= 0.4, "SaltRequirement"
# The problem data is written to an .lp file
prob.writeLP("WhiskasModel.lp")
# The problem is solved using PuLP's choice of Solver
prob.solve()
# The status of the solution is printed to the screen
print "Status:", LpStatus[prob.status]
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
print v.name, "=", v.varValue
# The optimised objective function value is printed to the screen
print "Total Cost of Ingredients per can = ", value(prob.objective)
一个例子,光照问题。p是光照的强度,r是距离,a_kj的由来是如果灯太低就会照不到,所以需要一个角度。
4种近似解法
凸约束优化解法