task = data(2:end,4); tasknum = length(task); dist = zeros(tasknum+1,tasknum+1); for i = 1:tasknum+1 for j = 1:tasknum+1 dist(i,j) = sqrt(sum((local_data(i,:)-local_data(j,:)).^2)); end end maxload = data2.data(2); v = 60; runt = dist./v; timewindow = data(2:end,5:6); worktime = data(2:end,7);的伪代码
时间: 2024-03-14 16:44:32 浏览: 24
```
// 读入数据
data = 从 'c108.txt' 中读入数据
local_data = 从 data 中提取第2列和第3列
task = 从 data 中提取第4列
tasknum = 计算 task 的长度
// 计算距离矩阵
dist = 一个大小为 tasknum+1 x tasknum+1 的零矩阵
for i = 1 到 tasknum+1
for j = 1 到 tasknum+1
dist(i,j) = 计算 local_data(i,:) 和 local_data(j,:) 之间的距离
end
end
// 计算最大载重量、速度、时间矩阵和时间窗口、工作时间
maxload = 从 data2 中提取第2个数据
v = 60
runt = dist./v
timewindow = 从 data 中提取第5列和第6列
worktime = 从 data 中提取第7列
// 存储数据
caldata.runt = runt
caldata.task = task
caldata.dist = dist
caldata.timewindow = timewindow
caldata.worktime = worktime
caldata.maxload = 100
```
相关问题
timewindow = caldata.timewindow; runt = caldata.runt; task = caldata.task; dist = caldata.dist; worktime = caldata.worktime; maxload = caldata.maxload; tasknum = length(task);
这是一段代码,它的作用是将计算数据(caldata)中的时间窗口(timewindow)、运行时间(runt)、任务(task)、距离(dist)、工作时间(worktime)和最大负载(maxload)等信息分别赋值给变量timewindow、runt、task、dist、worktime、maxload,并计算任务数量(tasknum)。
close all; dataset=importdata('D:\GA\task.txt'); H=15; noise=1e-5; ptr=1e-2; rho=10; k=4; X=dataset(:,2); Y=dataset(:,3); taskcycle=dataset(:,5); tasksize=10*dataset(:,4); XI=dataset(:,6); YI=dataset(:,7); VMc=[200 170 140 110 80 70 60 50 40 30 25 20 15 12 9 6]; size=15; G=900; F=0.3; cr=0.9; tasknum=length(X); VMnum=tasknum; Chrom=DADE_initial2(size,tasknum); gen=1; best=Chrom(1,:); besttime=zeros(G,1); for i=2:size if DEDA_Fit(Chrom(i,:),X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,k)<DEDA_Fit(best,X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,k) best=Chrom(i,:); end end besttime(1)=DEDA_Fit(best,X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,k); while gen <=G Chrom=DEDA_mutation_crossover_select(Chrom,X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,size,k,F,cr); for i=1:size if DEDA_Fit(Chrom(i,:),X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,k)<DEDA_Fit(best,X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,k) best=Chrom(i,:); end end besttime(gen)=DEDA_Fit(best,X,Y,tasknum,tasksize,taskcycle,XI,YI,VMnum,VMc,H,ptr,rho,noise,k); gen=gen+1; end plot(besttime);把这段代码用Java实现
对于这段代码的Java实现,我们需要将其进行逐行转换。以下是Java实现:
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
double[][] dataset = readData("D:\\GA\\task.txt");
double H = 15;
double noise = 1e-5;
double ptr = 1e-2;
double rho = 10;
int k = 4;
double[] X = getColumn(dataset, 1);
double[] Y = getColumn(dataset, 2);
double[] taskcycle = getColumn(dataset, 4);
double[] tasksize = multiply(getColumn(dataset, 3), 10);
double[] XI = getColumn(dataset, 5);
double[] YI = getColumn(dataset, 6);
int[] VMc = {200, 170, 140, 110, 80, 70, 60, 50, 40, 30, 25, 20, 15, 12, 9, 6};
int size = 15;
int G = 900;
double F = 0.3;
double cr = 0.9;
int tasknum = X.length;
int VMnum = tasknum;
double[][] Chrom = DADE_initial2(size, tasknum);
int gen = 1;
double[] best = Chrom[0];
double[] besttime = new double[G];
for (int i = 1; i < size; i++) {
if (DEDA_Fit(Chrom[i], X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, k) < DEDA_Fit(best, X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, k)) {
best = Chrom[i];
}
}
besttime[0] = DEDA_Fit(best, X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, k);
while (gen <= G) {
Chrom = DEDA_mutation_crossover_select(Chrom, X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, size, k, F, cr);
for (int i = 0; i < size; i++) {
if (DEDA_Fit(Chrom[i], X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, k) < DEDA_Fit(best, X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, k)) {
best = Chrom[i];
}
}
besttime[gen - 1] = DEDA_Fit(best, X, Y, tasknum, tasksize, taskcycle, XI, YI, VMnum, VMc, H, ptr, rho, noise, k);
gen++;
}
plot(besttime);
}
private static double[][] readData(String filename) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(filename));
double[][] data = new double[100][8];
int i = 0;
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(" ");
for (int j = 0; j < line.length; j++) {
data[i][j] = Double.parseDouble(line[j]);
}
i++;
}
return data;
}
private static double[] getColumn(double[][] matrix, int col) {
double[] column = new double[matrix.length];
for (int i = 0; i < matrix.length; i++) {
column[i] = matrix[i][col];
}
return column;
}
private static double[] multiply(double[] array, int factor) {
double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i] * factor;
}
return result;
}
private static void plot(double[] data) {
// implementation of plot method is omitted
}
private static double[][] DADE_initial2(int size, int tasknum) {
// implementation of DADE_initial2 method is omitted
return new double[size][tasknum];
}
private static double[][] DEDA_mutation_crossover_select(double[][] chrom, double[] x, double[] y, int tasknum, double[] tasksize, double[] taskcycle, double[] XI, double[] YI, int VMnum, int[] VMc, double H, double ptr, double rho, double noise, int popsize, int k, double f, double cr) {
// implementation of DEDA_mutation_crossover_select method is omitted
return chrom;
}
private static double DEDA_Fit(double[] chrom, double[] x, double[] y, int tasknum, double[] tasksize, double[] taskcycle, double[] XI, double[] YI, int VMnum, int[] VMc, double H, double ptr, double rho, double noise, int k) {
// implementation of DEDA_Fit method is omitted
return 0;
}
}
```
需要注意的是,这段代码中还有三个方法没有给出实现,分别是`DADE_initial2`、`DEDA_mutation_crossover_select`和`DEDA_Fit`,需要结合具体实现进行编写。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)