是否是一个过度约束的排班问题没有得到妥善解决?

如何解决是否是一个过度约束的排班问题没有得到妥善解决?

我正在尝试基于护士排班示例实施轮班安排系统。不幸的是,我做对了。显然,这似乎是一个过度约束的问题,需要特别注意和详细的自定义移动(我还没有开发任何自定义移动)和/或调整权重以及可能重新定义一些约束。 我不确定,我找不到出路。 以下是问题详情:

  1. 365 天(或至少 180 天)
  2. 每天有两班昼夜交替
  3. 25 家药店(没有特殊技能)分为 2 组:
    • A 组包含 zone1 药房
    • B 组包含 zone2 药房
  4. 3 个药房的地址相同(同一组 B 的成员)但被视为独立的。

限制条件

  1. 员工每天轮班硬约束
  2. 最小和最大白班数, 夜班和总分配相当分布。软约束
  3. 交替换班模式(D-N 或 N-D)->不需要的模式(N-N)和(D-D)硬约束
  4. 工作日之间的最少空闲天数软约束
  5. 没有连续工作日硬约束
  6. 区域覆盖范围:应为每个区域的 1 个药房分配日班和夜班。软约束
  7. 对于同一地址的员工:同一天没有 2 个任务硬约束,它们之间的时间 = 3 天(硬约束但如果需要放松问题可以软约束)

来自输入文件:dataset.xml

<Contract ID="0"><Description>fulltime</Description>
<SingleAssignmentPerDay weight="1">true</SingleAssignmentPerDay><MaxNumAssignments on="1" weight="1">13</MaxNumAssignments><MinNumAssignments on="1" weight="1">11</MinNumAssignments><MaxNumDayAssignments on="1" weight="1">7</MaxNumDayAssignments>
<MinNumDayAssignments on="1" weight="1">5</MinNumDayAssignments>
<MaxNumNightAssignments on="1" weight="1">7</MaxNumNightAssignments>
<MinNumNightAssignments on="1" weight="1">5</MinNumNightAssignments>
<MaxConsecutiveWorkingDays on="1" weight="1">1</MaxConsecutiveWorkingDays><MinConsecutiveWorkingDays on="0" weight="1">1</MinConsecutiveWorkingDays> <MaxConsecutiveFreeDays on="1" weight="1">12</MaxConsecutiveFreeDays>
<MinConsecutiveFreeDays on="1" weight="4">5</MinConsecutiveFreeDays>
<UnwantedPatterns>
<Pattern>0</Pattern>
<Pattern>1</Pattern>
</UnwantedPatterns> 
</Contract>  
Where Pattern{0,1}={(DAY,DAY),(NIGHT,NIGHT)}`

规则

    rule "oneShiftPerDay"
dialect "mvel"
    when
        $leftAssignment : ShiftAssignment($leftId : id,$pharmacy : pharmacy,$shiftDate : shiftDate,pharmacy != null)
        $rightAssignment : ShiftAssignment(pharmacy == $pharmacy,shiftDate == $shiftDate,id > $leftId)
    then
        scoreHolder.addHardConstraintMatch( kcontext,-10);
        
end

rule "insertPharmacyAssignmentTotal"
       salience 2 // Do these rules first (optional,for performance)
dialect "mvel"
    when
    MinMaxContractLine(contractLineType == ContractLineType.TOTAL_ASSIGNMENTS,enabled == true,$contract : contract)
    $pharmacy : Pharmacy(contract == $contract)
    $assignmentTotal : Number() from accumulate(
            $assignment : ShiftAssignment(pharmacy == $pharmacy,$sdi1:shiftDateDayIndex),count($assignment)
            )
     
    then
    
       insertLogical(new PharmacyAssignmentTotal($pharmacy,$assignmentTotal.intValue()));
           
end




  rule "insertPharmacyWorkSequence"
        salience 1 // Do these rules first (optional,for performance)
    when
        PharmacyConsecutiveAssignmentStart(
            $pharmacy : pharmacy,$firstDayIndex : shiftDateDayIndex
        )

        PharmacyConsecutiveAssignmentEnd(
            pharmacy == $pharmacy,shiftDateDayIndex >= $firstDayIndex,$lastDayIndex : shiftDateDayIndex
        )

        // There are no free days between the first and last day
        not PharmacyConsecutiveAssignmentEnd(
            pharmacy == $pharmacy,shiftDateDayIndex >= $firstDayIndex && < $lastDayIndex
        )
    then
        insertLogical(new PharmacyWorkSequence($pharmacy,$firstDayIndex,$lastDayIndex));
end


rule "insertFirstPharmacyFreeSequence"
        salience 1 // Do these rules first (optional,$lastDayIndexPlusOne : shiftDateDayIndex
        )

        // There are no working days before the first day
        not PharmacyConsecutiveAssignmentEnd(
            pharmacy == $pharmacy,shiftDateDayIndex < $lastDayIndexPlusOne
        )
        PharmacyRosterInfo(firstShiftDateDayIndex < $lastDayIndexPlusOne,$firstDayIndex : firstShiftDateDayIndex)
    then
        insertLogical(new PharmacyFreeSequence($pharmacy,$lastDayIndexPlusOne - 1));
end

rule "insertLastPharmacyFreeSequence"
        salience 1 // Do these rules first (optional,for performance)
    when
        PharmacyConsecutiveAssignmentEnd(
            $pharmacy : pharmacy,$firstDayIndexMinusOne : shiftDateDayIndex
        )

        // There are no working days after the last day
        not PharmacyConsecutiveAssignmentStart(
            pharmacy == $pharmacy,shiftDateDayIndex > $firstDayIndexMinusOne
        )
       PharmacyRosterInfo(lastShiftDateDayIndex > $firstDayIndexMinusOne,$lastDayIndex : lastShiftDateDayIndex)
    then
        insertLogical(new PharmacyFreeSequence($pharmacy,$firstDayIndexMinusOne + 1,$lastDayIndex));
end
rule "insertEntirePharmacyFreeSequence"
        salience 1 // Do these rules first (optional,for performance)
    when
        $pharmacy : Pharmacy()
        // There are no working days after the last day
        not PharmacyConsecutiveAssignmentStart(
            pharmacy == $pharmacy
        )
        PharmacyRosterInfo($firstDayIndex : firstShiftDateDayIndex,$lastDayIndex));
end
rule "insertPharmacyFreeSequence"
        salience 1 // Do these rules first (optional,$firstDayIndexMinusOne : shiftDateDayIndex
        )

        PharmacyConsecutiveAssignmentStart(
            pharmacy == $pharmacy,shiftDateDayIndex > $firstDayIndexMinusOne,$lastDayIndexPlusOne : shiftDateDayIndex
        )

        // There are no working days between the first and last day
        not PharmacyConsecutiveAssignmentStart(
            pharmacy == $pharmacy,shiftDateDayIndex > $firstDayIndexMinusOne && < $lastDayIndexPlusOne
        )
    then
        insertLogical(new PharmacyFreeSequence($pharmacy,$lastDayIndexPlusOne - 1));
end



rule "Minimum and Maximum total assignments"
salience 1
no-loop
    when
  
        $contractLine : MinMaxContractLine(
            contractLineType == ContractLineType.TOTAL_ASSIGNMENTS,maximumEnabled == true,$contract : contract,$maximumValue : maximumValue
        )
        $pharmacy: Pharmacy(contract == $contract)
          accumulate(
            $assignment : ShiftAssignment($pharmacy == pharmacy);
            $total : count($assignment)
        )
 then
   int totalInt = $total.intValue();
   if (totalInt < $contractLine.getMinimumValue()) {
            scoreHolder.addSoftConstraintMatch(kcontext,(totalInt - $contractLine.getMinimumValue()) * $contractLine.getMinimumWeight());
              helperWithMessage(drools,"minimum total ass for "+$pharmacy.getName()+" is "+$contractLine.getMinimumValue()+" > "+totalInt);       
        } else if (totalInt > $contractLine.getMaximumValue()) {
            scoreHolder.addSoftConstraintMatch(kcontext,($contractLine.getMaximumValue() - totalInt) * $contractLine.getMaximumWeight());
           helperWithMessage(drools,"maximum total ass for "+$pharmacy.getName()+" is "+$contractLine.getMaximumValue()+" < "+totalInt);       
                          } else {
            // Workaround for https://issues.redhat.com/browse/PLANNER-761
            scoreHolder.addSoftConstraintMatch(kcontext,0);      
    
        }

            
        
end
rule "Minimum and maximum number of day service assignments"
dialect "mvel"
    when
        $contractLine : MinMaxContractLine(contractLineType == ContractLineType.TOTAL_DAY_ASSIGNMENTS,$contract : contract)
  
           $pharmacy: Pharmacy(contract == $contract)
        accumulate(
            $assignment : ShiftAssignment($pharmacy == pharmacy,$shiftType:shift.getShiftType,$shiftType.toString=="DAY");
            $total : count($assignment)
        )
        
    then
        int totalInt = $total.intValue();
        if ($contractLine.isMinimumEnabled() && totalInt < $contractLine.getMinimumValue()) {
            scoreHolder.addSoftConstraintMatch(kcontext,(totalInt - $contractLine.getMinimumValue()) * $contractLine.getMinimumWeight());
        } else if ($contractLine.isMaximumEnabled() && totalInt > $contractLine.getMaximumValue()) {
            scoreHolder.addSoftConstraintMatch(kcontext,($contractLine.getMaximumValue() - totalInt) * $contractLine.getMaximumWeight());
        } else {
            // Workaround for https://issues.redhat.com/browse/PLANNER-761
            scoreHolder.addSoftConstraintMatch(kcontext,0);        
        }
end




rule "Minimum and maximum number of night service assignments"
dialect "mvel"
    when
        $contractLine : MinMaxContractLine(contractLineType == ContractLineType.TOTAL_NIGHT_ASSIGNMENTS,$contract : contract)
        $pharmacy: Pharmacy(contract == $contract)
          $total : Number() from  accumulate(
            $assignment : ShiftAssignment($pharmacy == pharmacy,$shiftType.toString=="NIGHT");       
            count($assignment)
        )
        
    then
        int totalInt = $total.intValue();
        if ($contractLine.isMinimumEnabled() && totalInt < $contractLine.getMinimumValue()) {
            scoreHolder.addSoftConstraintMatch(kcontext,0); helper(drools);
        }
end



rule "minimumConsecutiveFreeDays"
    when
        $contractLine : MinMaxContractLine(
            contractLineType == ContractLineType.CONSECUTIVE_FREE_DAYS,minimumEnabled == true,$minimumValue : minimumValue
        )
        $pharmacy : Pharmacy(contract == $contract)

        $pharmacyFreeSequence : PharmacyFreeSequence(
            pharmacy == $pharmacy,dayLength < $minimumValue,$dayLength : dayLength
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext,($dayLength-$minimumValue )* $contractLine.getMinimumWeight()); 
   end

rule "maxConsecutiveWorkingDays"
when
        $contractLine : MinMaxContractLine(
            contractLineType == ContractLineType.CONSECUTIVE_WORKING_DAYS,$maximumValue : maximumValue
        )

        PharmacyWorkSequence(
            getPharmacy().getContract() == $contract,dayLength > $maximumValue,($maximumValue - $dayLength) * $contractLine.getMaximumWeight());
end




rule "maximumConsecutiveFreeDays"
    when
        $contractLine : MinMaxContractLine(
            contractLineType == ContractLineType.CONSECUTIVE_FREE_DAYS,$maximumValue : maximumValue
        )
        $pharmacy : Pharmacy(contract == $contract)

        $pharmacyFreeSequence : PharmacyFreeSequence(
            pharmacy == $pharmacy,$dayLength : dayLength
        )
    then
     scoreHolder.addHardConstraintMatch(kcontext,($maximumValue - $dayLength )* $contractLine.getMaximumWeight()); helper(drools);
end


rule "insertPharmacyConsecutiveAssignmentStart"
        salience 2 // Do these rules first (optional,for performance)
    when
        ShiftAssignment(
            $pharmacy : pharmacy,pharmacy != null,$dayIndex : shiftDateDayIndex,$shiftDate : shiftDate
        )
        // The first day has no working day before it
        not ShiftAssignment(pharmacy == $pharmacy,shiftDateDayIndex == ($dayIndex - 1))
    then
        insertLogical(new PharmacyConsecutiveAssignmentStart($pharmacy,$shiftDate));
end
rule "insertPharmacyConsecutiveAssignmentEnd"
        salience 2 // Do these rules first (optional,$shiftDate : shiftDate
        )
        // The last day has no working day after it
        not ShiftAssignment(pharmacy == $pharmacy,shiftDateDayIndex == ($dayIndex + 1))
    then
       insertLogical(new PharmacyConsecutiveAssignmentEnd($pharmacy,$shiftDate));
end



rule "sameAddress4DaysFree"
dialect "mvel"
    when
    $leftAssignment : ShiftAssignment($leftPharmacy : pharmacy,$leftCode:pharmacy.getCode,$ldi : shiftDate.dayIndex,$leftAddress:pharmacy.getAddress(),pharmacy != null)
    ShiftAssignment($rightPharmacy:pharmacy,pharmacy.getAddress()==$leftAddress,$rightPharmacy.getCode()!=$leftCode,$sdi:shiftDateDayIndex,$sdi == $ldi,pharmacy != null)
    or ShiftAssignment($rightPharmacy:pharmacy,$rightPharmacy.getAddress()==$leftAddress,$sdi == $ldi+1,$sdi == $ldi+2,pharmacy != null)
   or ShiftAssignment($rightPharmacy:pharmacy,$sdi == $ldi+3,$sdi == $ldi+4,pharmacy != null)
   
  then 

  int penalty=$sdi-$ldi;
      scoreHolder.addHardConstraintMatch( kcontext,-4/penalty);
end

rule "alternateShiftTypes"

when
        $contractLine : MinMaxContractLine(
            contractLineType == ContractLineType.CONSECUTIVE_SAME_SHIFT_TYPE,$maximumValue : maximumValue
        )
        
           ShiftAssignment($pharmacy : pharmacy,$pharmacy != null,$shiftDate1:shiftDate,$dayIndex1:shiftDateDayIndex,$ShiftType1:shiftType,contract == $contract)
           ShiftAssignment($pharmacy== pharmacy,$dayIndex2:shiftDateDayIndex,$shiftDate2:shiftDate,$dayIndex2>$dayIndex1,$ShiftType2:shiftType,$ShiftType2!=$ShiftType1)
               not  ShiftAssignment($pharmacy== pharmacy,$dayIndex3:shiftDateDayIndex,$dayIndex3>$dayIndex1,$dayIndex3<$dayIndex2)
   then
      scoreHolder.addHardConstraintMatch( kcontext,1);
  end

rule "ZoneCoverage"
when
$leftAssignment : ShiftAssignment($leftId :id,$leftPharmacy : pharmacy,$leftCode:pharmacy.code,$leftShiftDate : shiftDate,$leftCentral:pharmacy.getCentral,pharmacy != null)
    $rightAssignment : ShiftAssignment(id > $leftId,$rightPharmacy : pharmacy,$rightPharmacy !=$leftPharmacy,shiftDate == $leftShiftDate,$leftCentral==pharmacy.getCentral,pharmacy != null)
    then
    scoreHolder.addSoftConstraintMatch(kcontext,-1);        
end

rule "evenlyDistributeTotalAssignments"
dialect "mvel"

when 
$sum:  Number() from 
accumulate(PharmacyAssignmentTotal($pharmacy:pharmacy,$assignmentTotal:total),sum($assignmentTotal)) 
    $sumSquared:Number() from accumulate(PharmacyAssignmentTotal($assignmentTotal:total),sum(squaredValue($assignmentTotal))
        )  
     then

getFairnessValue($sum.intValue(),$sumSquared.intValue()));
scoreHolder.addSoftConstraintMatch(kcontext,-1*getFairnessValue($sum.intValue(),$sumSquared.intValue())); 结束

rule "unwantedPatternShiftType2DaysPattern"

    when
        $pattern : ShiftType2DaysPattern(
            $dayIndex0ShiftType : dayIndex0ShiftType,$dayIndex1ShiftType : dayIndex1ShiftType
        )
        PatternContractLine(
            pattern == $pattern,$contract : contract
        )


        ShiftAssignment(
            shiftType == $dayIndex0ShiftType,contract == $contract,$firstDayIndex : shiftDateDayIndex
        )
      
       ShiftAssignment(shiftType == $dayIndex1ShiftType,pharmacy == $pharmacy,$dayIndex2 > $firstDayIndex )
       not ShiftAssignment($pharmacy== pharmacy,$dayIndex3>$firstDayIndex,$dayIndex3<$dayIndex2)
    
        
    then
         scoreHolder.addHardConstraintMatch(kcontext,-1);
end

Benchmark

无论我做什么,我都没有好的解决方案。可能是 2 个约束 freesequence 和同一地址药房的 freedays 之间的冲突? 性能很糟糕,我看到求解器陷入了局部最小值。 有什么想法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-