这是我用来使用 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;
}
}
我用来SortedMap
根据参数 和 获取最低Alpha
计算Beta
值Gamma
。(我知道这里不是这种情况,因为它是一个非常简化的摘录。)
我想根据,和in获取所有可能的值(变量),并获取最小值的参数。但是,发布的代码的输出是:value
Alpha
Beta
Gamma
minFromMap
value
值:2.990180006385608 Alpha:0.9 Beta:0.9 Lambda:0.9 k:2
我知道该值不会改变,这就是为什么在 SortedMap 中只存储一个条目,但正如刚才提到的:是否有可能存储所有带有值和参数的条目并像这样对它们进行排序SortedMap
?谁能帮我解决这个问题?谢谢。