2016-01-04 65 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列表中的位置來表示站點索引。

相關問題