Optaplanner Planning Variable

Posted By : Mohit Jain | 16-Dec-2018

Planning variable:-Planning variable is one of the core concept in terms of optaplanner framework which needs to be correctly defined during domain modeling of planning problem which is NP-hard(https://en.wikipedia.org/wiki/NP-hardness). During domain modeling, we have to carefully select the planning variable for any Planning Entity so that optaplanner framework will optimize it to give the best possible solution. 

This is the variable which is placed inside the @PlanningEntity annotated class. This variable is annotated with a @PlanningVariable annotation which makes it different from other variables inside Planning Entity class.

 

This variable has a range of planning values which is attached with it as shown below in the example by the keyword valueRangeProviderRefs = {"rowRange"}

Syntax:- Example of Nqueens provided by optaplanner. (https://docs.optaplanner.org/7.15.0.Final/optaplanner-docs/html_single/index.html#nQueens)

See the given URL for more information:- (https://en.wikipedia.org/wiki/Eight_queens_puzzle).

@PlanningEntity
public class Queen {
    ...

    private Row row;

    @PlanningVariable(valueRangeProviderRefs = {"rowRange"})
    public Row getRow() {
        return row;
    }

    public void setRow(Row row) {
        this.row = row;
    }

}

 

Note:- Planning variable can point either PlanningFact or other Planning Entity or combination of both having common interface as Planning value.

 

Here, In the example above Row class is a fact which will not change during planning(means if R1 is instance of Row class then all properties/variables will remain same) and ( private Row row;) variable value is changed with the all possible planning values of Row class instances (example:-R1, R2, R3, R4, R5, R6,....etc) but R1, R2, R3, R4, R5, R6 will not be changed iteself.

 

Planning variable is of two types:-

1.) Genuine:- which is the variable needed to be changed inside Planning Entity class for the optimization of Planning Problem Solution.

 

2.) Shadow:-which is the variable not for the optimization purpose but, it is the helper(modifiable) variable of any genuine planning variable whose value is only changed if, its associated genuine planning variable gets changed.

Shadow variables will be explained more in upcoming blogs.But the key concern here is to understand that planning variable can be a normal Java POJO class which is fact in terms of optaplanner or a planning Entity containing another Planning variables(recommended "shadow" only if this is the case otherwise search space will be increased) which will make harden to solve the problem).

It is recommented that search space should be minimal so we need to carefully decide planning variable(possibly one for each planning problem and shadow if having another plnningEntity inside same Planning Problem)

 

About Author

Author Image
Mohit Jain

Mohit Jain working as backend java developer having knowlege of core java,spring,hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..