我从事机器人装配线平衡问题的遗传算法(将装配操作和机器人分配给工作站以最小化给定数量的工作站的周期时间)。解决方案由一个ArrayList(configuration
)表示,该序列保存分配给不同站的序列中的所有操作。此外,我还有两个ArrayLists(robotAssignment
,operationPartition
),它们指示新站启动的位置以及将哪个机器人分配给站。例如,解决方案候选看起来像这样(configuration
,robotAssignment
,operationPartition
从顶部至底部):根据对象的调用对象,我可以在对象的字段中存储不同的值吗?
Initial cycle time: 50.0
|2|7|3|9|1|5|4|6|8|10|
|2|1|3|2|
|0|2|5|7|
从这种表示我们知道,操作3,图9和1被分配给站2和机器人1是自operationPartition
开始使用configuration
的新站的起始索引。
随着ArrayList中operationPartition
的帮助下,我总能找出一个操作站。但是,我宁愿将电台索引存储在类别Operation
本身的对象中。我用类变量stationIndex
创建了一个类Operation
。所有操作都被添加到另一个类OperationManager
,该类保存ArrayList中的所有操作。我的课程Configuration
用于创建新配置并包含OperationManager
的所有操作。
我的问题是,我可以只存储一次操作的stationIndex
而不是每个配置(至少我不知道如何做到这一点)。给你举个例子,假设我们有两种配置:
Configuration conf1 = new Configuration();
Configuration conf2 = new Configuration();
conf1.generateIndividual();
conf2.generateIndividual();
现在,如果我改变的operation1
的stationIndex
在conf1
它也是在conf2
改变,因为这个变量“属于”操作和不依赖于组态。
有什么办法来存储在stationIndex
不同的值取决于配置?如果这是可能的话,这将是我的首选解决方案。
public class Operation {
int[] predecessors;
int stationIndex;
// Construct an operation with given predecessors
public Operation(int[] predecessors){
this.predecessors = predecessors;
}
// Get operations's predecessors
public int[] getPredecessors() {
return this.predecessors;
}
// Set operations's station index
public void setStationIndex(int stationIndex){
this.stationIndex = stationIndex;
}
// Get operations's station index
public int getStationIndex(){
return this.stationIndex;
}
类OperationManager:
public class OperationManager {
// Holds our operations
private static ArrayList operations = new ArrayList<Operation>();
// Adds an operation
public static void addOperation(Operation operation) {
operations.add(operation);
}
// Get an operation
public static Operation getOperation (int index){
return (Operation)operations.get(index);
}
// Get index of an operation
public static int getOperationIndex(Operation operation) {
return operations.indexOf(operation);
}
// Get the number of operations
public static int numberOfOperations() {
return operations.size();
}
类别配置(未完成,但与所有相关部分):
public class Configuration {
int initialCycleTime = RobotManager.calcLowerBound();
// Holds our array of operations
private ArrayList configuration = new ArrayList<Operation>();
private ArrayList robotAssignment = new ArrayList<Robot>();
private ArrayList operationPartition = new ArrayList<Integer>();
// Cache
private int fitness = 0;
// Constructs a blank configuration
public Configuration() {
for (int i = 0; i < OperationManager.numberOfOperations(); i++) {
configuration.add(null);
}
for (int i = 0; i < GA_RALBP.numberOfStations; i++) {
operationPartition.add(null);
}
for (int i = 0; i < GA_RALBP.numberOfStations; i++) {
robotAssignment.add(null);
}
}
// Creates a random individual
public void generateIndividual() {
// Loop over all operations and add them to our configuration
for (int operationIndex = 0; operationIndex < OperationManager.numberOfOperations(); operationIndex++) {
setOperation(operationIndex, OperationManager.getOperation(operationIndex));
}
// Randomly shuffle the configuration
Collections.shuffle(configuration);
}
请提供完整的代码 – coolguy
我不明白这个问题,但我有一种强烈的感觉,当你不应该使用静态变量时。这条线特别响了警钟:*我只能存储一次操作的'stationIndex'而不是每次配置*,这意味着'stationIndex'是一个静态变量,但应该是配置类的一个实例变量。 – Bohemian
我想循环播放我的人群并获得某个操作的操作的站点索引。例如:'conf1.getOperaration(index).getStationIndex()' – Christoph