diff options
author | Todor Balabanov <todor.balabanov@gmail.com> | 2019-05-02 15:17:38 +0300 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2019-05-12 04:04:54 +0200 |
commit | c728bb5420f19eb6fbec859ec927f35d772694be (patch) | |
tree | ea8a176644d4d3d9d3920ab79d3b52bc3a604a35 /nlpsolver | |
parent | b396e68f10f2839c1afd95aa3a5f355c8e4f1de8 (diff) |
Generate behavior code simplification and very small speed-up.
Change-Id: Ib13080e4c21738affa129d12a07f5380f665e7a4
Reviewed-on: https://gerrit.libreoffice.org/71673
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'nlpsolver')
-rw-r--r-- | nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java | 61 |
1 files changed, 48 insertions, 13 deletions
diff --git a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java index a58a66196372..dc7f7400cd58 100644 --- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java +++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java @@ -48,24 +48,59 @@ public class DEGTBehavior extends AbsGTBehavior implements ILibEngine { pbest_t = pbest; } + /** + * Crossover and mutation for a single vector element done in a single step. + * + * @param index + * Index of the trial vector element to be changed. + * @param trialVector + * Trial vector reference. + * @param globalVector + * Global best found vector reference. + * @param differenceVectors + * List of vectors used for difference delta calculation. + */ + private void crossoverAndMutation(int index, double trialVector[], double globalVector[], BasicPoint differenceVectors[]) { + double delta = 0D; + + for (int i = 0; i < differenceVectors.length; i++) { + delta += (i % 2 == 0 ? +1D : -1D) * differenceVectors[i].getLocation()[index]; + } + + trialVector[index] = globalVector[index] + FACTOR * delta; + } + @Override public void generateBehavior(SearchPoint trailPoint, ProblemEncoder problemEncoder) { - SearchPoint gbest_t = socialLib.getGbest(); - BasicPoint[] referPoints = getReferPoints(); int DIMENSION = problemEncoder.getDesignSpace().getDimension(); - int rj = RandomGenerator.intRangeRandom(0, DIMENSION-1); - for (int k=0; k<DIMENSION; k++) { - if (Math.random()<CR || k == DIMENSION-1) { - double Dabcd = 0; - for(int i=0; i<referPoints.length; i++) { - Dabcd += (i%2==0 ? +1D : -1D)*referPoints[i].getLocation()[rj]; - } - trailPoint.getLocation()[rj] = gbest_t.getLocation()[rj]+FACTOR*Dabcd; - } else { - trailPoint.getLocation()[rj] = pbest_t.getLocation()[rj]; + int guaranteeIndex = RandomGenerator.intRangeRandom(0, DIMENSION - 1); + + double[] trailVector = trailPoint.getLocation(); + double[] locaclVector = pbest_t.getLocation(); + double[] globalVector = socialLib.getGbest().getLocation(); + + /* Handle first part of the trial vector. */ + for (int index = 0; index < guaranteeIndex; index++) { + if (CR <= Math.random()) { + trailVector[index] = locaclVector[index]; + continue; } - rj = (rj+1)%DIMENSION; + + crossoverAndMutation(index, trailVector, globalVector, referPoints); + } + + /* Guarantee for at least one change in the trial vector. */ + crossoverAndMutation(guaranteeIndex, trailVector, globalVector, referPoints); + + /* Handle second part of the trial vector. */ + for (int index = guaranteeIndex + 1; index < DIMENSION; index++) { + if (CR <= Math.random()) { + trailVector[index] = locaclVector[index]; + continue; + } + + crossoverAndMutation(index, trailVector, globalVector, referPoints); } } |