summaryrefslogtreecommitdiff
path: root/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/sco/SCAgent.java
blob: 910270e9e2dae711b42bc106fe9712679ecfe42e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package net.adaptivebox.sco;

/**
 * Description: The description of social cognitive agent.
 *
 * @Information source: a) external library (L); b) the own memory: a point that
 * generated in the last learning cycle
 *
 * @Coefficients: TaoB and TaoW
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    Mar 11, 2003
 * Xiaofeng Xie    May 11, 2004
 * Xiaofeng Xie    May 20, 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.0
 * @Since MAOS1.0
 *
 * @References:
 * [1] Xie X F, Zhang W J. Solving engineering design problems by social cognitive
 * optimization. Genetic and Evolutionary Computation Conference, 2004: 261-262
 */


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

public class SCAgent {

  //Describes the problem to be solved (encode the point into intermediate information)
  private ProblemEncoder problemEncoder;
  //Forms the goodness landscape
  private IGoodnessCompareEngine specComparator;

  //the coefficients of SCAgent
  protected int TaoB = 2;
  //The early version set TaoW as the size of external library (NL), but 4 is often enough
  protected int TaoW = 4;

  //The referred external library
  protected Library externalLib;
  //store the point that generated in current learning cycle
  protected SearchPoint trailPoint;
  //the own memory: store the point that generated in last learning cycle
  protected SearchPoint pcurrent_t;

  public void setExternalLib(Library lib) {
    externalLib = lib;
  }

  public void setProblemEncoder(ProblemEncoder encoder) {
    problemEncoder = encoder;
    trailPoint = problemEncoder.getFreshSearchPoint();
    pcurrent_t = problemEncoder.getEncodedSearchPoint();
  }

  public void setSpecComparator(IGoodnessCompareEngine comparer) {
    specComparator = comparer;
  }

  public SearchPoint generatePoint() {
    //generate a new point
    generatePoint(trailPoint);
    //evalute the generated point
    problemEncoder.evaluate(trailPoint);
    return trailPoint;
  }

  protected void generatePoint(ILocationEngine tempPoint) {
    SearchPoint Xmodel, Xrefer, libBPoint;

    // choose Selects a better point (libBPoint) from externalLib (L) based
    // on tournament selection
    int xb = externalLib.tournamentSelection(specComparator, TaoB, true);
    libBPoint = externalLib.getSelectedPoint(xb);
    // Compares pcurrent_t with libBPoint
    // The better one becomes model point (Xmodel)
    // The worse one becomes refer point (Xrefer)
    if(specComparator.compare(pcurrent_t.getEncodeInfo(), libBPoint.getEncodeInfo())==IGoodnessCompareEngine.LARGER_THAN) {
      Xmodel = libBPoint;
      Xrefer = pcurrent_t;
    } else {
      Xmodel = pcurrent_t;
      Xrefer = libBPoint;
    }
    // observational learning: generates a new point near the model point, which
    // the variation range is decided by the difference of Xmodel and Xrefer
    inferPoint(tempPoint, Xmodel, Xrefer, problemEncoder.getDesignSpace());
  }

  //1. Update the current point into the external library
  //2. Replace the current point by the generated point
  public void updateInfo() {
    //Selects a bad point kw from TaoW points in Library
    int xw = externalLib.tournamentSelection(specComparator, TaoW, false);
    //Repaces kw with pcurrent_t
    externalLib.getSelectedPoint(xw).importPoint(pcurrent_t);
    //Repaces pcurrent_t (x(t)) with trailPoint (x(t+1))
    pcurrent_t.importPoint(trailPoint);
   }

  //  1---model point, 2---refer point
   public boolean inferPoint(ILocationEngine newPoint, ILocationEngine point1,ILocationEngine point2, DesignSpace space){
     double[] newLoc = newPoint.getLocation();
     double[] real1 = point1.getLocation();
     double[] real2 = point2.getLocation();

     for (int i=0; i<newLoc.length; i++) {
       newLoc[i] = real1[i]*2-real2[i];
       //boundary handling
       newLoc[i] = space.boundAdjustAt(newLoc[i], i);
       newLoc[i] = RandomGenerator.doubleRangeRandom(newLoc[i], real2[i]);
     }
     return true;
   }
}