svx: prefix members of SvxClipboardFormatItem
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / sco / SCAgent.java
bloba09d0dcfd6b238d31e13bc0343b1e374cd3d9406
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
36 import net.adaptivebox.problem.ProblemEncoder;
37 import net.adaptivebox.space.DesignSpace;
38 import net.adaptivebox.space.ILocationEngine;
39 import net.adaptivebox.global.RandomGenerator;
40 import net.adaptivebox.goodness.IGoodnessCompareEngine;
41 import net.adaptivebox.knowledge.Library;
42 import net.adaptivebox.knowledge.SearchPoint;
44 public class SCAgent {
46 // Describes the problem to be solved (encode the point into intermediate information)
47 private ProblemEncoder problemEncoder;
49 // Forms the goodness landscape
50 private IGoodnessCompareEngine specComparator;
52 // the coefficients of SCAgent
53 private static final int TaoB = 2;
55 // The early version set TaoW as the size of external library (NL), but 4 is often enough
56 private static final int TaoW = 4;
58 // The referred external library
59 private Library externalLib;
61 // store the point that generated in current learning cycle
62 private SearchPoint trailPoint;
64 // the own memory: store the point that generated in last learning cycle
65 private SearchPoint pcurrent_t;
67 public void setExternalLib(Library lib) {
68 externalLib = lib;
71 public void setProblemEncoder(ProblemEncoder encoder) {
72 problemEncoder = encoder;
73 trailPoint = problemEncoder.getFreshSearchPoint();
74 pcurrent_t = problemEncoder.getEncodedSearchPoint();
77 public void setSpecComparator(IGoodnessCompareEngine comparer) {
78 specComparator = comparer;
81 public SearchPoint generatePoint() {
82 // generate a new point
83 generatePoint(trailPoint);
85 // evaluate the generated point
86 problemEncoder.evaluate(trailPoint);
87 return trailPoint;
90 private void generatePoint(ILocationEngine tempPoint) {
91 SearchPoint Xmodel, Xrefer, libBPoint;
93 // choose Selects a better point (libBPoint) from externalLib (L) based
94 // on tournament selection
95 int xb = externalLib.tournamentSelection(specComparator, TaoB, true);
96 libBPoint = externalLib.getSelectedPoint(xb);
98 // Compares pcurrent_t with libBPoint
99 // The better one becomes model point (Xmodel)
100 // The worse one becomes refer point (Xrefer)
101 if (specComparator.compare(pcurrent_t.getEncodeInfo(),
102 libBPoint.getEncodeInfo()) == IGoodnessCompareEngine.LARGER_THAN) {
103 Xmodel = libBPoint;
104 Xrefer = pcurrent_t;
105 } else {
106 Xmodel = pcurrent_t;
107 Xrefer = libBPoint;
110 // observational learning: generates a new point near the model point, which
111 // the variation range is decided by the difference of Xmodel and Xrefer
112 inferPoint(tempPoint, Xmodel, Xrefer, problemEncoder.getDesignSpace());
115 // 1. Update the current point into the external library
116 // 2. Replace the current point by the generated point
117 public void updateInfo() {
118 // Selects a bad point kw from TaoW points in Library
119 int xw = externalLib.tournamentSelection(specComparator, TaoW, false);
121 // Replaces kw with pcurrent_t
122 externalLib.getSelectedPoint(xw).importPoint(pcurrent_t);
124 // Replaces pcurrent_t (x(t)) with trailPoint (x(t+1))
125 pcurrent_t.importPoint(trailPoint);
128 // 1---model point, 2---refer point
129 private boolean inferPoint(ILocationEngine newPoint, ILocationEngine point1, ILocationEngine point2,
130 DesignSpace space) {
131 double[] newLoc = newPoint.getLocation();
132 double[] real1 = point1.getLocation();
133 double[] real2 = point2.getLocation();
135 for (int i = 0; i < newLoc.length; i++) {
136 newLoc[i] = real1[i] * 2 - real2[i];
137 // boundary handling
138 newLoc[i] = space.boundAdjustAt(newLoc[i], i);
139 newLoc[i] = RandomGenerator.doubleRangeRandom(newLoc[i], real2[i]);
141 return true;