[jboss-svn-commits] JBL Code SVN: r27906 - labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/pas/solver/solution/initializer.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Jul 12 11:04:51 EDT 2009


Author: ge0ffrey
Date: 2009-07-12 11:04:51 -0400 (Sun, 12 Jul 2009)
New Revision: 27906

Modified:
   labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/pas/solver/solution/initializer/PatientAdmissionScheduleStartingSolutionInitializer.java
Log:
pas: intializer does checkSameBedInSameNight

Modified: labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/pas/solver/solution/initializer/PatientAdmissionScheduleStartingSolutionInitializer.java
===================================================================
--- labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/pas/solver/solution/initializer/PatientAdmissionScheduleStartingSolutionInitializer.java	2009-07-12 14:09:05 UTC (rev 27905)
+++ labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/pas/solver/solution/initializer/PatientAdmissionScheduleStartingSolutionInitializer.java	2009-07-12 15:04:51 UTC (rev 27906)
@@ -4,6 +4,10 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Comparator;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
 
 import org.drools.FactHandle;
 import org.drools.WorkingMemory;
@@ -17,6 +21,7 @@
 import org.drools.solver.examples.pas.domain.BedDesignation;
 import org.drools.solver.examples.pas.domain.PatientAdmissionSchedule;
 import org.drools.solver.examples.pas.domain.Room;
+import org.drools.solver.examples.pas.domain.Night;
 import org.drools.solver.examples.itc2007.examination.domain.Period;
 import org.apache.commons.lang.builder.CompareToBuilder;
 
@@ -25,6 +30,8 @@
  */
 public class PatientAdmissionScheduleStartingSolutionInitializer extends AbstractStartingSolutionInitializer {
 
+    private boolean checkSameBedInSameNight = true;
+
     @Override
     public boolean isSolutionInitialized(LocalSearchSolverScope localSearchSolverScope) {
         PatientAdmissionSchedule patientAdmissionSchedule = (PatientAdmissionSchedule) localSearchSolverScope.getWorkingSolution();
@@ -41,12 +48,19 @@
             PatientAdmissionSchedule patientAdmissionSchedule) {
         WorkingMemory workingMemory = localSearchSolverScope.getWorkingMemory();
         List<BedDesignation> bedDesignationList = createBedDesignationList(patientAdmissionSchedule);
+        Map<Bed, Set<Integer>> bedToTakenNightIndexSetMap = null;
+        if (checkSameBedInSameNight) {
+            bedToTakenNightIndexSetMap = new HashMap<Bed, Set<Integer>>(
+                    patientAdmissionSchedule.getBedList().size());
+        }
         // Assign one admissionPart at a time
         List<Bed> bedListInPriority = new ArrayList(patientAdmissionSchedule.getBedList()); // TODO try LinkedList
 int stillRunningCounter = 0; // TODO https://jira.jboss.org/jira/browse/JBRULES-2145
         for (BedDesignation bedDesignation : bedDesignationList) {
 System.out.println("Trunk is bugged " + ++stillRunningCounter +"/" + bedDesignationList.size() + " but we do not use trunk. See JBRULES-2145.");
             Score unscheduledScore = localSearchSolverScope.calculateScoreFromWorkingMemory();
+            int firstNightIndex = bedDesignation.getAdmissionPart().getFirstNight().getIndex();
+            int lastNightIndex = bedDesignation.getAdmissionPart().getLastNight().getIndex();
             boolean perfectMatch = false;
             Score bestScore = DefaultHardAndSoftScore.valueOf(Integer.MIN_VALUE);
             Bed bestBed = null;
@@ -56,38 +70,78 @@
             // TODO by reordening the beds so index 0 has a different table then index 1 and so on,
             // this will probably be faster because perfectMatch will be true sooner
             for (Bed bed : bedListInPriority) {
-                if (bed.allowsAdmissionPart(bedDesignation.getAdmissionPart())) {
-                    if (bedDesignationHandle == null) {
-                        bedDesignation.setBed(bed);
-                        bedDesignationHandle = workingMemory.insert(bedDesignation);
-                    } else {
-                        workingMemory.modifyRetract(bedDesignationHandle);
-                        bedDesignation.setBed(bed);
-                        workingMemory.modifyInsert(bedDesignationHandle, bedDesignation);
+                if (!bed.allowsAdmissionPart(bedDesignation.getAdmissionPart())) {
+                    continue;
+                }
+                if (checkSameBedInSameNight) {
+                    boolean taken = false;
+                    Set<Integer> takenNightIndexSet = bedToTakenNightIndexSetMap.get(bed);
+                    if (takenNightIndexSet != null) {
+                        for (int i = firstNightIndex; i <= lastNightIndex; i++) {
+                            if (takenNightIndexSet.contains(i)) {
+                                taken = true;
+                                break;
+                            }
+                        }
                     }
-                    Score score = localSearchSolverScope.calculateScoreFromWorkingMemory();
-                    if (score.compareTo(unscheduledScore) < 0) {
-                        if (score.compareTo(bestScore) > 0) {
-                            bestScore = score;
-                            bestBed = bed;
-                        }
-                    } else if (score.equals(unscheduledScore)) {
-                        perfectMatch = true;
+                    if (taken) {
+                        continue;
+                    }
+                }
+                if (bedDesignationHandle == null) {
+                    bedDesignation.setBed(bed);
+                    bedDesignationHandle = workingMemory.insert(bedDesignation);
+                } else {
+                    workingMemory.modifyRetract(bedDesignationHandle);
+                    bedDesignation.setBed(bed);
+                    workingMemory.modifyInsert(bedDesignationHandle, bedDesignation);
+                }
+                Score score = localSearchSolverScope.calculateScoreFromWorkingMemory();
+                if (score.compareTo(unscheduledScore) < 0) {
+                    if (score.compareTo(bestScore) > 0) {
                         bestScore = score;
                         bestBed = bed;
-                        break;
-                    } else {
-                        throw new IllegalStateException("The score (" + score
-                                + ") cannot be higher than unscheduledScore (" + unscheduledScore + ").");
                     }
+                } else if (score.equals(unscheduledScore)) {
+                    perfectMatch = true;
+                    bestScore = score;
+                    bestBed = bed;
+                    break;
+                } else {
+                    throw new IllegalStateException("The score (" + score
+                            + ") cannot be higher than unscheduledScore (" + unscheduledScore + ").");
                 }
                 if (perfectMatch) {
                     break;
                 }
             }
             if (bestBed == null) {
-                throw new IllegalStateException("The bestBed (" + bestBed + ") cannot be null.");
+                if (checkSameBedInSameNight) {
+                    throw new IllegalArgumentException(
+                            "The initializer could not locate an allowed and empty bed for admissionPart ("
+                                    + bedDesignation.getAdmissionPart() + ").");
+                } else {
+                    throw new IllegalArgumentException(
+                            "The initializer could not locate an allowed bed for admissionPart ("
+                                    + bedDesignation.getAdmissionPart() + ").");
+                }
             }
+            if (checkSameBedInSameNight) {
+                Set<Integer> takenNightIndexSet = bedToTakenNightIndexSetMap.get(bestBed);
+                if (takenNightIndexSet == null) {
+                    takenNightIndexSet = new HashSet<Integer>(patientAdmissionSchedule.getNightList().size());
+                    bedToTakenNightIndexSetMap.put(bestBed, takenNightIndexSet);
+                }
+                if (takenNightIndexSet != null) {
+                    for (int i = firstNightIndex; i <= lastNightIndex; i++) {
+                        boolean unique = takenNightIndexSet.add(i);
+                        if (!unique) {
+                            throw new IllegalStateException(
+                                    "The takenNightIndexSet cannot possibly already have nightIndex (" + i + ").");
+                        }
+                    }
+                }
+            }
             if (!perfectMatch) {
                 workingMemory.modifyRetract(bedDesignationHandle);
                 bedDesignation.setBed(bestBed);




More information about the jboss-svn-commits mailing list