tdf#149907: sc: Add UItest
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / problem / ProblemEncoder.java
blob674d275420c39009f58df4ecb0301186377da572
1 /**
2 * Description: Encodes the specified problem into encoded information for
3 * forming the goodness landscape.
5 * Author Create/Modi Note
6 * Xiaofeng Xie May 31, 2000
7 * Xiaofeng Xie Sep. 19, 2002
8 * Xiaofeng Xie Mar. 01, 2003
9 * Xiaofeng Xie May 11, 2004
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * Please acknowledge the author(s) if you use this code in any way.
23 * @version 1.0
24 * @Since MAOS1.0
27 package net.adaptivebox.problem;
29 import net.adaptivebox.encode.EvalElement;
30 import net.adaptivebox.encode.EvalStruct;
31 import net.adaptivebox.global.BasicBound;
32 import net.adaptivebox.knowledge.SearchPoint;
33 import net.adaptivebox.space.DesignDim;
34 import net.adaptivebox.space.DesignSpace;
36 public abstract class ProblemEncoder {
37 // Store the calculated results for the responses
38 private final double[] tempResponseSet; // temp values
39 private final double[] tempLocation; // temp values
41 // the search space (S)
42 private final DesignSpace designSpace;
44 // For evaluate the response vector into encoded vector double[2]
45 private final EvalStruct evalStruct;
47 protected ProblemEncoder(int paramNum, int targetNum) throws Exception {
48 designSpace = new DesignSpace(paramNum);
49 evalStruct = new EvalStruct(targetNum);
50 tempLocation = new double[paramNum];
51 tempResponseSet = new double[targetNum];
54 public DesignSpace getDesignSpace() {
55 return designSpace;
58 // set the default information for each dimension of search space (S)
59 protected void setDefaultXAt(int i, double min, double max, double grain) {
60 DesignDim dd = new DesignDim();
61 dd.grain = grain;
62 dd.paramBound = new BasicBound(min, max);
63 designSpace.setElemAt(dd, i);
66 // set the default information for evaluation each response
67 protected void setDefaultYAt(int i, double min, double max) {
68 EvalElement ee = new EvalElement();
69 ee.targetBound = new BasicBound(min, max);
70 evalStruct.setElemAt(ee, i);
73 // get a fresh point
74 public SearchPoint getFreshSearchPoint() {
75 return new SearchPoint(designSpace.getDimension());
78 // get an encoded point
79 public SearchPoint getEncodedSearchPoint() {
80 SearchPoint point = getFreshSearchPoint();
81 designSpace.initializeGene(point.getLocation());
82 evaluate(point);
83 return point;
86 // evaluate the point into encoded information
87 public void evaluate(SearchPoint point) {
88 // copy to temp point
89 System.arraycopy(point.getLocation(), 0, this.tempLocation, 0, tempLocation.length);
91 // mapping the temp point to original search space S
92 designSpace.getMappingPoint(tempLocation);
94 // calculate based on the temp point
95 calcTargets(tempResponseSet, tempLocation);
96 evalStruct.evaluate(point.getEncodeInfo(), tempResponseSet);
97 point.setObjectiveValue(tempResponseSet[0]);
100 // calculate each response, must be implemented
101 abstract protected double calcTargetAt(int index, double[] VX);
103 // calculate all the responses VY[] based on given point VX[]
104 private void calcTargets(double[] VY, double[] VX) {
105 for (int i = 0; i < VY.length; i++) {
106 VY[i] = calcTargetAt(i, VX);