📄 movealgorithmwithclamping.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.*;/**This Class is like MoveAlgorithm except that it clamps down on the velocity constraints * as a dimension location approaches that of the Neighborhood best.<p> * 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 MoveAlgorithmWithClamping 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 MoveAlgorithmWithClamping 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 MoveAlgorithmWithClamping(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 done in a manner similar to * the standard (type 1) PSO algorithm except that it clamps down on the velocity constraints * when the particle's location in a dimension is close (within +/-0.1) to that of the * particle with 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 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 delta = currentVelocity + delta; // add weighted velocity to current velocity double speedClamp; if (Math.abs(nDelta) < 0.1) { if (Math.abs(nDelta) < 0.01) speedClamp = Math.abs(nDelta); else speedClamp = 0.1; } else speedClamp = 1.0; delta = Range.constrict(delta, velocityMin * speedClamp, velocityMax * speedClamp); // limit new velocity 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 + -