summaryrefslogtreecommitdiff
path: root/nlpsolver
diff options
context:
space:
mode:
authorTodor Balabanov <todor.balabanov@gmail.com>2021-06-18 10:46:33 +0300
committerTomaž Vajngerl <quikee@gmail.com>2021-06-20 03:59:07 +0200
commitffef53274b44bfe77128ba400845f4c4ce70893e (patch)
treefd83cef1b9ce5a528b0671ce3e1e66964b6197f9 /nlpsolver
parent4668e7e7a6322cfda854ab07eabd4322c86de980 (diff)
It is a good practice single source of random numbers to be used.
Change-Id: If76e247461288a9ed938b4f6cb592c814b8bbe2b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117406 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'nlpsolver')
-rw-r--r--nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/DEPSAgent.java3
-rw-r--r--nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java4
-rw-r--r--nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/PSGTBehavior.java7
-rw-r--r--nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java24
4 files changed, 32 insertions, 6 deletions
diff --git a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/DEPSAgent.java b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/DEPSAgent.java
index b9784c8a2ab3..58250089b02f 100644
--- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/DEPSAgent.java
+++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/DEPSAgent.java
@@ -35,6 +35,7 @@ package net.adaptivebox.deps;
import net.adaptivebox.deps.behavior.AbsGTBehavior;
import net.adaptivebox.deps.behavior.DEGTBehavior;
import net.adaptivebox.deps.behavior.PSGTBehavior;
+import net.adaptivebox.global.RandomGenerator;
import net.adaptivebox.goodness.IGoodnessCompareEngine;
import net.adaptivebox.knowledge.ILibEngine;
import net.adaptivebox.knowledge.Library;
@@ -91,7 +92,7 @@ public class DEPSAgent implements ILibEngine {
}
private AbsGTBehavior getGTBehavior() {
- if (Math.random() < switchP) {
+ if (RandomGenerator.doubleZeroOneRandom() < switchP) {
return deGTBehavior;
} else {
return psGTBehavior;
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 5dedc8052ec7..df7ff571065d 100644
--- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java
+++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java
@@ -88,7 +88,7 @@ public class DEGTBehavior extends AbsGTBehavior implements ILibEngine {
/* Handle first part of the trial vector. */
for (int index = 0; index < guaranteeIndex; index++) {
- if (CR <= Math.random()) {
+ if (CR <= RandomGenerator.doubleZeroOneRandom()) {
trailVector[index] = locaclVector[index];
continue;
}
@@ -101,7 +101,7 @@ public class DEGTBehavior extends AbsGTBehavior implements ILibEngine {
/* Handle second part of the trial vector. */
for (int index = guaranteeIndex + 1; index < DIMENSION; index++) {
- if (CR <= Math.random()) {
+ if (CR <= RandomGenerator.doubleZeroOneRandom()) {
trailVector[index] = locaclVector[index];
continue;
}
diff --git a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/PSGTBehavior.java b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/PSGTBehavior.java
index dd61355f86fd..3c7523209138 100644
--- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/PSGTBehavior.java
+++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/PSGTBehavior.java
@@ -55,6 +55,7 @@
package net.adaptivebox.deps.behavior;
+import net.adaptivebox.global.RandomGenerator;
import net.adaptivebox.goodness.IGoodnessCompareEngine;
import net.adaptivebox.knowledge.Library;
import net.adaptivebox.knowledge.SearchPoint;
@@ -102,14 +103,14 @@ public class PSGTBehavior extends AbsGTBehavior {
int DIMENSION = designSpace.getDimension();
for (int b = 0; b < DIMENSION; b++) {
- if (Math.random() < CL) {
+ if (RandomGenerator.doubleZeroOneRandom() < CL) {
designSpace.mutationAt(trailPointLocation, b);
continue;
}
double deltaxb = weight * (pcurrent_t_location[b] - pold_t_location[b])
- + c1 * Math.random() * (pbest_t_location[b] - pcurrent_t_location[b])
- + c2 * Math.random() * (gbest_t_location[b] - pcurrent_t_location[b]);
+ + c1 * RandomGenerator.doubleZeroOneRandom() * (pbest_t_location[b] - pcurrent_t_location[b])
+ + c2 * RandomGenerator.doubleZeroOneRandom() * (gbest_t_location[b] - pcurrent_t_location[b]);
// limitation for delta_x
double deltaxbm = 0.5 * designSpace.getMagnitudeIn(b);
diff --git a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java
index 4910de990091..245149e87c37 100644
--- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java
+++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java
@@ -23,6 +23,7 @@
package net.adaptivebox.global;
import java.util.Random;
+import java.security.SecureRandom;
public class RandomGenerator {
/**
@@ -31,6 +32,20 @@ public class RandomGenerator {
private static Random PRNG = new Random();
/**
+ * Switch between weaker, but faster pseudo-random number generator and
+ * stronger, but slower.
+ *
+ * @param stronger activation of secure pseudo random generator flag
+ */
+ public static void useStrongerGenerator(boolean stronger) {
+ if(stronger == true) {
+ PRNG = new SecureRandom();
+ } else {
+ PRNG = new Random();
+ }
+ }
+
+ /**
* This function returns a random integer number between the lowLimit and
* upLimit.
*
@@ -55,6 +70,15 @@ public class RandomGenerator {
return num;
}
+ /**
+ * This function returns a random float number between the zero (inclusive) and one (exclusive).
+ *
+ * @return double value in the range [0, 1)
+ */
+ public static double doubleZeroOneRandom() {
+ return PRNG.nextDouble();
+ }
+
public static int[] randomSelection(int maxNum, int times) {
if (maxNum < 0) {
maxNum = 0;