Skip to content
Snippets Groups Projects
Commit 492cd4e2 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Started LSHADE.

Implemented update memory and weighted lehmer mean.
parent 9fb96d06
No related branches found
No related tags found
No related merge requests found
package quantization.de.shade;
import quantization.de.DEIndividual;
import quantization.de.DESolverWithArchive;
import quantization.de.DeException;
import quantization.de.DeHistory;
import quantization.utilities.Means;
import quantization.utilities.Utils;
import java.util.ArrayList;
public class LShadeSolver extends DESolverWithArchive {
private int memorySize;
private int memoryIndex = 0;
private double[] memoryCr;
private double[] memoryF;
protected LShadeSolver(int dimension, int populationSize, int generationCount, int memorySize) {
super(dimension, populationSize, generationCount, populationSize);
this.memorySize = memorySize;
}
private void initializeMemory() {
memoryIndex = 0;
memoryCr = new double[memorySize];
memoryF = new double[memorySize];
}
@Override
public DeHistory[] train() throws DeException {
initializeMemory();
ArrayList<Double> successfulCr = new ArrayList<Double>();
ArrayList<Double> successfulF = new ArrayList<Double>();
for (int generation = 0; generation < generationCount; generation++) {
successfulCr.clear();
successfulF.clear();
}
return new DeHistory[0];
}
private void updateMemory(final ArrayList<Integer> successfulIndices,
final ArrayList<Double> successfulCr,
final ArrayList<Double> successfulF,
final DEIndividual[] offsprings) {
assert ((successfulIndices.size() == successfulCr.size()) && (successfulCr.size() == successfulF.size()));
int kCount = successfulCr.size();
double[] weights = new double[kCount];
for (int k = 0; k < kCount; k++) {
final int successfulIndex = successfulIndices.get(k);
double numerator = Math.abs(offsprings[successfulIndex].getFitness() - currentPopulation[successfulIndex].getFitness());
double denominator = 0.0;
for (int l = 0; l < kCount; l++) {
Math.abs(offsprings[successfulIndices.get(l)].getFitness() - currentPopulation[successfulIndices.get(l)].getFitness());
}
weights[k] = (numerator / denominator);
}
if ((!successfulCr.isEmpty()) && (!successfulF.isEmpty())) {
if ((Double.isNaN(memoryCr[memoryIndex])) || (Utils.arrayListMax(successfulCr) == 0)) {
memoryCr[memoryIndex] = Double.NaN;
} else {
memoryCr[memoryIndex] = Means.weightedLehmerMean(successfulCr, weights);
}
memoryF[memoryIndex] = Means.weightedLehmerMean(successfulF, weights);
++memoryIndex;
if (memoryIndex >= memorySize) {
memoryIndex = 0;
}
} else {
memoryCr[memoryIndex] = memoryCr[memoryIndex];
memoryF[memoryIndex] = memoryF[memoryIndex];
}
}
public int getMemorySize() {
return memorySize;
}
public void setMemorySize(final int memorySize) {
this.memorySize = memorySize;
}
}
......@@ -16,7 +16,6 @@ public class Means {
public static double lehmerMean(final ArrayList<Double> values) {
double numerator = 0.0;
double denominator = 0.0;
double value;
for (double val : values) {
numerator += Math.pow(val, 2);
......@@ -26,4 +25,20 @@ public class Means {
double result = numerator / denominator;
return result;
}
public static double weightedLehmerMean(final ArrayList<Double> values,
final double[] weights) {
assert (values.size() == weights.length);
double numerator = 0.0;
double denominator = 0.0;
for (int i = 0; i < values.size(); i++) {
numerator += (weights[i] * Math.pow(values.get(i), 2));
denominator += (weights[i] * values.get(i));
}
double result = numerator / denominator;
return result;
}
}
......@@ -3,12 +3,12 @@ package quantization.utilities;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class Utils {
public static double calculatePsnr(final double mse, final int signalMax)
{
double psnr = 10.0 * Math.log10((Math.pow(signalMax, 2) / mse));
public static double calculatePsnr(final double mse, final int signalMax) {
double psnr = 10.0 * Math.log10((Math.pow(signalMax, 2) / mse));
return psnr;
}
......@@ -30,6 +30,16 @@ public class Utils {
return false;
}
public static double arrayListMax(final ArrayList<Double> array) {
double max = Double.MIN_VALUE;
for (final double val : array) {
if (val > max) {
max = val;
}
}
return max;
}
public static boolean arrayContainsToIndex(final int[] array, final int toIndex, final int element) {
for (int i = 0; i < toIndex; i++) {
if (array[i] == element)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment