Search code examples
javaconstraint-programmingoptaplanner

Optaplanner : Planning entity and Planning variable at the same time?


Using Optaplanner, is it possible to define a class to be a planning entity and planning variable at the same time ?

Example :

  @PlanningEntity(difficultyComparatorClass = NodeDifficultyComparator.class)
    public class Node extends ProcessChain {
       // Planning variables: changes during planning, between score calculations.
        private List<Node>  parents;
        private List<Node>  childs;


        @PlanningVariable(valueRangeProviderRefs = {"nodeRange"})
        public List<Node> getParents() {
            return parents;
        }

        @PlanningVariable(valueRangeProviderRefs = {"nodeRange"})
        public List<Node> getChilds() {
            return childs;
        }

        public void setParents(List<Node> parents) {
            this.parents = parents;
        }

        public void setChilds(List<Node> childs) {
            this.childs = childs;
        }
    }

Solution

  • The TSP and Vehicle Routing example already have planning values that are planning entities too, although they both use a chained=true variable. Because chained=true, it implies that no 2 planning entities have the same planning variable (and all are directly or indirectly connected to an anchor). See docs section "chained planning variables".

    I suspect in your case, that you don't want chains, but instead a tree, a directed graph or an undirected graph. An undirected graph (= a graph which allows cycles) is problematic for Construction Heuristics, because out-of-the-box they can't construct a graph with a cycle. As for a tree and a directed graph: it should work in theory, but you might need custom moves (see docs) to have the moves be efficient. In the future, we'd like to support a tree structure similarly to how we support chains (for job shop scheduling).

    In any case: you can't have a @PlanningVariable on a List of planning entities currently. You 'll need to turn many2many relationships into many2one and one2many relationships.