bump product version to 4.1.6.2
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / sco / SCAgent.java
blob910270e9e2dae711b42bc106fe9712679ecfe42e
1 package net.adaptivebox.sco;
3 /**
4 * Description: The description of social cognitive agent.
6 * @Information source: a) external library (L); b) the own memory: a point that
7 * generated in the last learning cycle
9 * @Coefficients: TaoB and TaoW
11 * @ Author Create/Modi Note
12 * Xiaofeng Xie Mar 11, 2003
13 * Xiaofeng Xie May 11, 2004
14 * Xiaofeng Xie May 20, 2004
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * Please acknowledge the author(s) if you use this code in any way.
28 * @version 1.0
29 * @Since MAOS1.0
31 * @References:
32 * [1] Xie X F, Zhang W J. Solving engineering design problems by social cognitive
33 * optimization. Genetic and Evolutionary Computation Conference, 2004: 261-262
37 import net.adaptivebox.global.*;
38 import net.adaptivebox.space.*;
39 import net.adaptivebox.goodness.*;
40 import net.adaptivebox.problem.*;
41 import net.adaptivebox.knowledge.*;
43 public class SCAgent {
45 //Describes the problem to be solved (encode the point into intermediate information)
46 private ProblemEncoder problemEncoder;
47 //Forms the goodness landscape
48 private IGoodnessCompareEngine specComparator;
50 //the coefficients of SCAgent
51 protected int TaoB = 2;
52 //The early version set TaoW as the size of external library (NL), but 4 is often enough
53 protected int TaoW = 4;
55 //The referred external library
56 protected Library externalLib;
57 //store the point that generated in current learning cycle
58 protected SearchPoint trailPoint;
59 //the own memory: store the point that generated in last learning cycle
60 protected SearchPoint pcurrent_t;
62 public void setExternalLib(Library lib) {
63 externalLib = lib;
66 public void setProblemEncoder(ProblemEncoder encoder) {
67 problemEncoder = encoder;
68 trailPoint = problemEncoder.getFreshSearchPoint();
69 pcurrent_t = problemEncoder.getEncodedSearchPoint();
72 public void setSpecComparator(IGoodnessCompareEngine comparer) {
73 specComparator = comparer;
76 public SearchPoint generatePoint() {
77 //generate a new point
78 generatePoint(trailPoint);
79 //evalute the generated point
80 problemEncoder.evaluate(trailPoint);
81 return trailPoint;
84 protected void generatePoint(ILocationEngine tempPoint) {
85 SearchPoint Xmodel, Xrefer, libBPoint;
87 // choose Selects a better point (libBPoint) from externalLib (L) based
88 // on tournament selection
89 int xb = externalLib.tournamentSelection(specComparator, TaoB, true);
90 libBPoint = externalLib.getSelectedPoint(xb);
91 // Compares pcurrent_t with libBPoint
92 // The better one becomes model point (Xmodel)
93 // The worse one becomes refer point (Xrefer)
94 if(specComparator.compare(pcurrent_t.getEncodeInfo(), libBPoint.getEncodeInfo())==IGoodnessCompareEngine.LARGER_THAN) {
95 Xmodel = libBPoint;
96 Xrefer = pcurrent_t;
97 } else {
98 Xmodel = pcurrent_t;
99 Xrefer = libBPoint;
101 // observational learning: generates a new point near the model point, which
102 // the variation range is decided by the difference of Xmodel and Xrefer
103 inferPoint(tempPoint, Xmodel, Xrefer, problemEncoder.getDesignSpace());
106 //1. Update the current point into the external library
107 //2. Replace the current point by the generated point
108 public void updateInfo() {
109 //Selects a bad point kw from TaoW points in Library
110 int xw = externalLib.tournamentSelection(specComparator, TaoW, false);
111 //Repaces kw with pcurrent_t
112 externalLib.getSelectedPoint(xw).importPoint(pcurrent_t);
113 //Repaces pcurrent_t (x(t)) with trailPoint (x(t+1))
114 pcurrent_t.importPoint(trailPoint);
117 // 1---model point, 2---refer point
118 public boolean inferPoint(ILocationEngine newPoint, ILocationEngine point1,ILocationEngine point2, DesignSpace space){
119 double[] newLoc = newPoint.getLocation();
120 double[] real1 = point1.getLocation();
121 double[] real2 = point2.getLocation();
123 for (int i=0; i<newLoc.length; i++) {
124 newLoc[i] = real1[i]*2-real2[i];
125 //boundary handling
126 newLoc[i] = space.boundAdjustAt(newLoc[i], i);
127 newLoc[i] = RandomGenerator.doubleRangeRandom(newLoc[i], real2[i]);
129 return true;