alexandershi000 2023-08-07 09:59 采纳率: 0%
浏览 31
已结题

OptaPlanner ShadowVariable

初学OptaPlanner,在测试@ShadowVariable注解时,总是报错:
The entityClass (class com.example.optatest.timeTabling.entity.Room) has an @InverseRelationShadowVariable annotated property (lessons) with a sourceClass (class com.example.optatest.timeTabling.entity.Lesson) which is not a valid planning entity.

仅在案例TimeTable的基础上,为Room类增加了一个阴影变量lessons,其余不变。

烦请各位帮忙看看如何解决。不胜感激。或者,烦请指示,在TimeTable案例的基础上,如何在Room类里增加一个lessonCount阴影变量,以便其可以跟随Lesson类里room变量变化而变化。

  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;

    // No-arg constructor required for OptaPlanner
    public Lesson() {
    }

    public Lesson(long id, String subject, String teacher, String studentGroup) {
        this.id = id;
        this.subject = subject;
        this.teacher = teacher;
        this.studentGroup = studentGroup;
    }

    public Lesson(long id, String subject, String teacher, String studentGroup, Timeslot timeslot, Room room) {
        this(id, subject, teacher, studentGroup);
        this.timeslot = timeslot;
        this.room = room;
    }

    @Override
    public String toString() {
        return subject + "(" + id + ")";
    }

    // ************************************************************************
    // Getters and setters
    // ************************************************************************

    public Long getId() {
        return id;
    }

    public String getSubject() {
        return subject;
    }

    public String getTeacher() {
        return teacher;
    }

    public String getStudentGroup() {
        return studentGroup;
    }

    public Timeslot getTimeslot() {
        return timeslot;
    }

    public void setTimeslot(Timeslot timeslot) {
        this.timeslot = timeslot;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

}
  1. Room.java
@Slf4j
@PlanningEntity
public class Room {

    @PlanningId
    private Long id;

    private String name;

    // *********** 测试01 *************************

    // @InverseRelationShadowVariable(sourceVariableName = "room")
    // public List<Lesson> lessons = new ArrayList<>();
    //
    // public Integer getLessonCount() {
    //     log.info("size: {}", lessons.size());
    //     return this.lessons.size();
    // }

    // *********** 测试02 *************************

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

    public Integer getLessonCount() {
        return lessonCount;
    }

    public void setLessonCount(Integer lessonCount) {
        this.lessonCount = lessonCount;
    }

    public Room() {
    }

    public Room(String name) {
        this.name = name;
    }

    public Room(long id, String name) {
        this(name);
        this.id = id;
    }

    @Override
    public String toString() {
        return name;
    }

    // ************************************************************************
    // Getters and setters
    // ************************************************************************

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
  1. TimeTable.java
@PlanningSolution
public class TimeTable {

    @ProblemFactCollectionProperty
    @ValueRangeProvider
    private List<Timeslot> timeslotList;
    @ProblemFactCollectionProperty
    @ValueRangeProvider
    private List<Room> roomList;
    @PlanningEntityCollectionProperty
    private List<Lesson> lessonList;

    @PlanningScore
    private HardSoftScore score;

    // No-arg constructor required for OptaPlanner
    public TimeTable() {
    }

    public TimeTable(List<Timeslot> timeslotList, List<Room> roomList, List<Lesson> lessonList) {
        this.timeslotList = timeslotList;
        this.roomList = roomList;
        this.lessonList = lessonList;
    }

    // ************************************************************************
    // Getters and setters
    // ************************************************************************

    public List<Timeslot> getTimeslotList() {
        return timeslotList;
    }

    public List<Room> getRoomList() {
        return roomList;
    }

    public List<Lesson> getLessonList() {
        return lessonList;
    }

    public HardSoftScore getScore() {
        return score;
    }

}

  1. LessonCountUpdatingVariableListener.java
@Slf4j
public class LessonCountUpdatingVariableListener implements VariableListener<TimeTable, Lesson> {

    @Override
    public void beforeVariableChanged(ScoreDirector<TimeTable> scoreDirector, Lesson lesson) {
        log.info("==>beforeVariableChanged");
        Room room = lesson.getRoom();
        if (room != null) {
            scoreDirector.beforeVariableChanged(room, "lessonCount");
            room.setLessonCount(room.getLessonCount() - 1);
            scoreDirector.afterVariableChanged(room, "lessonCount");
        }
    }

    @Override
    public void afterVariableChanged(ScoreDirector<TimeTable> scoreDirector, Lesson lesson) {
        log.info("==>afterVariableChanged");
        Room room = lesson.getRoom();
        if (room != null) {
            scoreDirector.beforeVariableChanged(room, "lessonCount");
            room.setLessonCount(room.getLessonCount() + 1);
            scoreDirector.afterVariableChanged(room, "lessonCount");
        }
    }

    @Override
    public void beforeEntityAdded(ScoreDirector<TimeTable> scoreDirector, Lesson lesson) {
        log.info("==>beforeEntityAdded");
        Room room = lesson.getRoom();
        if (room != null) {
            scoreDirector.beforeVariableChanged(room, "lessonCount");
            room.setLessonCount(room.getLessonCount() + 1);
            scoreDirector.afterVariableChanged(room, "lessonCount");
        }
    }

    @Override
    public void afterEntityAdded(ScoreDirector<TimeTable> scoreDirector, Lesson lesson) {
        log.info("==>afterEntityAdded");
    }

    @Override
    public void beforeEntityRemoved(ScoreDirector<TimeTable> scoreDirector, Lesson lesson) {
        log.info("==>beforeEntityRemoved");
        Room room = lesson.getRoom();
        if (room != null) {
            scoreDirector.beforeVariableChanged(room, "lessonCount");
            room.setLessonCount(room.getLessonCount() - 1);
            scoreDirector.afterVariableChanged(room, "lessonCount");
        }
    }

    @Override
    public void afterEntityRemoved(ScoreDirector<TimeTable> scoreDirector, Lesson lesson) {
        log.info("==>afterEntityRemoved");
    }
}
  1. Test
        SolverFactory<TimeTable> solverFactory = SolverFactory.create(new SolverConfig()
                .withSolutionClass(TimeTable.class)
                .withEntityClasses(Lesson.class)
                .withEntityClasses(Room.class)
                .withConstraintProviderClass(TimeTableConstraintProvider.class)
                // The solver runs only for 5 seconds on this small dataset.
                // It's recommended to run for at least 5 minutes ("5m") otherwise.
                .withTerminationSpentLimit(Duration.ofSeconds(1)));

        // Load the problem
        TimeTable problem = generateDemoData();

        // Solve the problem
        Solver<TimeTable> solver = solverFactory.buildSolver();
        TimeTable solution = solver.solve(problem);
  1. 报错:
2023-08-07 09:54:48,331 ERROR [http-nio-8090-exec-5] o.a.c.c.C.[.[.[.[dispatcherServlet].log(175): Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: The entityClass (class com.example.optatest.timeTabling.entity.Room) has an @InverseRelationShadowVariable annotated property (lessonList) with a sourceClass (class com.example.optatest.timeTabling.entity.Lesson) which is not a valid planning entity.
Maybe check the annotations of the class (class com.example.optatest.timeTabling.entity.Lesson).
Maybe add the class (class com.example.optatest.timeTabling.entity.Lesson) among planning entities in the solver configuration.] with root cause
java.lang.IllegalArgumentException: The entityClass (class com.example.optatest.timeTabling.entity.Room) has an @InverseRelationShadowVariable annotated property (lessonList) with a sourceClass (class com.example.optatest.timeTabling.entity.Lesson) which is not a valid planning entity.
Maybe check the annotations of the class (class com.example.optatest.timeTabling.entity.Lesson).
Maybe add the class (class com.example.optatest.timeTabling.entity.Lesson) among planning entities in the solver configuration.
    at org.optaplanner.core.impl.domain.variable.inverserelation.InverseRelationShadowVariableDescriptor.linkShadowSources(InverseRelationShadowVariableDescriptor.java:77) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.impl.domain.variable.inverserelation.InverseRelationShadowVariableDescriptor.linkVariableDescriptors(InverseRelationShadowVariableDescriptor.java:47) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.impl.domain.entity.descriptor.EntityDescriptor.linkVariableDescriptors(EntityDescriptor.java:413) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor.afterAnnotationsProcessed(SolutionDescriptor.java:482) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor.buildSolutionDescriptor(SolutionDescriptor.java:108) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.impl.solver.DefaultSolverFactory.buildSolutionDescriptor(DefaultSolverFactory.java:147) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.impl.solver.DefaultSolverFactory.<init>(DefaultSolverFactory.java:69) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at org.optaplanner.core.api.solver.SolverFactory.create(SolverFactory.java:106) ~[optaplanner-core-impl-9.41.0.Final.jar:9.41.0.Final]
    at com.example.optatest.controller.Test04Controller.test04(Test04Controller.java:27) ~[classes/:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.16.jar:5.3.16]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.58.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.16.jar:5.3.16]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.58.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.16.jar:5.3.16]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.16.jar:5.3.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.16.jar:5.3.16]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.16.jar:5.3.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.16.jar:5.3.16]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.16.jar:5.3.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.58.jar:9.0.58]
    at java.lang.Thread.run(Thread.java:829) [?:?]

  • 写回答

8条回答 默认 最新

  • CSDN专家-sinJack 2023-08-08 14:37
    关注

    写法上可能存在问题
    Lesson 包含了Room 属性,Room 中又包含List,循环依赖了吧

    @PlanningEntity
    public class Lesson {
       
        @PlanningVariable
        private Room room;
    }
    

    你给的视频截图好像不是这么应用的。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月8日
  • 赞助了问题酬金50元 8月8日
  • 赞助了问题酬金15元 8月7日
  • 修改了问题 8月7日
  • 展开全部

悬赏问题

  • ¥30 使用matlab将观测点聚合成多条目标轨迹
  • ¥15 Workbench中材料库无法更新,如何解决?
  • ¥20 如何推断此服务器配置
  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析