本周主要是对考试科目进行复习,然后成功安装了了mindopt的python软件包,并用python语言运行了其中的案例。然后运行了fista算法对二维地震数据的重构,并已经改写好了的相应的ista算法。下面是在python中调用mindopt库对线性规划问题的求解。
对以下问题求解:
* Minimize
* obj: 1 x0 + 1 x1 + 1 x2 + 1 x3
* Subject To
* c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
* c2 : 1 x0 - 1 x2 + 6 x3 = 1
* Bounds
* 0 <= x0 <= 10
* 0 <= x1
* 0 <= x2
* 0 <= x3
1.创建模型
from mindoptpy import *
if __name__ == "__main__":
MDO_INFINITY = MdoModel.get_infinity()
model = MdoModel()
2.输入模型参数
try:
model.set_int_attr("MinSense", 1) #最小化问题
# 添加空白约束
cons = []
cons.append(model.add_cons(1.0, MDO_INFINITY, None, "c0"))
cons.append(model.add_cons(1.0, 1.0, None, "c1"))
# 约束条件相应参数
col = []
for j in range(4):
col.append(MdoCol())
col[0].add_term(cons[0], 1.0)#x0的系数
col[0].add_term(cons[1], 1.0)
col[1].add_term(cons[0], 1.0)
col[2].add_term(cons[0], 2.0)
col[2].add_term(cons[1], -1.0)
col[3].add_term(cons[0], 3.0)
col[3].add_term(cons[1], 6.0)
# 变量的范围约束
x = []
x.append(model.add_var(0.0, 10.0, 1.0, col[0], "x0", False))
x.append(model.add_var(0.0, MDO_INFINITY, 1.0, col[1], "x1", False))
x.append(model.add_var(0.0, MDO_INFINITY, 1.0, col[2], "x2", False))
x.append(model.add_var(0.0, MDO_INFINITY, 1.0, col[3], "x3", False))
3.求解约束优化问题
model.solve_prob()
model.display_results()
status_code, status_msg = model.get_status()
if status_msg == "OPTIMAL":
print("Optimizer terminated with an OPTIMAL status (code {0}).".format(status_code))
print("Primal objective : {0}".format(round(model.get_real_attr("PrimalObjVal"), 2)))
for curr_x in x:
print(" - x[{0}] : {1}".format(curr_x.get_index(), round(curr_x.get_real_attr("PrimalSoln"), 2)))
else:
print("Optimizer terminated with a(n) {0} status (code {1}).".format(status_msg, status_code))
except MdoError as e:
print("Received Mindopt exception.")
print(" - Code : {}".format(e.code))
print(" - Reason : {}".format(e.message))
except Exception as e:
print("Received exception.")
print(" - Explanation : {}".format(e))
finally:
model.free_mdl()
求解结果: