Particle.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package psoAlgoCode;
  2. import java.util.Vector;
  3. public class Particle {
  4. //Number of dimensions of the particle
  5. private int dim;
  6. //Value for the local best -> best value for this particle so far
  7. private double local_best;
  8. //Current value of the particle -> value in this iteration
  9. private double actual_value;
  10. //Each particle possesses a position and a velocity in every dimension
  11. private Coordinate<Vector<Object>> posAdvanced;
  12. private Coordinate<Vector<Object>> velAdvanced;
  13. //The corresponding position for the local best value
  14. private Coordinate<Vector<Object>> localBestPosAdv;
  15. /**
  16. * Create a particle
  17. * @param dimensions number of dimensions for this particle
  18. */
  19. public Particle(int dimensions) {
  20. this.dim = dimensions;
  21. posAdvanced = new Coordinate<Vector<Object>>();
  22. velAdvanced = new Coordinate<Vector<Object>>();
  23. local_best = Double.POSITIVE_INFINITY;
  24. actual_value = Double.POSITIVE_INFINITY;
  25. }
  26. // Getters
  27. public int getDimensions() {
  28. return dim;
  29. }
  30. public Coordinate<Vector<Object>> getPositionAdv() {
  31. return posAdvanced;
  32. }
  33. public Coordinate<Vector<Object>> getVelocityAdv() {
  34. return velAdvanced;
  35. }
  36. public Coordinate<Vector<Object>> getBestLocalPosAdv() {
  37. return localBestPosAdv;
  38. }
  39. public double getLocalBestValue() {
  40. return local_best;
  41. }
  42. public double getActualValue() {
  43. return actual_value;
  44. }
  45. // Setters
  46. public void setDimensions(int dim) {
  47. this.dim = dim;
  48. }
  49. public void setPositionAdv(Coordinate<Vector<Object>> new_pos) {
  50. this.posAdvanced = new_pos;
  51. }
  52. public void setVelocityAdv(Coordinate<Vector<Object>> new_vel) {
  53. this.velAdvanced = new_vel;
  54. }
  55. public void setActualValue(double v) {
  56. this.actual_value = v;
  57. }
  58. // Update Functions
  59. public void updateLocalBest() {
  60. if (actual_value < local_best) {
  61. this.local_best = this.actual_value;
  62. this.localBestPosAdv = this.posAdvanced;
  63. }
  64. }
  65. }