简体   繁体   English

加快CPLEX C ++代码的速度

[英]speed up CPLEX C++ code

This is the result of implementing my very first model in CPLEX C++ and I am very much surprised how slow and poor the quality is. 这是在CPLEX C ++中实现我的第一个模型的结果,我非常惊讶质量如此缓慢和糟糕。 I belief that much of it can be avoided by a better formulation. 我相信,通过更好的表述可以避免很多事情。 Can anyone help me improve the code, please? 有人可以帮我改善代码吗? Hints, ideas, thoughts ... everything is appreciated! 提示,想法,想法...一切都受到赞赏!

It is about scheduling exams within 5 days that have 2 timeslots each available. 这是关于在5天内安排考试,每个考试都有2个时段。 My input is the number of exams (first row first number) and conflicting exam pairs (first row second number) where I also know the number of students taking both exams(in the following rows -> exam1 exam2 #students taking both exams). 我的输入是考试数量(第一行第一数字)和冲突的考试对(第一行第二数字),在这里我还知道参加这两项考试的学生人数(在接下来的行-> exem1test2#参加这两项考试的学生中)。 The code you can find here and the instance here . 您可以在此处找到代码并在此处找到实例。

The constraints I am including are: 我包括的约束是:

  1. each exams is scheduled exactly once 每次考试只安排一次
  2. conflicting exams cannot be scheduled at the very same period 不能同时安排冲突的考试
  3. penalize if conflicting exams are scheduled on same day 如果在同一天安排了冲突的考试,则将受到处罚
  4. penalize if conflicting exams are scheduled on consecutive days 如果连续两天安排考试​​冲突,将受到处罚
  5. penalize if conflicting exams are scheduled on adjacent periods over night 如果在晚上的相邻时段安排了冲突的考试,则将受到处罚

I have the feeling that something is even wrong in the formulation because I cannot imagine that the value of the objective value is that high. 我感到在表述中甚至有些错误,因为我无法想象目标值的价值是如此之高。 Does anyone see the flaw? 有人看到这个缺陷吗? I'm stuck. 我被卡住了。

The problem might be in the loop where I try to figure out whether a soft constraint is violated or not. 问题可能出在我试图找出是否违反了软约束的循环中。 There I am looping over days but probably I accidentally overwrite my variables all the time. 我在那里循环了几天,但是可能我不小心一直覆盖我的变量。 Does anyone have an idea how to determine the binary variable indicating if the soft constraint is violated on any day (and of course it can happen only once but most probably it is not at the end). 是否有人知道如何确定二进制变量,该二进制变量指示软约束是否在任何一天都被违反(当然,它只能发生一次,但很可能不会在最后发生)。

Rather than debugging C++ code I just re-implemented the model using a modeling language (I believe that is quicker and a more pleasant way to spend a sunday evening). 我没有调试C ++代码,而是使用建模语言重新实现了模型(我相信这是度过一个周日夜晚的更快,更愉快的方式)。

(Note: updated after fixing bug). (注意:修正错误后更新)。 Here is my solution with a total penalty of 751: 这是我的解决方案,总罚金为751:

在此处输入图片说明

You should be able to plug this into your code and verify the results. 您应该能够将此插入您的代码并验证结果。

Note: it took about 900 seconds on an old laptop to prove optimality with Cplex. 注意:使用一台旧笔记本电脑花了大约900秒钟来证明Cplex的最佳性能。 If you want just a good solution, you can stop a little bit earlier: 如果您只想找到一个好的解决方案,可以早一点停止:

在此处输入图片说明

(Blue line is the objective and the red line is the best possible bound). (蓝线是目标,红线是最好的边界)。 To get good performance I helped Cplex a little bit with setting branching priorities and using some options suggested by a tuning run. 为了获得良好的性能,我帮助Cplex设置了分支优先级并使用了优化运行建议的一些选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM