0

This is a very simplified excerpt of my code I am using to get the lowest computed value and its parameters using SortedMap:

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry;

public class Main {

    public static void main(String[] args) throws Exception {
        getMinValue();
    }

    public static Parameter getMinValue() throws Exception {

        SortedMap<Double, Parameter> minFromMap = new TreeMap<Double, Parameter>();
        List<Double> real = new ArrayList<>(
                Arrays.asList(4.0, 3.0, 3.0, 1.0, 0.0, 2.0, 5.0, 1.0, 0.0, 0.0,
                        1.0, 7.0, 4.0, 1.0, 2.0, 4.0, 0.0));
        int k = 2;
        BigDecimal step = new BigDecimal("0.1");

        for (BigDecimal alphaNew = BigDecimal.ZERO; alphaNew
                .compareTo(BigDecimal.ONE) < 0; alphaNew = alphaNew.add(step)) {

            for (BigDecimal betaNew = BigDecimal.ZERO; betaNew
                    .compareTo(BigDecimal.ONE) < 0; betaNew = betaNew.add(step)) {

                for (BigDecimal lambda = BigDecimal.ZERO; lambda
                        .compareTo(BigDecimal.ONE) < 0; lambda = lambda
                        .add(step)) {

                    Parameter param = new Parameter();
                    param.setAlpha(alphaNew.doubleValue());
                    param.setBeta(betaNew.doubleValue());
                    param.setGamma(lambda.doubleValue());
                    param.setK(k);

                    double sumUp = 0;
                    double value = 0;

                    int observations = 0;
                    for (int i = 0; i < real.size(); i++) {
                        double pow = Math.pow(real.get(i), 2);
                        sumUp += pow;
                        observations++;
                    }
                    value = Math.sqrt(sumUp / observations);
                    param.setValue(value);
                    minFromMap.put(value, param);
                }
            }
        }

        for (Entry<Double, Parameter> en : minFromMap.entrySet()) {
            System.out.println("Value: " + en.getKey() + " Alpha: "
                    + en.getValue().getAlpha() + " Beta: "
                    + en.getValue().getBeta() + " Lambda: "
                    + en.getValue().getGamma() + " k: " + en.getValue().getK());
        }

        Parameter param = new Parameter();

        param.setAlpha(minFromMap.get(minFromMap.firstKey()).getAlpha());
        param.setBeta(minFromMap.get(minFromMap.firstKey()).getBeta());
        param.setGamma(minFromMap.get(minFromMap.firstKey()).getGamma());
        param.setK(minFromMap.get(minFromMap.firstKey()).getK());
        param.setValue(minFromMap.get(minFromMap.firstKey()).getValue());

        return param;
    }
}

public class Parameter {

    private double alpha;
    private double beta;
    private double gamma;
    private int k;
    private double value;

    public double getAlpha() {
        return alpha;
    }

    public void setAlpha(double alpha) {
        this.alpha = alpha;
    }

    public double getBeta() {
        return beta;
    }

    public void setBeta(double beta) {
        this.beta = beta;
    }

    public double getGamma() {
        return gamma;
    }

    public void setGamma(double gamma) {
        this.gamma = gamma;
    }

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }
}

I use SortedMap to get the lowest computed value depending on the parameters Alpha, Beta and Gamma. (I know this is not the case here because it is a very simplified excerpt.)

I would like to get all possible values (variable value) stored depending on Alpha, Beta and Gamma in minFromMap and to get the parameters for which value is the lowest value. However, the output of the posted code is:

Value: 2.990180006385608 Alpha: 0.9 Beta: 0.9 Lambda: 0.9 k: 2

I know that the value does not change and that's why only one entry is stored in the SortedMap but as just mentioned: is there any possibility to store all entries with values and parameters and to sort them like SortedMap? Can anyone help me solving this issue? Thanks.

Tunc Jamgocyan
  • 321
  • 2
  • 7
  • 18

1 Answers1

0

Inside your loops, you re-initialise :

double sumUp = 0;
double value = 0;

And you compute these value without the different parameter values. You will end up with only one entry in the sorted map. If you change the computing part in order to use the parameters, then you will have different value and several entries in the map.

Apart for this, the code is ok and should work : since you use a SortedMap<Double, Parameter>, the map will be sorted on the natural order of Double, that means the first element shall be the minimum.

Note : at the end of the getMinValue() method, you don't need to copy the parameters in a new object, just return

return minFromMap.get(minFromMap.firstKey());
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32