summaryrefslogtreecommitdiff
path: root/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/knowledge/Library.java
blob: eb204576559443b75eb31939427e68a9dd8eede5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

/**
 * Description: Contains a set of points.
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    Mar 7, 2003
 * Xiaofeng Xie    May 3, 2003
 * Xiaofeng Xie    May 11, 2004
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * Please acknowledge the author(s) if you use this code in any way.
 *
 * @version 1.1
 * @Since MAOS1.0
 */
package net.adaptivebox.knowledge;

import net.adaptivebox.global.*;
import net.adaptivebox.goodness.*;
import net.adaptivebox.problem.*;

public class Library {
  private SearchPoint[] libPoints = new SearchPoint[0];
  protected int gIndex = -1;

  public Library(SearchPoint[] points){
    this.libPoints = points;
  }

  public Library(int number, ProblemEncoder problemEncoder){
    libPoints = new SearchPoint[number];
    for (int i=0; i<number; i++) {
      libPoints[i] = problemEncoder.getEncodedSearchPoint();
    }
  }

  public SearchPoint getGbest() {
    return getSelectedPoint(gIndex);
  }

  public void refreshGbest(IGoodnessCompareEngine qualityComparator) {
    gIndex = tournamentSelection(qualityComparator, getPopSize()-1, true);
  }

  public int getPopSize() {
    return libPoints.length;
  }

  public SearchPoint getSelectedPoint(int index) {
    return libPoints[index];
  }

  public static boolean replace(IGoodnessCompareEngine comparator, SearchPoint outPoint, SearchPoint tobeReplacedPoint) {
    boolean isBetter = false;
    if(comparator.compare(outPoint.getEncodeInfo(), tobeReplacedPoint.getEncodeInfo())<IGoodnessCompareEngine.LARGER_THAN) {
      tobeReplacedPoint.importPoint(outPoint);
      isBetter = true;
    }
    return isBetter;
  }

  public int tournamentSelection(IGoodnessCompareEngine comparator, int times, boolean isBetter) {
    int[] indices = RandomGenerator.randomSelection(getPopSize(), times);
    int currentIndex = indices[0];
    for (int i=1; i<indices.length; i++) {
      int compareValue = comparator.compare(libPoints[indices[i]].getEncodeInfo(), libPoints[currentIndex].getEncodeInfo());
      if (isBetter == (compareValue<IGoodnessCompareEngine.LARGER_THAN)) {
        currentIndex = indices[i];
      }
    }
    return currentIndex;
  }

  public double getExtremalVcon(boolean isMAX) {
    double val=BasicBound.MINDOUBLE;
    for(int i=0; i<libPoints.length; i++) {
      if(libPoints[i].getEncodeInfo()[0]>val==isMAX) {
        val = libPoints[i].getEncodeInfo()[0];
      }
    }
    return val;
  }

  public int getVconThanNum(double allowedCons) {
    int num=0;
    for(int i=0; i<libPoints.length; i++) {
      if(libPoints[i].getEncodeInfo()[0]<=allowedCons) {
        num++;
      }
    }
    return num;
  }

}