How to correctly set ShadowVariable to PlanningEntity ?

32 views
Skip to first unread message

Shi Alexander

unread,
Aug 7, 2023, 11:10:11 PM8/7/23
to OptaPlanner development
Hi,

I am learning OptaPlanner, but here is one problem confused me. I modified the example of TimeTable from OptaPlanner website, and added @PlanningEntity to Room.java, also added a field private Integer lessonCount with @ShadowVariable annoated. But it throws exception and says: The entityClass (class com.example.optatest.timeTabling.entity.Room) has a @ShadowVariable annotated property (lessonCount) with a sourceEntityClass (class com.example.optatest.timeTabling.entity.Lesson) which is not a valid planning entity.

Below are codes.

1, Lesson.java
@PlanningEntity
public class Lesson {

    @PlanningId
    private Long id;

    private String subject;
    private String teacher;
    private String studentGroup;

    @PlanningVariable
    private Timeslot timeslot;
    @PlanningVariable
    private Room room;

// construction
// getter and setter
}

2, Room.java
@PlanningEntity
public class Room {

    @PlanningId
    private Long id;

    private String name;

    @ShadowVariable(variableListenerClass = LessonCountUpdatingVariableListener.class, sourceEntityClass = Lesson.class, sourceVariableName = "room")
    private Integer lessonCount = 0;

// constructions
// getter and setter
}

Please support: what should i do that i can make the field lessonCount be changed following the field room of Lesson.java.

thanks.

Anna Dupliak

unread,
Aug 8, 2023, 7:01:39 AM8/8/23
to OptaPlanner development
Hello,

Thank you for interesting question.

As I understand you want to track amount of lessons assigned to a particular rooms during the solution. Your shadow variable looks fine to me and you added @PlanningEntity for OptaPlanner to discover it, though the solver is complaining about invalid Planning Entity.

Could you please check Solver configuration in your main method? You need to add both Room.class and Lesson.class into .withEntityClasses(Lesson.class, Room.class).

Try this solution: https://github.com/kiegroup/optaplanner-quickstarts/commit/a7aeee74f6a5e6874f1dc28423e15871eff2323a

Think of a Shadow Variable as a secret helper that you can only put inside a class with Planning Entity annotation.
To make sure the magic works, you also need to tell the grand Solver about this special class so it can work its optimization properly.


Shi Alexander

unread,
Aug 8, 2023, 10:33:51 AM8/8/23
to OptaPlanner development
Hi, Anna,

Thank you very much, I solved my problem. thank you.

Anna Dupliak

unread,
Aug 8, 2023, 10:39:23 AM8/8/23
to optapla...@googlegroups.com
Hi Shi,

It's great! 
Happy to help!

--
You received this message because you are subscribed to a topic in the Google Groups "OptaPlanner development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/optaplanner-dev/Quc5o5RW5sk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to optaplanner-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/optaplanner-dev/b015b6a5-8e15-4e4a-9d38-0240a90c01f1n%40googlegroups.com.


--

Dupliak Anna

She /Her /Hers

QE, Business Automation

Red Hat

adup...@redhat.com    

 +42-0773-016991




Reply all
Reply to author
Forward
0 new messages