If we take a closer look at the above code, the important parts become more clear. We calculate a score in the loop because the List<Lecture> contains specific non-unique combinations of rooms and periods.

The HashSet is used to save a unique key (string) so that we can penalize duplicate lectures in the same room and period.

As a result, we receive unique sets of rooms and periods.

4.2. Drools

Drools files give us a quick way to alter rules for applying to files. While the syntax can sometimes be confusing the Drools file can be a way to manage logic outside of the compiled classes.

Lecture in Room 1 during Period 1
Lecture in Room 2 during Period 1
Lecture in Room 1 during Period 2
Lecture in Room 2 during Period 2
Lecture in Room 1 during Period 3
Lecture in Room 2 during Period 3
Lecture in Room 1 during Period 1
Lecture in Room 1 during Period 1
Lecture in Room 1 during Period 1
Lecture in Room 1 during Period 1

Notice how the last three entries are repeating. This happens because there is no optimal solution to our problem. We chose three periods, two classrooms and ten lectures.

There are only six possible lectures due to these fixed resources. At the very least this answer shows the user that there are not enough rooms or periods to contain all the lectures.

7. Extra Features

Our example for OptaPlanner we created was a simple one, however, the framework has added features for more diverse use cases. We may want to implement or alter our algorithm for optimization and then specify the framework to use it.

Due to recent improvements in Java's multi-threading capabilities, OptaPlanner also gives developers the ability to use multiple implementations of multi-threading such as fork and join, incremental solving and multitenancy.

8. Conclusion

The OptaPlanner framework provides developers with a powerful tool to solve constraint satisfaction problems such as scheduling and resource allocation.

OptaPlanner offers minimal JVM resource usage as well as integrating with Jakarta EE. The author continues to support the framework, and Red Hat has added it as part of its Business Rules Management Suite.

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Great article. Thanks for covering OptaPlanner.
Currently we haven’t written a optaplanner-spring-boot-starter yet. Do you think that would be useful? And more importantly, what do you think it should do?

We intend to build SolverManager, a component that manages several Solvers and their threads. I think that might be a good candidate to make it easy to inject – but I am looking for proposals of strawman examples on how people would want that to work.

I think that a Spring Boot starter could be nice. However, OptaPlanner integration, as it stands currently, is pretty simple. Some of the examples could be beneficial to include as Spring Boot projects.

Possibly examples that show custom heuristic algorithms or further customization using OptaPlanner.

I appreciate the additional information below as well. I included the Drools rule as a simple way to integrate Drools in the article without creating an overly complex use case.

Thanks again for the comments!

0

Geoffrey De Smet

1 year ago

For completeness: the drools rule is obsolete because OptaPlanner guarantees that none of the planning variables are null after solving, unless @PlanningVariable(nullabe=true).

It’s even slightly better not to have that constraint (very advanced explanation: the initializingScoreTrend becomes ANY instead of ONLY_DOWN, so the Construction Heuristics algorithms can’t take certain shortcuts any more) – but don’t worry too much about this last part.