From b1af8e5008a8dad4832827bafabff38c40d6915d Mon Sep 17 00:00:00 2001 From: Ludovic Apvrille <ludovic.apvrille@telecom-paristech.fr> Date: Thu, 28 May 2020 18:08:44 +0200 Subject: [PATCH] Adding gaussian distribution law to time intervals --- .../avatartranslator/AvatarTransition.java | 8 +- .../AvatarSimulationPendingTransaction.java | 8 +- src/main/java/myutil/MyMath.java | 30 ++++++- .../ui/window/JDialogAvatarTransition.java | 5 +- ttool/src/test/java/myutil/MyMathTest.java | 84 +++++++++++++++++++ 5 files changed, 127 insertions(+), 8 deletions(-) create mode 100755 ttool/src/test/java/myutil/MyMathTest.java diff --git a/src/main/java/avatartranslator/AvatarTransition.java b/src/main/java/avatartranslator/AvatarTransition.java index aed1609584..d87f09174f 100644 --- a/src/main/java/avatartranslator/AvatarTransition.java +++ b/src/main/java/avatartranslator/AvatarTransition.java @@ -56,10 +56,12 @@ public class AvatarTransition extends AvatarStateMachineElement { // Delay distribution laws public final static int DELAY_UNIFORM_LAW = 0; public final static int DELAY_TRIANGULAR_LAW = 1; - public final static String[] DISTRIBUTION_LAWS = {"Uniform", "Triangular"}; - public final static String[] DISTRIBUTION_LAWS_SHORT = {"", " ^"}; + public final static int DELAY_GAUSSIAN_LAW = 2; + public final static String[] DISTRIBUTION_LAWS = {"Uniform", "Triangular", "Gaussian"}; + public final static String[] DISTRIBUTION_LAWS_SHORT = {"", " ^", "Ä"}; - public final static int[] NB_OF_EXTRA_ATTRIBULTES = {0, 1}; + public final static int[] NB_OF_EXTRA_ATTRIBULTES = {0, 1, 1}; + public final static String[] LABELS_OF_EXTRA_ATTRIBULTES = {"", "triangle top", "standard deviation"}; // Type management: to be used by code generators public static final int UNDEFINED = -1; diff --git a/src/main/java/avatartranslator/directsimulation/AvatarSimulationPendingTransaction.java b/src/main/java/avatartranslator/directsimulation/AvatarSimulationPendingTransaction.java index 510b4d8f91..b325baa03d 100644 --- a/src/main/java/avatartranslator/directsimulation/AvatarSimulationPendingTransaction.java +++ b/src/main/java/avatartranslator/directsimulation/AvatarSimulationPendingTransaction.java @@ -261,13 +261,17 @@ public class AvatarSimulationPendingTransaction { public void makeRandomDelay() { switch (delayDistributionLaw) { case AvatarTransition.DELAY_UNIFORM_LAW: - TraceManager.addDev("\n\n\n******* UNIFORM LAW ********"); + //TraceManager.addDev("\n\n\n******* UNIFORM LAW ********"); selectedDuration = myMinDuration + (int) (Math.floor(Math.random() * (maxDuration - myMinDuration))); return; case AvatarTransition.DELAY_TRIANGULAR_LAW: - TraceManager.addDev("\n\n\n******* TRIANGULAR LAW ********"); + //TraceManager.addDev("\n\n\n******* TRIANGULAR LAW ********"); selectedDuration = (int) (MyMath.triangularDistribution((double) (myMinDuration), (double) (maxDuration), extraParam1)); return; + case AvatarTransition.DELAY_GAUSSIAN_LAW: + //TraceManager.addDev("\n\n\n******* GAUSSIAN LAW ********"); + selectedDuration = (int)(Math.floor(MyMath.gaussianDistribution((double) (myMinDuration), (double) (maxDuration), extraParam1))); + return; } } diff --git a/src/main/java/myutil/MyMath.java b/src/main/java/myutil/MyMath.java index d5b5da6cc9..20b38db7ff 100644 --- a/src/main/java/myutil/MyMath.java +++ b/src/main/java/myutil/MyMath.java @@ -41,6 +41,8 @@ package myutil; +import java.util.Random; + /** * Class MyMath * Creation: 08/02/2012 @@ -91,6 +93,8 @@ public class MyMath { } // Triangular distribution + // a,b: interval min and max + // c: std deviation public static double triangularDistribution(double a, double b, double c) { double F = (c - a) / (b - a); double rand = Math.random(); @@ -101,7 +105,29 @@ public class MyMath { } } - - + // Gaussian distribution + // a: min of interval + // b : max of interval + // c: standard deviation + public static double gaussianDistribution(double a, double b, double c) { + Random r = new Random(); + double n = r.nextGaussian(); + //System.out.println("1. n=" + n); + n = n * c; + //System.out.println("1.1 n=" + n); + n = n + (b+a)/2; + //System.out.println("1.2 n=" + n); + if (n < a) { + return gaussianDistribution(a, b, c); + } + //System.out.println("1.3 n=" + n); + if (n>b) { + return gaussianDistribution(a, b, c); + } + //n = Math.max(a, n); + //System.out.println("2. n=" + n); + return n; + } + } diff --git a/src/main/java/ui/window/JDialogAvatarTransition.java b/src/main/java/ui/window/JDialogAvatarTransition.java index 7bc64383d9..5277970a84 100644 --- a/src/main/java/ui/window/JDialogAvatarTransition.java +++ b/src/main/java/ui/window/JDialogAvatarTransition.java @@ -107,6 +107,7 @@ public class JDialogAvatarTransition extends JDialogBase implements ActionListen // Panel1 private JTextField guardT, afterMinT, afterMaxT, extraDelay1T, /*computeMinT, computeMaxT,*/ probabilityT; + private JLabel extraDelay1L; private JComboBox<String> distributionLawB; private JTable actionsTable; @@ -291,7 +292,8 @@ public class JDialogAvatarTransition extends JDialogBase implements ActionListen distributionLawB.addActionListener(this); pnlTransitionInfo.add(distributionLawB, constraintsFields ); - pnlTransitionInfo.add(new JLabel("Attr 1:", SwingConstants.RIGHT ), constraintsLabels ); + extraDelay1L = new JLabel("Attr 1:", SwingConstants.RIGHT); + pnlTransitionInfo.add(extraDelay1L, constraintsLabels ); extraDelay1T = new JTextField(extraDelay1, 10); constraintsFields.gridwidth = GridBagConstraints.REMAINDER;; constraintsFields.insets.right = 0; @@ -811,6 +813,7 @@ public class JDialogAvatarTransition extends JDialogBase implements ActionListen distributionLaw = distributionLawB.getSelectedIndex(); int nbOfExtras = AvatarTransition.NB_OF_EXTRA_ATTRIBULTES[distributionLaw]; extraDelay1T.setEnabled(nbOfExtras>0); + extraDelay1L.setText(AvatarTransition.LABELS_OF_EXTRA_ATTRIBULTES[distributionLaw] + ":"); } } diff --git a/ttool/src/test/java/myutil/MyMathTest.java b/ttool/src/test/java/myutil/MyMathTest.java new file mode 100755 index 0000000000..d8972bcf30 --- /dev/null +++ b/ttool/src/test/java/myutil/MyMathTest.java @@ -0,0 +1,84 @@ +/**Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille + + ludovic.apvrille AT enst.fr + + This software is a computer program whose purpose is to allow the + edition of TURTLE analysis, design and deployment diagrams, to + allow the generation of RT-LOTOS or Java code from this diagram, + and at last to allow the analysis of formal validation traces + obtained from external tools, e.g. RTL from LAAS-CNRS and CADP + from INRIA Rhone-Alpes. + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. +*/ +package myutil; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class MyMathTest { + + @Test + public void testGaussianDistributionLaw() { + System.out.println("Testing gaussian distribution law between 1 and 10"); + + int[] tab = new int[10]; + + int loop = 10000; + double average = 0; + double a = 1.0; + double b = 10.0; + + for(int i=0; i<loop; i++) { + double d = MyMath.gaussianDistribution(a-0.4999, b+0.49999, 2.0); + average += d; + int r = (int)(Math.round(d)); + //System.out.println("d=" + d + " r=" + r); + //assertTrue(r>=a); + //assertTrue(r<=b); + tab[r-1]++; + } + + System.out.println("Results of gaussian test:"); + for(int i=0; i<tab.length; i++) { + System.out.println("tab[" + (i+1) + "]=" + tab[i]); + } + average = average / loop; + System.out.println("Average:" + average); + + assertTrue((int)average > ((b-a)/2) - 1); + assertTrue((int)average < ((b-a)/2) + 1); + + + + + } + + +} -- GitLab