代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。