// initialize objects private void initDataObjects() { objects = new ArrayList<DataObject>(); for (int i = 0; i < n; i++) { DataObject obj = new DataObject(); int year = (int) (Math.random() * MAX_YEAR); double ratio1 = Math.random() % MAX_RATIO; double ratio2 = Math.random() % MAX_RATIO; obj.setYear(year); obj.setRatio1(ratio1); obj.setRatio2(ratio2); objects.add(obj); } System.out.println("Data Size:" + objects.size()); }
private CenterObject getMutant(CenterObject center, float f) { CenterObject mutant = new CenterObject(); int a = (int) (Math.random() * n); int b = (int) (Math.random() * n); int c = (int) (Math.random() * n); // System.out.println("a b c:" + a + " " + b + " " + c); DataObject objA = objects.get(a); DataObject objB = objects.get(b); DataObject objC = objects.get(c); DataObject obj; for (int i = 0; i < k; i++) { int year = (int) (objA.getYear() + f * (objB.getYear() - objC.getYear())); double ratio1 = objA.getRatio1() + f * (objB.getRatio1() - objC.getRatio1()); double ratio2 = objA.getRatio2() + f * (objB.getRatio2() - objC.getRatio2()); obj = new DataObject(); obj.setYear(year); obj.setRatio1(ratio1); obj.setRatio2(ratio2); mutant.getObjs().add(obj); System.out.println("Mutant " + i); printCenter(mutant); } return mutant; }
private float getFitness(CenterObject center) { float fitness = 0.0F; for (DataObject c : center.getObjs()) { for (DataObject obj : objects) { fitness += (obj.getYear() - c.getYear()) * (obj.getYear() - c.getYear()) + (obj.getRatio1() - c.getRatio1()) * (obj.getRatio1() - c.getRatio1()) + (obj.getRatio2() - c.getRatio2()) * (obj.getRatio2() - c.getRatio2()); } } System.out.println("Fitness:" + fitness); return fitness; }