PoissonDistributionHandler.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JTextField;
  8. import org.apache.commons.math3.distribution.NormalDistribution;
  9. import org.apache.commons.math3.distribution.PoissonDistribution;
  10. import org.apache.commons.math3.random.RandomGenerator;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
  12. /**
  13. * Example class representing a Normal Distribution
  14. *
  15. * @author Andreas T. Meyer-Berg
  16. */
  17. public class PoissonDistributionHandler implements
  18. ProbabilityDistributionHandler {
  19. /**
  20. * Poisson Mean value
  21. */
  22. private double p;
  23. /**
  24. * Standard Deviation
  25. */
  26. private double epsilon;
  27. /**
  28. * Normal Distribution
  29. */
  30. private PoissonDistribution dist;
  31. /**
  32. *
  33. */
  34. private RandomGenerator gen = null;
  35. /**
  36. * Creates a new distribution which returns the given value
  37. * @param value value to be returned
  38. */
  39. public PoissonDistributionHandler(double p, double epsilon) {
  40. this.p = p;
  41. this.epsilon = epsilon;
  42. dist = new PoissonDistribution(p, epsilon);
  43. }
  44. /**
  45. * Creates a default distribution, which returns 50.
  46. */
  47. public PoissonDistributionHandler() {
  48. this.p = 50.0;
  49. this.epsilon = 12.0;
  50. dist = new PoissonDistribution(p, epsilon);
  51. }
  52. @Override
  53. public void setRandomGenerator(RandomGenerator rng) {
  54. gen = rng;
  55. dist = new PoissonDistribution(rng, p, epsilon, 100);
  56. }
  57. @Override
  58. public long sampleNextValue() {
  59. return (long)Math.round(dist.sample());
  60. }
  61. @Override
  62. public JPanel getConfigurationPanel() {
  63. /**
  64. * JPanel which allows configuration of this Distribution
  65. */
  66. JPanel panel = new JPanel();
  67. panel.setMinimumSize(new Dimension(90, 80));
  68. panel.setLayout(null);
  69. /**
  70. * Label
  71. */
  72. JLabel label = new JLabel("p(Mean): ");
  73. panel.add(label);
  74. label.setLocation(20, 20);
  75. label.setSize(100,20);
  76. label.setMinimumSize(new Dimension(50, 20));
  77. /**
  78. * Textfield for changing the value
  79. */
  80. JTextField valueConfig = new JTextField(""+p);
  81. panel.add(valueConfig);
  82. valueConfig.setLocation(130, 20);
  83. valueConfig.setMinimumSize(new Dimension(70, 20));
  84. valueConfig.setSize(70, 20);
  85. valueConfig.addActionListener(a->{
  86. try {
  87. /**
  88. * Update the value
  89. */
  90. p = Double.parseDouble(valueConfig.getText());
  91. initializeDistribution();
  92. valueConfig.setBackground(Color.WHITE);
  93. } catch (Exception e) {
  94. valueConfig.setBackground(Color.RED);
  95. }
  96. });
  97. /**
  98. * Label
  99. */
  100. JLabel lbSD = new JLabel("epsilon: ");
  101. panel.add(lbSD);
  102. lbSD.setLocation(20, 50);
  103. lbSD.setSize(100,20);
  104. lbSD.setMinimumSize(new Dimension(50, 20));
  105. /**
  106. * Textfield for changing the value
  107. */
  108. JTextField tfSDvalue = new JTextField(""+epsilon);
  109. panel.add(tfSDvalue);
  110. tfSDvalue.setLocation(130, 50);
  111. tfSDvalue.setMinimumSize(new Dimension(70, 20));
  112. tfSDvalue.setSize(70, 20);
  113. tfSDvalue.addActionListener(a->{
  114. try {
  115. /**
  116. * Update the value
  117. */
  118. epsilon = Double.parseDouble(tfSDvalue.getText());
  119. tfSDvalue.setBackground(Color.WHITE);
  120. } catch (Exception e) {
  121. tfSDvalue.setBackground(Color.RED);
  122. }
  123. });
  124. return panel;
  125. }
  126. private void initializeDistribution() {
  127. if(gen==null)
  128. dist = new PoissonDistribution(p, epsilon);
  129. else
  130. dist = new PoissonDistribution(gen, p, epsilon, 100);
  131. }
  132. public static void main(String[] args) {
  133. PoissonDistributionHandler c = new PoissonDistributionHandler(12.0, 5.0);
  134. JFrame test = new JFrame("test");
  135. test.setSize(400, 400);
  136. test.add(c.getConfigurationPanel());
  137. test.setEnabled(true);
  138. test.setVisible(true);
  139. }
  140. @Override
  141. public String getSimpleDescription() {
  142. return "Normal Distribution";
  143. }
  144. }