gpt4 book ai didi

How can I define PlanningVariable dynamicly, such as a map?(如何动态定义PlanningVariable,例如地图?)

转载 作者:bug小助手 更新时间:2023-10-22 16:11:34 31 4
gpt4 key购买 nike



I have a problem with OptaPlanner. I need to design a PlanningEntity with dynamic PlanningVariables. So that I can reuse the PlanningEntity in different projects.
Maybe like this :

我对OptaPlanner有问题。我需要设计一个带有动态规划变量的规划实体。这样我就可以在不同的项目中重用PlanningEntity。也许是这样的:



import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.variable.PlanningVariable;

import java.util.Map;

@PlanningEntity
public class Lesson {

private Long id;
/**
* key: room value: Room Object
* key: timeslot value: TimeSlot Object
*/
@PlanningVariable(valueRangeProviderRefs = "planningVariableMapRange")
private Map<String, Object> planningVariableMap;


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

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

public Long getId() {
return id;
}


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

public Map<String, Object> getPlanningVariableMap() {
return planningVariableMap;
}

public void setPlanningVariableMap(Map<String, Object> planningVariableMap) {
this.planningVariableMap = planningVariableMap;
}
}

How could I achive that ?

我怎么能做到呢?


I've also write a PlanningSolution like below.

我还写了一个计划解决方案,如下所示。



import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.ProblemFactProperty;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;

import java.util.List;
import java.util.Map;

@PlanningSolution
public class TimeTable {

@ValueRangeProvider(id = "planningVariableMapRange")
@ProblemFactProperty
private Map<String, List<Object>> valueProvider;

@PlanningEntityCollectionProperty
private List<Lesson> lessonList;

@PlanningScore
private HardSoftScore score;


public HardSoftScore getScore() {
return score;
}


public void setScore(HardSoftScore score) {
this.score = score;
}


public Map<String, List<Object>> getValueProvider() {
return valueProvider;
}

public void setValueProvider(Map<String, List<Object>> valueProvider) {
this.valueProvider = valueProvider;
}
}

Then I don't know what to do. Apparently, it won't work.

那我就不知道该怎么办了。显然,这行不通。


更多回答
优秀答案推荐

Indeed, it won't work. OptaPlanner (nor Timefold for that matter) is not designed to work like that. You will have to figure out a way of sharing logic between entities in a way that, in the end, each entity only has the variables it needs and they are each annotated separately.

事实上,这是行不通的。OptaPlanner(也不是Timefold)并不是这样设计的。您必须找到一种在实体之间共享逻辑的方法,最终,每个实体都只有它需要的变量,并且每个变量都被单独注释。


Inheritance is probably the best way to go. You can have all the methods on the supertype, and then annotate the getters on the concrete subtype for the variables you need in that particular problem. This will ensure most of your code is not duplicated, but some boilerplate will remain. (Such as the concrete solution and entity subtypes.) I'd argue this is a small cost to pay.

继承可能是最好的方法。您可以在超类型上拥有所有方法,然后在特定问题中所需变量的具体子类型上注释getter。这将确保大部分代码不会重复,但会保留一些样板文件。(比如具体的解决方案和实体子类型。)我认为这是一个很小的成本。


更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com