📄 testpso.java
字号:
/* * TestPSO.java * * Created on March 10, 2003, 2:35 PM */package com.adaptiveview.ospso.test;import com.adaptiveview.ospso.*;/**This Class contains a main method for testing the PSO implementation. As * originally set up, you just need to change which of the test fitness * algorithms are to be used and then rebuild and run it. * *<p> See comments in the main method for more detailed information. * * @author AdaptiveView.com */public class TestPSO { /** Cannot create a new instance of TestPSO */ private TestPSO() { } /**Tests/demonstrates the PSO implementation. * @param args the command line arguments (not used) */ public static void main(String[] args) { int particles = 40; // how many particles in the swarm (typically 20-40) /* ================================================================================ * Change the fitness algorithm to run the swarm against different problems. * * The iterations value controls how many times the swarm will move each particle. * * The exitConstraint defines how close to 0.0 the fitness for a particle's * coordinates must be to consider problem "solved." Note that in our implementation, * higher fitness values are always better and we, as a matter of practice, revise * fitness values using: <code>fitness = 0.0 - Math.abs(fitness);</code>, making * 0 (zero) the optimum. * * Keep in mind that the code is using floating point (double) values -- you are * unlikely to get 100% accurate solutions, especially when a problem requires * division. An exit constraint of from -1.0E-5 to 1.0E-5 * (-1/100000 <= fitness <= 1/100000) is more than adequate for most applications. * * Note that for algorithms F2 and F5 the swarm usually can't deliver a fitness * value < 1.0E-5 (at least during our tests). F5 is quite difficult due to the * topology of its solution space (a compressed sine wave with an amplitude that * increases exponentially, but slowly, as it nears the y-axis). We supply a * non-standard move algorithm you can instantiate (below) that does extremely * well on F5, but not as well on other problems. * ================================================================================ * There are 5 test problems included in the download: * * FitnessAlgorithmF1: x^2 + y^2 + z^2 + w^2 == 0.0 . . . . . . . * FitnessAlgorithmF2: 5(x^2) + 2*(y^3) - (z/w)^2 + 4 == 0.0 . . : * FitnessAlgorithmF3: x + y + (z * w))^2 == 0.0 . . . . . . . . : * FitnessAlgorithmF4: Math.PI - (x/y) == 0.0 . . . . . . . . . : * FitnessAlgorithmF5: Schaffer's Test Function 6 . . . . . . . : * : * ===============================================================V================ */ IFitnessAlgorithm fitnessAlgorithm = new FitnessAlgorithmF4(); int iterations = 10000; // # of times Swarm moves all particles Constraint exitConstraint = new Constraint(-1.0E-6,1.0E-6); // 1/1000000 int neighborhoodSize = 5; // applies only when CIRCLE topology is used/* ================================================================================ * The following 3 lines set up the neighborhood topology. Uncomment just the one you * want to use. In general, the Circle topology (the first line) works best. GLOBAL * usually results in the worst performance. * ================================================================================*/ INeighborhoodTopology topology = new NeighborhoodTopology(particles, neighborhoodSize);// INeighborhoodTopology topology = new NeighborhoodTopology(particles, "STAR");// INeighborhoodTopology topology = new NeighborhoodTopology(particles, "GLOBAL"); /* ================================================================================ * **** You should not need to change anything below this line, **** * **** but read the note about the move algorithm, below ... **** * ================================================================================*//* ================================================================================ * Changing the individuality, sociality or velocity constraints is NOT RECOMMENDED. * ================================================================================*/ StochasticFactor i,s; // individuality and sociality i = new StochasticFactor(0.0,1.0,2.0); s = new StochasticFactor(0.0,1.0,2.0); Constraint vConstraint = new Constraint(-4.0,4.0); // velocity constraint int dimensions = fitnessAlgorithm.getDimensions(); // # of unknowns in problem Constraint dConstraint = fitnessAlgorithm.getDimensionConstraint(); ConstraintSet cSet = new ConstraintSet(dimensions,dConstraint,vConstraint,s,i); LocationSet lSet = new LocationSet(dimensions); /* ================================================================================ * If the swarm gets close to an answer but never quite finds it, try using the * "WithClamping" move algorithm, instead. This isn't the standard (original) PSO move * algorithm. It clamps down on the velocity constraints (meaning it forces smaller * moves) as the particle approaches the current "neighborhood best" location along * any dimension. For example, try FitnessAlgorithmF5 with and without clamping * using an exit constraint of -1.0E-16,1.0E-16. * ================================================================================ */ IMoveAlgorithm moveAlgorithm = new MoveAlgorithm(lSet, cSet);// IMoveAlgorithm moveAlgorithm = new MoveAlgorithmWithClamping(lSet, cSet); Swarm swarm = new Swarm(particles, fitnessAlgorithm, topology); swarm.addParticles(particles, cSet, moveAlgorithm); // add all of them at once. swarm.setInitialFitnessValues(); // let swarm know we're done adding them. /* =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= * begin removable code (also see: Swarm.java) * =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= */System.out.println("Problem: " + fitnessAlgorithm.toString()); System.out.print("Swarm Parameters: " + iterations + " maximum iterations with " + particles + " particles using a " + topology.getTopology());if (topology.getTopology() == "Circle") System.out.print(" (neighborhood size = " + neighborhoodSize + ")");System.out.println(" topology.");System.out.println("Exit constraint: " + exitConstraint.getMinimum() + " <= fitness <= " + exitConstraint.getMaximum());if (moveAlgorithm instanceof MoveAlgorithmWithClamping) System.out.println("Note: Using non-standard Move Algorithm (velocity clamping).");/* =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= * end removable code * =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= */ boolean isSolved = swarm.iterate(iterations, exitConstraint);/* =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= * begin removable code * =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= */String exitMsg = " an answer with a fitness within the exit constraints.";if (isSolved) System.out.println("Found" + exitMsg); else System.out.println("Did not find" + exitMsg);/* =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= * end removable code * =#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#==#= */ } // end of main method. }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -