📄 movealgorithm.java
字号:
/* * MoveAlgorithm.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;import com.adaptiveview.toolkits.numbers.Range;import com.adaptiveview.ospso.dmi.*;/**Each particle has a reference to an <CODE>IMoveAlgorithm</CODE> instance used to * change its location. This method is invoked by the <CODE>Swarm</CODE> instance to * which this particle "belongs." * * @author AdaptiveView.com */public class MoveAlgorithm implements IMoveAlgorithm, DMI_AboutNeighborhoods, com.adaptiveview.ospso.dmi.DMI_GPL_License { private LocationSet locationSet; private ConstraintSet constraintSet; private double velocityMin, velocityMax; private double dimensionMin, dimensionMax; private double iMin, iMax, iWeight; private double sMin, sMax, sWeight; private int dimensionsCount; private double nextVelocity, nextCoordinate, currentCoordinate; /**Creates a new instance of MoveAlgorithm using the Particle's location * and contraints information. * * @param locationSet the LocationSet instance holds the <CODE>current</CODE>, <CODE>next</CODE> * and <CODE>best</CODE> locations for the particle. * @param ConstraintSet the ConstraintSet instance holds upper/lower bounds * of each dimension, the method to use if a change in * position exceeds those bounds, and the minimum and maximum * allowable changes in position along each dimension. */ public MoveAlgorithm(LocationSet locationSet, ConstraintSet constraintSet) { this.locationSet = locationSet; this.constraintSet = constraintSet; storeParticleConstraints(); } /** Sets <code>nextLocation</code> in LocationSet instance per Move Algorithm * * @param lSet a reference to the particle's LocationSet instance * @param cSet a reference to the particle's ConstraintSet instance * @param neighborhoodBest the LocationChange (location and velocity) of a * particle in this particle's neighborhood (possibly <I>this</I> particle) * which has the best fitness in the neighborhood. */ public void calculateMove(LocationSet lSet, ConstraintSet cSet, LocationChange neighborhoodBest) { LocationChange pCurrent = lSet.getCurrentLocation().getLocationChange(); double[] pBest = lSet.getBestLocation().getLocationChange().getCoordinates(); double[] nBest = neighborhoodBest.getCoordinates(); double[] nextC = new double[dimensionsCount]; // next coordinates double[] nextV = new double[dimensionsCount]; // next velocities for (int d = 0;d < dimensionsCount; d++) { currentCoordinate = pCurrent.getCoordinate(d); nextVelocity = getNextVelocity(currentCoordinate,pCurrent.getVelocity(d),pBest[d],nBest[d]); nextCoordinate = currentCoordinate + nextVelocity; dimensionMin = cSet.getConstraint(d + 1).getMinimum(); dimensionMax = cSet.getConstraint(d + 1).getMaximum(); switch (cSet.getConstraint(d + 1).getBoundaryType()) { case Constraint.BOUNCE: nextCoordinate = Range.bounce(nextCoordinate,dimensionMin,dimensionMax); break; case Constraint.WRAP: nextCoordinate = Range.wrap(nextCoordinate,dimensionMin,dimensionMax); break; case Constraint.STICK: nextCoordinate = Range.stick(nextCoordinate,dimensionMin,dimensionMax); break; default: break; } nextV[d] = nextVelocity; nextC[d] = nextCoordinate; } lSet.setNextLocation(new Location(nextC, nextV)); } /** Returns the next velocity for a particle dimension; this is essentially * the "heart" of the standard (type 1) PSO algorithm. It applies the individuality * and sociality factors to the distances, respectively, between the particle's * current position along this dimension and its position when the particle coordinates * resulted in its (personal) best fitness and the position along this dimension of * the particle's neighbor when its coordinates resulted in the neighborhood best fitness. * * @param currentLocation the particle's current position along a dimension * @param currentVelocity the velocity along the dimension with which the current * location was approached * @param personalBest the position along the dimension at which this particle * "found its best fitness" * @param neighborhoodBest the position along the dimension at which a neighbor * of this particle "found the neighborhood's best fitness" * @return the next velocity for a particle dimension */ public double getNextVelocity(double currentCoordinate, double currentVelocity, double personalBest, double neighborhoodBest) { double iFactor = iWeight * Randoms.doubleInRange(iMin,iMax); // stochastic individuality double sFactor = sWeight * Randoms.doubleInRange(sMin,sMax); // stochastic sociality//iFactor = 4.0 - sFactor; double pDelta = personalBest - currentCoordinate; // distance to personal best double nDelta = neighborhoodBest - currentCoordinate; // distance to neighborhood best double delta = (iFactor * pDelta) + (sFactor * nDelta); // apply stochastic factors/* System.out.print("current:" + currentCoordinate); System.out.print("; personalBest:" + personalBest); System.out.print("; neighborhoodBest:" + neighborhoodBest); System.out.print("; pDelta:" + pDelta); System.out.print("; nDelta:" + nDelta); System.out.print("; iFactor:" + iFactor); System.out.print("; sFactor:" + sFactor); System.out.print("; velocityDelta:" + delta);*/ delta = currentVelocity + delta; // add weighted velocity to current velocity/*System.out.print("; currentVelocity:" + currentVelocity); System.out.print("; nextVelocity:" + delta);*/ delta = Range.constrict(delta, velocityMin, velocityMax); // limit new velocity/* System.out.println("; nextVelocity (constricted):" + delta);*/ return delta; } /*Called by constructor; stores constraints applicable to the Particle's movement. */ private void storeParticleConstraints() { dimensionsCount = locationSet.getDimensionsCount(); // Velocity (movement) Constraints velocityMin = constraintSet.getVelocityConstraint().getMinimum(); velocityMax = constraintSet.getVelocityConstraint().getMaximum(); // Individuality Constraints (Stochastic Factors) iMin = constraintSet.getIndividuality().getMinimum(); iMax = constraintSet.getIndividuality().getMaximum(); iWeight = constraintSet.getIndividuality().getWeight(); // Sociality Constraints (Stochastic Factors) sMin = constraintSet.getSociality().getMinimum(); sMax = constraintSet.getSociality().getMaximum(); sWeight = constraintSet.getSociality().getWeight(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -