Function.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package psoAlgoCode;
  2. import ui.controller.Control;
  3. import ui.model.Model;
  4. public class Function {
  5. public static String name;
  6. private int desiredFunction;
  7. private int type;
  8. public static double upperLimit;
  9. public static double lowerLimit;
  10. public Function() {
  11. super();
  12. name = "";
  13. desiredFunction = Constants.function;
  14. upperLimit = 0;
  15. lowerLimit = 0;
  16. type = 0;
  17. }
  18. /*public void setParameters(Object obj) {
  19. if (obj.getClass().equals(Boolean.class)) {
  20. name = "Boolean function";
  21. // upperLimit = 500;
  22. // lowerLimit = -500;
  23. type = 0;
  24. } else if (obj.getClass().equals(Double.class) && desiredFunction == 0) {
  25. name = "Sphere function";
  26. upperLimit = 100;
  27. lowerLimit = 50;
  28. type = 1;
  29. } else if (obj.getClass().equals(Double.class) && desiredFunction == 1) {
  30. name = "Rastrigin function";
  31. upperLimit = 5.12;
  32. lowerLimit = 2.56;
  33. type = 1;
  34. } else if (obj.getClass().equals(Double.class) && desiredFunction == 2) {
  35. name = "Griewank function";
  36. upperLimit = 600;
  37. lowerLimit = 300;
  38. type = 1;
  39. }
  40. }*/
  41. public double execute(Particle p, int index, Model model, Control control) {
  42. double result = 0.0;
  43. if (type == 0) {
  44. result = executeSmartGrid(p, index, model, control);
  45. } /*else if (type == 1 && desiredFunction == 0) {
  46. result = executeSphere(p, index);
  47. } else if (type == 1 && desiredFunction == 1) {
  48. result = executeRastrigin(p, index);
  49. } else if (type == 1 && desiredFunction == 2) {
  50. result = executeGriewank(p, index);
  51. }*/
  52. return result;
  53. }
  54. private static double executeSmartGrid(Particle p, int index, Model model, Control control) {
  55. double result = 0.0;
  56. SGFunctions sg = new SGFunctions(p, model, control);
  57. result = sg.calculateFitness();
  58. return result;
  59. }
  60. /*private static double executeSphere(Particle p, int index) {
  61. double result = 0.0;
  62. for (int dim = 0; dim < p.getDimensions(); dim++) {
  63. for (int i = 0; i < p.getPositionAdv().getCoord(dim).size(); i++) {
  64. double temp = (double) p.getPositionAdv().getCoord(dim).get(i);
  65. result += Math.pow(temp, 2);
  66. }
  67. }
  68. return result;
  69. }*/
  70. /*private static double executeRastrigin(Particle p, int index) {
  71. double result = 0.0;
  72. for (int dim = 0; dim < p.getDimensions(); dim++) {
  73. for (int i = 0; i < p.getPositionAdv().getCoord(dim).size(); i++) {
  74. double temp = (double) p.getPositionAdv().getCoord(dim).get(i);
  75. result += (Math.pow(temp, 2) - (10 * Math.cos(2 * Math.PI * temp)) + 10);
  76. }
  77. }
  78. return result;
  79. }*/
  80. /*
  81. private static double executeGriewank(Particle p, int index) {
  82. double sum = 0.0;
  83. double mult = 1.0;
  84. double result = 0.0;
  85. for (int dim = 0; dim < p.getDimensions(); dim++) {
  86. for (int i = 0; i < p.getPositionAdv().getCoord(dim).size(); i++) {
  87. double temp = (double) p.getPositionAdv().getCoord(dim).get(i);
  88. sum += Math.pow(temp, 2) / 4000;
  89. mult *= Math.cos(temp / Math.sqrt(index));
  90. }
  91. }
  92. result = sum - mult + 1;
  93. return result;
  94. }
  95. */
  96. }