tdf#149907: sc: Add UItest
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / knowledge / Library.java
blob76e57ac76077b43e9e74f891880001a579f04896
2 /**
3 * Description: Contains a set of points.
5 * Author Create/Modi Note
6 * Xiaofeng Xie Mar 7, 2003
7 * Xiaofeng Xie May 3, 2003
8 * Xiaofeng Xie May 11, 2004
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * Please acknowledge the author(s) if you use this code in any way.
22 * @version 1.1
23 * @Since MAOS1.0
25 package net.adaptivebox.knowledge;
27 import net.adaptivebox.global.BasicBound;
28 import net.adaptivebox.global.RandomGenerator;
29 import net.adaptivebox.goodness.IGoodnessCompareEngine;
30 import net.adaptivebox.problem.ProblemEncoder;
32 public class Library {
33 private final SearchPoint[] libPoints;
34 private int gIndex = -1;
36 public Library(int number, ProblemEncoder problemEncoder) {
37 libPoints = new SearchPoint[number];
38 for (int i = 0; i < number; i++) {
39 libPoints[i] = problemEncoder.getEncodedSearchPoint();
43 public SearchPoint getGbest() {
44 return getSelectedPoint(gIndex);
47 public void refreshGbest(IGoodnessCompareEngine qualityComparator) {
48 gIndex = tournamentSelection(qualityComparator, getPopSize() - 1, true);
51 public int getPopSize() {
52 return libPoints.length;
55 public SearchPoint getSelectedPoint(int index) {
56 return libPoints[index];
59 public SearchPoint getRandomPoint() {
60 return libPoints[RandomGenerator.intRangeRandom(0, libPoints.length - 1)];
63 public static boolean replace(IGoodnessCompareEngine comparator, SearchPoint outPoint,
64 SearchPoint tobeReplacedPoint) {
65 boolean isBetter = false;
66 if (comparator.compare(outPoint.getEncodeInfo(),
67 tobeReplacedPoint.getEncodeInfo()) < IGoodnessCompareEngine.LARGER_THAN) {
68 tobeReplacedPoint.importPoint(outPoint);
69 isBetter = true;
71 return isBetter;
74 public int tournamentSelection(IGoodnessCompareEngine comparator, int times, boolean isBetter) {
75 int[] indices = RandomGenerator.randomSelection(getPopSize(), times);
76 int currentIndex = indices[0];
77 for (int i = 1; i < indices.length; i++) {
78 int compareValue = comparator.compare(libPoints[indices[i]].getEncodeInfo(),
79 libPoints[currentIndex].getEncodeInfo());
80 if (isBetter == (compareValue < IGoodnessCompareEngine.LARGER_THAN)) {
81 currentIndex = indices[i];
84 return currentIndex;
87 public double getExtremalVcon(boolean isMAX) {
88 double val = BasicBound.MINDOUBLE;
89 for (int i = 0; i < libPoints.length; i++) {
90 if (libPoints[i].getEncodeInfo()[0] > val == isMAX) {
91 val = libPoints[i].getEncodeInfo()[0];
94 return val;
97 public int getVconThanNum(double allowedCons) {
98 int num = 0;
99 for (int i = 0; i < libPoints.length; i++) {
100 if (libPoints[i].getEncodeInfo()[0] <= allowedCons) {
101 num++;
104 return num;