com.github.chen0040.gp.utils.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-genetic-programming Show documentation
Show all versions of java-genetic-programming Show documentation
Genetic Programming in Java, including packages on Linear Genetic Programming
package com.github.chen0040.gp.utils;
import com.github.chen0040.data.utils.TupleTwo;
import com.github.chen0040.gp.services.RandEngine;
import com.github.chen0040.gp.services.SimpleRandEngine;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xschen on 4/5/2017.
*/
public class CollectionUtils {
public static void shuffle(List a, RandEngine randEngine) {
for(int i=0; i < a.size(); ++i) {
int j = randEngine.nextInt(i+1);
exchange(a, i, j);
}
}
public static void shuffle(List a) {
RandEngine randEngine = new SimpleRandEngine();
shuffle(a, randEngine);
}
public static void exchange(List a, int i, int j) {
T temp = a.get(i);
a.set(i, a.get(j));
a.set(j, temp);
}
public static > boolean isBetterThan(T a, T b) {
return a.compareTo(b) < 0;
}
public static TupleTwo,List> split(List data, double p) {
int split_index = (int)(data.size() * p);
List data1 = new ArrayList<>();
List data2 = new ArrayList<>();
for(int i=0; i < split_index; ++i){
data1.add(data.get(i));
}
for(int i=split_index; i < data.size(); ++i) {
data2.add(data.get(i));
}
return new TupleTwo<>(data1, data2);
}
}