[Libreoffice-commits] core.git: nlpsolver/ThirdParty

Todor Balabanov (via logerrit) logerrit at kemper.freedesktop.org
Sun Jun 20 01:59:46 UTC 2021


 nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/DEPSAgent.java             |    3 -
 nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/DEGTBehavior.java |    4 -
 nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/deps/behavior/PSGTBehavior.java |    7 +-
 nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java     |   24 ++++++++++
 4 files changed, 32 insertions(+), 6 deletions(-)

New commits:
commit ffef53274b44bfe77128ba400845f4c4ce70893e
Author:     Todor Balabanov <todor.balabanov at gmail.com>
AuthorDate: Fri Jun 18 10:46:33 2021 +0300
Commit:     Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Sun Jun 20 03:59:07 2021 +0200

    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 at gmail.com>

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 {
   /**
@@ -30,6 +31,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;


More information about the Libreoffice-commits mailing list