📄 particle.java
字号:
/* * Particle.java * * This is a required part of the com.adaptiveview.ospso package. * * Copyright (C) 2003 AdaptiveView.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * You may contact AdaptiveView.com via email at: comments.pso@adaptiveview.com * */package com.adaptiveview.ospso;import com.adaptiveview.toolkits.numbers.Randoms;/**A Particle "moves" through a solution space according to a move algorithm * which is "limited" by a number of contraints. A particle has a current location * and a "best so far" (most fit) location. During the move process, it will also * have a "next" location. * * @author AdaptiveView.com */public class Particle implements com.adaptiveview.ospso.dmi.DMI_GPL_License { private LocationSet locationSet; private ConstraintSet constraintSet; private IMoveAlgorithm moveAlgorithm; private int particleID; /** Creates a new instance of Particle */ public Particle(int particleID, ConstraintSet constraintSet, IMoveAlgorithm moveAlgorithm) { this.particleID = particleID; int dimensions = constraintSet.getDimensionsCount(); this.locationSet = new LocationSet(dimensions); this.constraintSet = constraintSet; this.moveAlgorithm = moveAlgorithm; setInitialNextLocation(dimensions); } /** Calculate the next location of this Particle */ public void calculateNextLocation(LocationChange neighborhoodBest) { moveAlgorithm.calculateMove(locationSet, constraintSet, neighborhoodBest); } /** Update the current location of this Particle (from calculated next location) */ public void updateCurrentLocation() { locationSet.updateLocation(); } /** Set the current location as the best location of this Particle */ public void setCurrentLocationAsBest() { locationSet.setBestLocation(); } /** Update the best[so far] location of this Particle */ public void updateBestLocation(Location best) { locationSet.setBestLocation(best); } /** Returns the fitness of the best[so far] location of this Particle */ protected double getBestLocationFitness() { return locationSet.getBestLocation().getFitness(); } /** Returns the fitness of the best[so far] location of this Particle */ protected LocationChange getBestLocationChange() { return locationSet.getBestLocation().getLocationChange(); } /** Returns the fitness of the best[so far] location of this Particle */ protected LocationChange getCurrentLocationChange() { return locationSet.getCurrentLocation().getLocationChange(); } /** Sets the fitness of the current location of this Particle */ protected void setCurrentLocationFitness(double fitness) { locationSet.getCurrentLocation().setFitness(fitness); } /** Called by the constructor. */ private void setInitialNextLocation(int dimensions) { double[] velocities = new double[dimensions]; // let default to 0.0 // Set coordinates to "constrained" random values Constraint c; double[] coordinates = new double[dimensions]; for (int d = 0; d < dimensions; d++) { c = constraintSet.getConstraint(d + 1); coordinates[d] = Randoms.doubleInRange(c.getMinimum(), c.getMaximum()); } // Set the next location's coordinates and velocities (no fitness) locationSet.setNextLocation(new Location(coordinates,velocities)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -