使用JMetal解決分配優(yōu)化問題
遺傳算法是一種基于自然選擇和遺傳學(xué)原理的優(yōu)化算法,也很適合解決任務(wù)分配問題, 比如達(dá)到任務(wù)總耗時(shí)最短, 比如再兼顧每個(gè)工人工作量相對均衡.
下面代碼中 TaskAssignmentProblem(單目標(biāo)優(yōu)化) 和 BalancedTaskAssignmentProblem(多目標(biāo)優(yōu)化) .
package com.example;
import java.util.ArrayList;
import java.util.List;
import org.uma.jmetal.algorithm.Algorithm;
import org.uma.jmetal.algorithm.examples.AlgorithmRunner;
import org.uma.jmetal.algorithm.multiobjective.nsgaii.NSGAIIBuilder;
import org.uma.jmetal.operator.crossover.impl.IntegerSBXCrossover;
import org.uma.jmetal.operator.mutation.impl.IntegerPolynomialMutation;
import org.uma.jmetal.operator.selection.SelectionOperator;
import org.uma.jmetal.operator.selection.impl.BinaryTournamentSelection;
import org.uma.jmetal.problem.Problem;
import org.uma.jmetal.problem.integerproblem.impl.AbstractIntegerProblem;
import org.uma.jmetal.solution.integersolution.IntegerSolution;
import org.uma.jmetal.util.AbstractAlgorithmRunner;
import org.uma.jmetal.util.JMetalLogger;
import org.uma.jmetal.util.fileoutput.SolutionListOutput;
import org.uma.jmetal.util.fileoutput.impl.DefaultFileOutputContext;
public class App {
public static void main(String[] args) {
// 1. 定義問題
Problem<IntegerSolution> problem = null;
//problem = new TaskAssignmentProblem();
problem = new BalancedTaskAssignmentProblem();
// 2. 設(shè)置交叉和變異算子 和 設(shè)置選擇算子
// 定義交叉操作: SBX交叉
double crossoverProbability = 0.9;
double crossoverDistributionIndex = 20.0;
var crossover = new IntegerSBXCrossover(crossoverProbability, crossoverDistributionIndex);
// 定義變異操作: 多項(xiàng)式變異
double mutationProbability = 1.0 / problem.numberOfVariables();
double mutationDistributionIndex = 20.0;
var mutation = new IntegerPolynomialMutation(mutationProbability, mutationDistributionIndex);
// 定義選擇操作: 二元競標(biāo)賽選擇
SelectionOperator<List<IntegerSolution>, IntegerSolution> selection = new BinaryTournamentSelection<IntegerSolution>();
// 3. 迭代次數(shù)和種群大小
int populationSize = 100;
// 4. 定義算法(NSGA-II)
Algorithm<List<IntegerSolution>> algorithm = new NSGAIIBuilder<IntegerSolution>(problem, crossover, mutation,
populationSize)
.setSelectionOperator(selection)
.setMaxEvaluations(25000)
.build();
// 5. 運(yùn)行算法
AlgorithmRunner algorithmRunner = new AlgorithmRunner.Executor(algorithm).execute();
List<IntegerSolution> solutionSet = algorithm.result();
long computingTime = algorithmRunner.getComputingTime();
JMetalLogger.logger.info("Total execution time: " + computingTime + "ms");
// 6. 打印非支配排序結(jié)果,每個(gè)solution包含決策變量取值和目標(biāo)函數(shù)取值.
for (IntegerSolution solution : solutionSet) {
JMetalLogger.logger.info("Solution: " + solution);
}
JMetalLogger.logger.info("Solution Count: " + solutionSet.size());
// 7. save to tsv files
new SolutionListOutput(solutionSet).setVarFileOutputContext(new DefaultFileOutputContext("VAR.csv", ","))
.setFunFileOutputContext(new DefaultFileOutputContext("FUN.csv", ",")).print();
AbstractAlgorithmRunner.printFinalSolutionSet(solutionSet);
}
}
/*
* 有4個(gè)任務(wù)需要3個(gè)工人完成, 需要找出最節(jié)省時(shí)間的任務(wù)分配方式.
* 目標(biāo)函數(shù)一個(gè), 即所有任務(wù)總計(jì)耗時(shí)
* 每個(gè)任務(wù)由哪個(gè)worker完成, 即決策變量, 所以共有4個(gè)變量, 變量的取值為 workerId, 范圍從0~3
*
* 任務(wù)和工人的時(shí)間Cost矩陣如下:
*
* 任務(wù) 工人A 工人B 工人C
* 任務(wù)A 2 3 1
* 任務(wù)B 4 2 3
* 任務(wù)C 3 4 2
* 任務(wù)D 1 2 4
*/
class TaskAssignmentProblem extends AbstractIntegerProblem {
private int evaluationCount;
private static final int NUMBER_OF_TASK = 4;
private static final int[][] COMPLETION_TIMES = {
{ 2, 3, 1 },
{ 4, 2, 3 },
{ 3, 4, 2 },
{ 1, 2, 4 },
};
public TaskAssignmentProblem() {
numberOfObjectives(1);
name("TaskAssignmentProblem");
// 4 varabiles for 4 tasks
var lowerList = new ArrayList<Integer>();
lowerList.add(0); // workerId lower value
lowerList.add(0); // workerId lower value
lowerList.add(0); // workerId lower value
lowerList.add(0); // workerId lower value
var upperList = new ArrayList<Integer>();
upperList.add(2); // workerId upper value
upperList.add(2); // workerId upper value
upperList.add(2); // workerId upper value
upperList.add(2); // workerId upper value
variableBounds(lowerList, upperList);
}
@Override
public IntegerSolution evaluate(IntegerSolution solution) {
int totalTime = 0;
for (int i = 0; i < NUMBER_OF_TASK; i++) {
var workerId = solution.variables().get(i);
totalTime += COMPLETION_TIMES[i][workerId];
}
solution.objectives()[0] = totalTime;
return solution;
}
}
/*
* 有4個(gè)任務(wù)需要3個(gè)工人完成, 需要找出最節(jié)省時(shí)間的任務(wù)分配方式, 同時(shí)要求每個(gè)人的工作量盡量平衡
* 目標(biāo)函數(shù)2個(gè), (1)所有任務(wù)總計(jì)耗時(shí)最小, (2)每個(gè)人總耗時(shí)最小, 即最忙的那個(gè)人耗時(shí)最小
* 每個(gè)任務(wù)由哪個(gè)worker完成, 即決策變量, 所以共有4個(gè)變量, 變量的取值為 workerId, 范圍從0~3
*
* 任務(wù)和工人的時(shí)間Cost矩陣如下:
*
* 任務(wù) 工人A 工人B 工人C
* 任務(wù)A 3 2 4
* 任務(wù)B 5 4 3
* 任務(wù)C 2 3 2
* 任務(wù)D 1 4 2
*/
class BalancedTaskAssignmentProblem extends AbstractIntegerProblem {
private int evaluationCount;
private static final int NUMBER_OF_TASK = 4;
private static final int NUMBER_OF_WORKER = 3;
private static final int[][] COMPLETION_TIMES = {
{ 3, 2, 4 },
{ 5, 4, 3 },
{ 2, 3, 2 },
{ 1, 4, 2 },
};
public BalancedTaskAssignmentProblem() {
numberOfObjectives(2);
name("TaskAssignmentProblem");
// 4 varabiles for 4 tasks
var lowerList = new ArrayList<Integer>();
lowerList.add(0); // workerId lower value
lowerList.add(0); // workerId lower value
lowerList.add(0); // workerId lower value
lowerList.add(0); // workerId lower value
var upperList = new ArrayList<Integer>();
upperList.add(2); // workerId upper value
upperList.add(2); // workerId upper value
upperList.add(2); // workerId upper value
upperList.add(2); // workerId upper value
variableBounds(lowerList, upperList);
}
@Override
public IntegerSolution evaluate(IntegerSolution solution) {
int totalTime = 0;
int[] workerTime= new int[NUMBER_OF_WORKER] ;
//目標(biāo)函數(shù): 總耗時(shí)最小
for (int i = 0; i < NUMBER_OF_TASK; i++) {
var workerId = solution.variables().get(i);
totalTime += COMPLETION_TIMES[i][workerId];
workerTime[workerId]+=COMPLETION_TIMES[i][workerId];
}
solution.objectives()[0] = totalTime;
//目標(biāo)函數(shù):每個(gè)人總耗時(shí)最小
int maxTime=0 ;
for (int time : workerTime) {
if (maxTime<time){
maxTime=time;
}
}
solution.objectives()[1]=maxTime;
return solution;
}
}

浙公網(wǎng)安備 33010602011771號(hào)