1

So I've created a custom score in drools:

public interface MyScore extends Score<MyScore>

and have implemented it. However I can't see how to use the score. The config has a

<scoreDefinition>

tag but putting anything inside this other than SIMPLE or HARD_AND_SOFT produces an error.

How can I configure the solver to use the score I've created, the docs seem to imply this is possible but doesn't go into any detail.

Jim
  • 22,354
  • 6
  • 52
  • 80

2 Answers2

3

This was meant to be possible (and a normal practice), but there's a roadblock.

I just added this documentation:

Implementing a custom Score

To implement a custom Score, you 'll also need to implement a custom ScoreDefinition. Extend AbstractScoreDefinition (preferable by copy pasting HardAndSoftScoreDefinition or SimpleScoreDefinition) and start from there.

Next, hook you custom ScoreDefinition in your SolverConfig.xml:

<scoreDefinition>
    <scoreDefinitionClass>org.drools.planner.examples.my.score.definition.MyScoreDefinition</scoreDefinitionClass>
</scoreDefinition>

The Roadblock

There's a roadblock that I 'll fix for 5.3 or 5.4:

ScoreDefinitionConfig has this code:

/**
 * @TODO score-in-solution refactor
 */
public ScoreCalculator buildScoreCalculator() {
    if (scoreDefinitionType != null) {
        switch (scoreDefinitionType) {
            case SIMPLE:
                return new SimpleScoreCalculator();
            case SIMPLE_DOUBLE:
                return new SimpleDoubleScoreCalculator();
            case HARD_AND_SOFT:
                return new DefaultHardAndSoftConstraintScoreCalculator();
            default:
                throw new IllegalStateException("The scoreDefinitionType (" + scoreDefinitionType
                        + ") is not implemented");
        }
    } else {
        return new SimpleScoreCalculator();
    }
}

One way to deal with that is to extend ScoreDefinitionConfig and overwrite that method, as described in the manual in the section using a custom Selector, Acceptor or Forager.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
1

For what I could see, there could be no support for custom scores, which is a shame... I checked the ScoreDefinitionConfig class and I saw this:

                   switch (scoreDefinitionType) {
                case SIMPLE:
                    return new SimpleScoreDefinition();
                case HARD_AND_SOFT:
                    return new HardAndSoftScoreDefinition();
                default:
                    throw new IllegalStateException("scoreDefinitionType ("
                            + scoreDefinitionType + ") not implemented");

So, anything other than SIMPLE and HARD_AND_SOFT doesn't cut it...

Any insights on this?

KR, Luis

Luis
  • 11
  • 1
  • ScoreDefinitionConfig has an alternative for `scoreDefinitionType`, which is `scoreDefinitionClass`, which is meant for using custom scores. See other answer. – Geoffrey De Smet Jul 29 '11 at 12:14