OptaPlanner logo
  • Download
  • Learn
    • Documentation
    • Videos

    • Use cases
    • Compatibility
    • Testimonials and case studies
  • Get help
  • Blog
  • Source
  • Team
  • Services
  • Star
  • T
  • L
  • F
  • YT
Fork me on GitHub

Put the user in control of the score constraints

Thu 17 April 2014
Avatar Geoffrey De Smet
Geoffrey De Smet

Twitter LinkedIn GitHub

OptaPlanner creator

OptaPlanner optimizes towards the plan with the best score. But who defines the score function? Normally the developers do, based on the specs of the users. Can we empower users to define the score function themselves? Let’s take a look how.

If we’re using the Drools rule engine for the score calculation in OptaPlanner, then we can use any of the supported features in the Drools ecosystem to manipulate those score rules. That includes Decision Tables, Scorecards and the Drools Workbench web application.

I’ll empower the users on the Cloud Balancing example, for which we need to assign processes to computers, without going above the capacity of any computer. For more information about this example, see this video.

Static DRL rules

As a developer, we can write the score rules statically in the DRL file:

rule "Risk for out of memory"
    when
        CloudComputerUsage(freeMemory < 2)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -100);
end
rule "Unused memory but no CPU"
    when
        CloudComputerUsage(freeMemory >= 8, freeCpuPower < 4)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -10);
end

However, DRL files are too difficult for a user to edit directly.

Data driven DRL rules

One way to empower the user, is to create a data object for him/her to fill in:

public class FreePenalty {

    private int minFreeMemory; // Defaults to 0
    private int maxFreeMemory; // Defaults to Integer.MAX_VALUE
    private int minFreeCpuPower; // Defaults to 0
    private int maxFreeCpuPower; // Defaults to Integer.MAX_VALUE
    private int minFreeNetworkBandwidth; // Defaults to 0
    private int maxFreeNetworkBandwidth; // Defaults to Integer.MAX_VALUE

    private int softScoreImpact;

    ...
}

We write 1 rule to apply those FreePenalty instances as score constraints:

rule "requiredCpuPowerTotal"
    when
        // Defined by the user
        FreePenalty(
                $minFreeMemory : minFreeMemory, $maxFreeMemory : maxFreeMemory,
                $minFreeCpuPower : minFreeCpuPower, $maxFreeCpuPower : maxFreeCpuPower,
                $minFreeNetworkBandwidth : minFreeNetworkBandwidth, $maxFreeNetworkBandwidth : maxFreeNetworkBandwidth)
        // Calculated insertlogical from the process assignments
        CloudComputerUsage(
                freeMemory >= $minFreeMemory, freeMemory < $maxFreeMemory,
                freeCpuPower >= $minFreeCpuPower, freeCpuPower < $maxFreeCpuPower,
                freeMemory >= $minFreeNetworkBandwidth, freeMemory < $maxFreeNetworkBandwidth,
                $softScoreImpact : softScoreImpact)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, $softScoreImpact);
end

And of course, we build a web interface so the user can create/update/delete FreePenalty records. So the user can use that UI to create these records:

User creates new FreePenalty(maxFreeMemory = 2, softScoreImpact = -100)
User creates new FreePenalty(minFreeMemory = 8, maxFreeCpuPower = 4, softScoreImpact = -10)

These 2 records result in the same score function as the statically defines score function which we saw earlier, but now the user can change them.

An added advantage of this approach is that the user’s changes are part of the dataset. Multiple datasets, for the exact same Solver configuration, use separate FreePenalty instances. This is especially useful in a multi-tenancy application.

Decision tables

Another way to expose the constraints to the user, is through a Drools decision table. The user edits an xls file with Excel or LibreOffice to change the score function:

decisionTable

Before sending this file to the users, we hide row 2 to 11, so the user isn’t exposed to the technical details.

See this video for a detailed demonstration:

Note that a decision table is more powerful than a data driven DRL rule: the user decides which conditions to apply. The other conditions are not even included (which is faster than checking them against default values).

Rule templates

If data driven DRL rules aren’t powerful enough for you, but a decision table's xls file sticks out like sore thumb in your UI, use Drools rule templates with a custom UI.

Drools Workbench

Workbench (formerly known as Guvnor) is a full-blown web application to manage rules and workflows. Using a web interface, business users can create and edit rules, including the score rules used by OptaPlanner.

This is a very interesting setup, which I’ll demonstrate in a future blog post. Meanwhile, for more information, see the Drools documentation.

Conclusion

In OptaPlanner, there are several ways to enable your users to define the score function of their planning problem themselves.

This means that your users can fine tune the score function at runtime, by looking at the result of the Solver, changing the score function and solving it again. Those faster iterations, enables them to evolve the business planning quicker.


Permalink
 tagged as howto feature

Comments

Visit our forum to comment
AtomNews feed
Don’t want to miss a single blog post?
Follow us on
  • T
  • L
  • F
Blog archive
Latest release
  • 8.35.0.Final released
    Fri 3 March 2023
Upcoming events
    Add event / Archive
Latest blog posts
  • OptaPlanner 9 is coming
    Tue 21 February 2023
    Lukáš Petrovický
  • Farewell - a new lead
    Tue 15 November 2022
    Geoffrey De Smet
  • Run OptaPlanner workloads on OpenShift, part II
    Wed 9 November 2022
    Radovan Synek
  • Bavet - A faster score engine for OptaPlanner
    Tue 6 September 2022
    Geoffrey De Smet
  • Run OptaPlanner workloads on OpenShift, part I.
    Thu 9 June 2022
    Radovan Synek
  • OptaPlanner deprecates score DRL
    Thu 26 May 2022
    Lukáš Petrovický
  • Real-time planning meets SolverManager
    Mon 7 March 2022
    Radovan Synek
  • Blog archive
Latest videos
  • The Vehicle Routing Problem
    Fri 23 September 2022
    Geoffrey De Smet
  • Introduction to OptaPlanner AI constraint solver
    Thu 25 August 2022
    Anna Dupliak
  • On schedule: Artificial Intelligence plans that meet expectations
    Sat 23 July 2022
    Geoffrey De Smet
  • Host your OptaPlanner app on OpenShift (Kubernetes)
    Mon 7 February 2022
    Geoffrey De Smet
  • OptaPlanner - A fast, easy-to-use, open source AI constraint solver for software developers
    Mon 31 January 2022
  • Order picking planning with OptaPlanner
    Fri 31 December 2021
    Anna Dupliak
  • AI lesson scheduling on Quarkus with OptaPlanner
    Thu 18 November 2021
    Geoffrey De Smet
  • Video archive

OptaPlanner is open. All dependencies of this project are available under the Apache Software License 2.0 or a compatible license. OptaPlanner is trademarked.

This website was built with JBake and is open source.

Community

  • Blog
  • Get Help
  • Team
  • Governance
  • Academic research

Code

  • Build from source
  • Issue tracker
  • Release notes
  • Upgrade recipes
  • Logo and branding
CC by 3.0 | Privacy Policy
Sponsored by Red Hat