Java 我可以在对象中存储不同的值吗';s字段取决于调用它的对象?

Java 我可以在对象中存储不同的值吗';s字段取决于调用它的对象?,java,genetic-algorithm,Java,Genetic Algorithm,我研究了机器人装配线平衡问题的遗传算法(将装配操作和机器人分配到工位,以使给定工位数的周期时间最小化)。解决方案由一个ArrayList(配置)表示,它保存分配给不同站点的序列中的所有操作。此外,我还有两个ArrayList(robotAssignment,operationPartition),它们指示新站点的启动位置以及将哪个机器人分配给站点。例如,候选解决方案如下所示(配置,机器人分配,操作分区自上而下): 从这个表示中,我们知道操作3、9和1被分配给站点2,而机器人1被使用,因为操作分区

我研究了机器人装配线平衡问题的遗传算法(将装配操作和机器人分配到工位,以使给定工位数的周期时间最小化)。解决方案由一个ArrayList(
配置
)表示,它保存分配给不同站点的序列中的所有操作。此外,我还有两个ArrayList(
robotAssignment
operationPartition
),它们指示新站点的启动位置以及将哪个机器人分配给站点。例如,候选解决方案如下所示(
配置
机器人分配
操作分区
自上而下):

从这个表示中,我们知道操作3、9和1被分配给站点2,而机器人1被使用,因为
操作分区
给出了
配置中新站点的起始索引

在ArrayList
operationPartition
的帮助下,我总能找出操作的位置。但是,我更愿意将站点索引存储在类
操作
本身的对象中。我用一个类变量
stationIndex
创建了一个类
操作。所有操作都添加到另一个类
OperationManager
,该类在ArrayList中保存所有操作。我的类
配置
用于创建新配置,并包括
操作管理器
中的所有操作

我的问题是,我只能存储一次操作的
stationIndex
,而不是每个配置(至少我不知道如何做到这一点)。举个例子,假设我们有两种配置:

    Configuration conf1 = new Configuration();
    Configuration conf2 = new Configuration();
    conf1.generateIndividual();
    conf2.generateIndividual();
现在,如果我在
conf1
中更改
operation1
stationIndex
,它也会在
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;
}
类操作管理器:

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();
}
公共类操作管理器{
//控制我们的行动
私有静态ArrayList操作=新建ArrayList();
//添加一个操作
公共静态无效添加操作(操作){
操作。添加(操作);
}
//动手术
公共静态操作getOperation(int索引){
返回(操作)操作。获取(索引);
}
//获取操作的索引
公共静态int getOperationIndex(操作){
返回操作。indexOf(操作);
}
//获取操作数
公共静态int numberOfOperations(){
返回操作;
}
类配置(不完整,但包含所有相关部分):

公共类配置{
int initialCycleTime=RobotManager.calcLowerBound();
//控制着我们的一系列行动
私有ArrayList配置=新建ArrayList();
private ArrayList robotAssignment=new ArrayList();
私有ArrayList operationPartition=新ArrayList();
//缓存
私人整数适合度=0;
//构造一个空白配置
公共配置(){
对于(int i=0;i
站点索引是否为操作对象的一部分

如果站点索引是操作对象的一部分,那么您需要用多个
操作
对象来表示同一操作,因为您希望一个配置中的操作对象在一个配置中持有不同于另一个配置的站点索引。您可以在
配置中实现这一点。generateIndividual()
通过克隆从
OperationManager.getOperation()
返回的对象


或者,您可以从操作对象中删除站点索引。例如,您可以通过
配置中操作在列表中的位置来表示站点索引

请提供完整的代码我不理解这个问题,但我强烈感觉您使用的是静态变量,而不应该。这行特别响了警钟:我只能存储一次操作的
stationIndex
,而不是每个配置,这表明
stationIndex
是一个静态变量,但应该是配置类的一个实例变量例如:
conf1.getOperation(index.getStationIndex()
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);
}