加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ortools_cond.py 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
曾振宇 提交于 2021-05-15 15:31 . 将约束转化为ortools代码
from __future__ import print_function
from ortools.sat.python import cp_model
import tex2ortools
import time
def main(frame_cond:tex2ortools.Frame_cond):
#建立模型
model = cp_model.CpModel()
n = frame_cond.count
#创建变量
#frame变量
frames_x = []#x坐标
frames_y = []#y坐标
frames_w = []#w宽度
frames_h = []#h高度
frames_v = []#v面积
ends_x = []#x+w
ends_y = []#x+h
intervals_x = []#(x,x+w)
intervals_y = []#(y,y+h)
#article变量
#pass
for i in range(n):
end_x = model.NewIntVar(0,100,'end_frame_%d' % i)
end_y = model.NewIntVar(0,100,'end_frame_%d' % i)
frame_x = model.NewIntVar(0,100,'frame_%d_x' % i)
frame_y = model.NewIntVar(0,100,'frame_%d_y' % i)
frame_w = model.NewIntVar(10,100,'frame_%d_w' % i)
frame_h = model.NewIntVar(10,100,'frame_%d_h' % i)
frame_v = model.NewIntVar(100,10000,'frame_%d_v' % i)
ends_x.append(end_x)
ends_y.append(end_y)
frames_x.append(frame_x)
frames_y.append(frame_y)
frames_w.append(frame_w)
frames_h.append(frame_h)
frames_v.append(frame_v)
for i in range(n):
interval_x = model.NewIntervalVar(frames_x[i], frames_w[i], ends_x[i],'frame_%d_xp' % i )
interval_y = model.NewIntervalVar(frames_y[i], frames_h[i], ends_y[i],'frame_%d_yp' % i )
intervals_x.append(interval_x)
intervals_y.append(interval_y)
#增加约束(article)
#pass
############################################################
#增加约束(frame)
for i in frame_cond.x_equal_0:
model.Add(frames_x[i] == 0)
for i,j in frame_cond.x_equal_x:
model.Add(frames_x[i] == frames_x[j])
for i,j in frame_cond.w_equal_w:
model.Add(frames_w[i] == frames_w[j])
for i,j in frame_cond.xw_equal_x:
model.Add(frames_x[i] + frames_w[i] == frames_x[j])
for i in frame_cond.xw_equal_100:
model.Add(frames_x[i] + frames_w[i] == 100)
for i in frame_cond.y_equal_0:
model.Add(frames_y[i] == 0)
for i,j in frame_cond.y_equal_y:
model.Add(frames_y[i] == frames_y[j])
for i,j in frame_cond.h_equal_h:
model.Add(frames_h[i] == frames_h[j])
for i,j in frame_cond.yh_equal_y:
model.Add(frames_y[i] + frames_h[i] == frames_y[j])
for i in frame_cond.yh_equal_100:
model.Add(frames_y[i] + frames_h[i] == 100)
#一些防止变形的约束
#pass
################################################################
#通用约束
for i in range(n):
model.Add(frames_x[i] + frames_w[i] == ends_x[i])#interval_x的end
model.Add(frames_y[i] + frames_h[i] == ends_y[i])#interval_y的end
model.AddMultiplicationEquality(frames_v[i],[frames_w[i],frames_h[i]])#w*h=v
model.Add(cp_model.LinearExpr.Sum(frames_v) == 10000)#矩形面积之和为10000
model.AddNoOverlap2D(intervals_x,intervals_y)#矩形之间互不重叠
#####################
#补充强制约束
# model.Add(articles_vs[1] == 30)
# model.Add(articles_ts[1] == 27)
# model.Add(articles_Ts[1] == 80)
#####################
#目标函数
model.Maximize(sum(frames_x))
#创建求解器并求解
solver = cp_model.CpSolver()
status = solver.Solve(model)
if status == cp_model.OPTIMAL:
print('Maximum of objective function: %i' % solver.ObjectiveValue())
for i in range(n):
print('frame_%i_x: ' % i, solver.Value(frames_x[i]))
print('frame_%i_y: ' % i, solver.Value(frames_y[i]))
print('frame_%i_w: ' % i, solver.Value(frames_w[i]))
print('frame_%i_h: ' % i, solver.Value(frames_h[i]))
print('frame_%i_v: ' % i, solver.Value(frames_v[i]))
print('============================')
else:
print("no optimal solution")
if __name__ == '__main__':
start = time.time()
frame_cond = tex2ortools.main()
main(frame_cond)
end = time.time()
print('running time',end - start)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化