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实现
时间: 2023-07-29 20:10:22 浏览: 117
对于这段代码的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`,需要结合具体实现进行编写。
阅读全文