[jboss-svn-commits] JBL Code SVN: r17766 - labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/itc2007/examination/persistence.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Jan 12 11:26:11 EST 2008
Author: ge0ffrey
Date: 2008-01-12 11:26:11 -0500 (Sat, 12 Jan 2008)
New Revision: 17766
Modified:
labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/itc2007/examination/persistence/ExaminationInputConvertor.java
Log:
make inputconvertor more scalable
Modified: labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/itc2007/examination/persistence/ExaminationInputConvertor.java
===================================================================
--- labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/itc2007/examination/persistence/ExaminationInputConvertor.java 2008-01-12 11:49:51 UTC (rev 17765)
+++ labs/jbossrules/trunk/drools-solver/drools-solver-examples/src/main/java/org/drools/solver/examples/itc2007/examination/persistence/ExaminationInputConvertor.java 2008-01-12 16:26:11 UTC (rev 17766)
@@ -354,7 +354,7 @@
XmlSolverConfigurer configurer = new XmlSolverConfigurer();
configurer.configure(ExaminationApp.SOLVER_CONFIG);
EvaluationHandler evaluationHandler = configurer.buildSolver().getEvaluationHandler();
-
+
List<Topic> topicList = examination.getTopicList();
List<Period> periodList = examination.getPeriodList();
List<Room> roomList = examination.getRoomList();
@@ -382,25 +382,40 @@
exam.setId(topic.getId());
exam.setTopic(topic);
examList.add(exam);
+ FactHandle examHandle = null;
- FactHandle examHandle = null;
+ List<PeriodScoring> periodScoringList = new ArrayList<PeriodScoring>(periodList.size());
+
+ for (Period period : periodList) {
+ exam.setPeriod(period);
+ if (examHandle == null) {
+ examHandle = workingMemory.insert(exam); // TODO move up
+ } else {
+ workingMemory.update(examHandle, exam);
+ }
+ double score = evaluationHandler.fireAllRulesAndCalculateStepScore();
+ periodScoringList.add(new PeriodScoring(period, score));
+ }
+ Collections.sort(periodScoringList);
+
boolean perfectMatch = false;
double bestScore = Double.NEGATIVE_INFINITY;
Period bestPeriod = null;
Room bestRoom = null;
- for (Period period : periodList) {
- exam.setPeriod(period);
+ for (PeriodScoring periodScoring : periodScoringList) {
+ if (bestScore >= periodScoring.getScore()) {
+ // No need to check the rest
+ break;
+ }
+ exam.setPeriod(periodScoring.getPeriod());
for (Room room : roomList) {
exam.setRoom(room);
- if (examHandle == null) {
- examHandle = workingMemory.insert(exam);
- }
workingMemory.update(examHandle, exam);
double score = evaluationHandler.fireAllRulesAndCalculateStepScore();
if (score < unscheduledScore) {
if (score > bestScore) {
bestScore = score;
- bestPeriod = period;
+ bestPeriod = periodScoring.getPeriod();
bestRoom = room;
}
} else if (score == unscheduledScore) {
@@ -428,45 +443,106 @@
Collections.sort(examList, new PersistableIdComparator());
}
- private void createExamListNoRuleEngine(Examination examination) {
+ private class PeriodScoring implements Comparable<PeriodScoring> {
+
+ private Period period;
+ private double score;
+
+ private PeriodScoring(Period period, double score) {
+ this.period = period;
+ this.score = score;
+ }
+
+ public Period getPeriod() {
+ return period;
+ }
+
+ public double getScore() {
+ return score;
+ }
+
+ public int compareTo(PeriodScoring other) {
+ return - new CompareToBuilder().append(score, other.score).toComparison();
+ }
+
+ }
+
+ private void createExamListSlow(Examination examination) {
+ XmlSolverConfigurer configurer = new XmlSolverConfigurer();
+ configurer.configure(ExaminationApp.SOLVER_CONFIG);
+ EvaluationHandler evaluationHandler = configurer.buildSolver().getEvaluationHandler();
+
List<Topic> topicList = examination.getTopicList();
List<Period> periodList = examination.getPeriodList();
List<Room> roomList = examination.getRoomList();
List<Exam> examList = new ArrayList<Exam>(topicList.size());
- for (Topic topic : topicList) {
- Exam exam = new Exam();
- exam.setId(topic.getId());
- exam.setTopic(topic);
- // Period and room assigned later
- examList.add(exam);
- }
examination.setExamList(examList);
- // Sort the order in which we 'll assign them
- List<Exam> sortedExamList = new ArrayList<Exam>(examList);
- Collections.sort(sortedExamList, new Comparator<Exam>() {
- public int compare(Exam a, Exam b) {
+ evaluationHandler.setSolution(examination);
+ evaluationHandler.resetStatefullSession();
+ StatefulSession workingMemory = evaluationHandler.getStatefulSession();
+
+ // Sort the order in which we 'll assign the topics into periods and rooms
+ List<Topic> sortedTopicList = new ArrayList<Topic>(topicList);
+ Collections.sort(sortedTopicList, new Comparator<Topic>() {
+ public int compare(Topic a, Topic b) {
return new CompareToBuilder()
- .append(a.getTopic().getStudentListSize(), b.getTopic().getStudentListSize())
+ .append(a.getStudentListSize(), b.getStudentListSize())
.toComparison();
}
});
- // Assign periods
- for (Exam exam : sortedExamList) {
- Period assignedPeriod = null;
- int assignedPeriodWorth = 0;
- for (Period period : periodList) {
+ for (Topic topic : sortedTopicList) {
+ logger.info("Scheduling topic ({}).", topic);
+ double unscheduledScore = evaluationHandler.fireAllRulesAndCalculateStepScore();
+ Exam exam = new Exam();
+ exam.setId(topic.getId());
+ exam.setTopic(topic);
+ examList.add(exam);
+ FactHandle examHandle = null;
+ boolean perfectMatch = false;
+ double bestScore = Double.NEGATIVE_INFINITY;
+ Period bestPeriod = null;
+ Room bestRoom = null;
+ for (Period period : periodList) {
+ exam.setPeriod(period);
+ for (Room room : roomList) {
+ exam.setRoom(room);
+ if (examHandle == null) {
+ examHandle = workingMemory.insert(exam);
+ }
+ workingMemory.update(examHandle, exam);
+ double score = evaluationHandler.fireAllRulesAndCalculateStepScore();
+ if (score < unscheduledScore) {
+ if (score > bestScore) {
+ bestScore = score;
+ bestPeriod = period;
+ bestRoom = room;
+ }
+ } else if (score == unscheduledScore) {
+ perfectMatch = true;
+ break;
+ } else {
+ throw new IllegalStateException("The score (" + score
+ + ") cannot be higher than unscheduledScore (" + unscheduledScore + ").");
+ }
+ }
+ if (perfectMatch) {
+ break;
+ }
}
- if (assignedPeriod == null) {
- // TODO randomize
+ if (!perfectMatch) {
+ if (bestPeriod == null || bestRoom == null) {
+ throw new IllegalStateException("The bestPeriod (" + bestPeriod + ") or the bestRoom ("
+ + bestRoom + ") cannot be null.");
+ }
+ exam.setPeriod(bestPeriod);
+ exam.setRoom(bestRoom);
+ workingMemory.update(examHandle, exam);
}
}
-
- // Assign rooms
-
- // TODO
+ Collections.sort(examList, new PersistableIdComparator());
}
}
More information about the jboss-svn-commits
mailing list