2016-01-04 66 views
0

我从事机器人装配线平衡问题的遗传算法(将装配操作和机器人分配给工作站以最小化给定数量的工作站的周期时间)。解决方案由一个ArrayList(configuration)表示,该序列保存分配给不同站的序列中的所有操作。此外,我还有两个ArrayLists(robotAssignment,operationPartition),它们指示新站启动的位置以及将哪个机器人分配给站。例如,解决方案候选看起来像这样(configurationrobotAssignmentoperationPartition从顶部至底部):根据对象的调用对象,我可以在对象的字段中存储不同的值吗?

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(); 

现在,如果我改变的operation1stationIndexconf1它也是在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); 
} 
+0

请提供完整的代码 – coolguy

+6

我不明白这个问题,但我有一种强烈的感觉,当你不应该使用静态变量时。这条线特别响了警钟:*我只能存储一次操作的'stationIndex'而不是每次配置*,这意味着'stationIndex'是一个静态变量,但应该是配置类的一个实例变量。 – Bohemian

+0

我想循环播放我的人群并获得某个操作的操作的站点索引。例如:'conf1.getOperaration(index).getStationIndex()' – Christoph

回答

0

要么站指数是操作对象与否的一部分。

如果站指数是操作对象的一部分,那么你就需要来表示多个Operation对象相同的操作,因为你想要的操作目的是在一个配置,以上的配置持有不同的测站索引另一个。你可以通过克隆你从OperationManager.getOperation()得到的对象来实现Configuration.generateIndividual()

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

相关问题