RandomFunctions.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package psoAlgoCode;
  2. import java.util.Random;
  3. import java.util.Vector;
  4. public class RandomFunctions {
  5. static Random random = new Random();
  6. public static Vector<Object> nextDoubles(int dimension) {
  7. Vector<Object> result = new Vector<Object>();
  8. for (int i = 0; i < dimension; i++) {
  9. result.addElement(Function.lowerLimit + random.nextDouble() * (Function.upperLimit * 2));
  10. }
  11. return result;
  12. }
  13. public static Vector<Object> nextRandoms(int dimension) {
  14. Vector<Object> result = new Vector<Object>();
  15. for (int i = 0; i < dimension; i++) {
  16. result.addElement(random.nextDouble());
  17. }
  18. return result;
  19. }
  20. public static Vector<Object> nextBoolean(int dimension) {
  21. Vector<Object> result = new Vector<Object>();
  22. for (int i = 0; i < dimension; i++) {
  23. result.addElement(random.nextBoolean());
  24. }
  25. return result;
  26. }
  27. // The paper Modified binary particle swarm optimization from S. Lee states that
  28. // phi is a random number between 2.4 and 2.01
  29. public static double randomPhi() {
  30. double result = 0.0;
  31. result = (random.nextDouble() * (2.4 - 2.01)) + 2.01;
  32. return result;
  33. }
  34. }