neighborhoodtopology.java
来自「使用java编写的关于通讯及串口编程所必需的类库」· Java 代码 · 共 146 行
JAVA
146 行
/* * NeighborhoodTopology.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.*;import com.adaptiveview.ospso.dmi.*;/**Handles neighborhood related methods; supports GLOBAL, STAR and CIRCLE * Neighborhood Topologies. * * @author AdaptiveView.com */public class NeighborhoodTopology implements INeighborhoodTopology, DMI_AboutNeighborhoods, com.adaptiveview.ospso.dmi.DMI_GPL_License { private int swarmSize; private int neighborhoodSize = 0; private String topology; /** Creates a new instance of Neighborhood with topology other than Circle. * @param topology a valid topology ID other than CIRCLE. * @param swarmSize the number of particles in the swarm. * @throws IllegalArgumentException if wrong topology or invalid swarm size. */ public NeighborhoodTopology(int swarmSize, String topology) throws IllegalArgumentException { if (topology.compareToIgnoreCase(CIRCLE) == 0) throw new IllegalArgumentException("Neighborhood size not specified. Required for Circle Topology."); if (!(topology.compareToIgnoreCase(GLOBAL) == 0 || topology.compareToIgnoreCase(STAR) == 0)) throw new IllegalArgumentException("Invalid Topology."); if (swarmSize < 2) throw new IllegalArgumentException("Swarm size must be greater than 1."); this.topology = topology; this.swarmSize = swarmSize; this.neighborhoodSize = (topology == STAR ? 0 : this.swarmSize); } /** Creates a new instance of Neighborhood with Circle topology. * @param swarmSize the number of particles in the swarm. * @param neighborhoodSize number of particles in a neighborhood. */ public NeighborhoodTopology(int swarmSize, int neighborhoodSize) { this(CIRCLE,swarmSize,neighborhoodSize); } /** Creates a new instance of Neighborhood with specified topology * @param topology a valid topology ID (use constants defined in INeighborhood) * @param swarmSize the number of particles in the swarm. * @param neighborhoodSize number of particles in a neighborhood. * @throws IllegalArgumentException if unknown topology, invalid swarm size or * the neighborhhod size is less than the swarm size. */ public NeighborhoodTopology(String topology, int swarmSize, int neighborhoodSize) throws IllegalArgumentException { if (!(topology.compareToIgnoreCase(GLOBAL) == 0 || topology.compareToIgnoreCase(STAR) == 0 || topology.compareToIgnoreCase(CIRCLE) == 0 )) throw new IllegalArgumentException("Invalid Topology."); if (swarmSize < 2) throw new IllegalArgumentException("Swarm size must be greater than 1."); if (neighborhoodSize > swarmSize) throw new IllegalArgumentException("Neighborhood size cannot be greater than Swarm size."); if (topology.compareToIgnoreCase(CIRCLE) == 0 && neighborhoodSize < 3) throw new IllegalArgumentException("Neighborhood size must be > 2."); this.topology = topology; this.swarmSize = swarmSize; if (topology.compareToIgnoreCase(CIRCLE) != 0 ) this.neighborhoodSize = (topology.compareToIgnoreCase(STAR) == 0 ? 0 : this.swarmSize); else this.neighborhoodSize = neighborhoodSize; } /** Returns an int array containing the IDs of a particle's neighbors. * @param particleID the ID of the particle (whose neighborhood) * @throws IllegalArgumentException if the arg1 particle ID is invalid. * @return an int array containing the IDs of a particle's neighbors. */ public int[] getNeighborIDs(int particleID) throws IllegalArgumentException { int[] neighbors; if (topology.compareToIgnoreCase(CIRCLE) == 0 ) { int count = neighborhoodSize - 1; neighbors = new int[neighborhoodSize]; for(int i = 0, n = particleID - (neighborhoodSize % 2);i < neighborhoodSize; i++) { neighbors[i] = Range.wrap(n++,0,count); } } else if (topology.compareToIgnoreCase(STAR) == 0 ) { if (particleID == 0) { neighbors = new int[swarmSize]; includeAllParticles(neighbors); } else { neighbors = new int[2]; neighbors[0] = 0; neighbors[1] = particleID; } } else if (topology.compareToIgnoreCase(GLOBAL) == 0 ) { neighbors = new int[swarmSize]; includeAllParticles(neighbors); } else // won't happen if constructors prevent invalid topologies. neighbors = new int[0]; return neighbors; } /** Returns a String (static final field) identifying the neighborhood topology type. * @return a String field identifying the neighborhood topology type. */ public String getTopology() { return this.topology; } /* Fills an int array with its index numbers. */ private final void includeAllParticles(int[] neighbors) { int s = neighbors.length; for (int i = 0; i < s; i++) { neighbors[i] = i; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?