📄 locationset.java
字号:
/* * LocationSet.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.arrays.CircularIndex;import com.adaptiveview.toolkits.arrays.ToStringBuffer;/**A container for the <CODE>next</CODE>, <CODE>current</CODE> and <CODE>best</CODE> * <CODE>Location</CODE> objects that supports getting references * and copies of these locations and setting these locations. * * @author AdaptiveView.com */public class LocationSet implements com.adaptiveview.ospso.dmi.DMI_GPL_License { private Location best; private Location[] locations; private CircularIndex ci; private int dimensions = 0; /** Creates a new instance of LocationSet * @param dimensions The number of dimensions in the solution space. * Becomes the size of the <CODE>Location</CODE> arrays. */ public LocationSet(int dimensions) { this.dimensions = dimensions; locations = new Location[2]; // current and next ci = new CircularIndex(2); // current and next } /** Stores a <I>copy</I> of the arg1 <CODE>Location</CODE> into the <CODE>locations</CODE> * array at the index returned from the <CODE>CircularIndex</CODE> object. * @param newLocation The new Location to be copied. */ protected void setNextLocation(Location newLocation) { locations[ci.getNext()] = newLocation.getCopyOfLocation(); } /** Stores a <I>copy of the current</I> <CODE>Location</CODE> into the * <CODE>best Location</CODE>. */ protected void setBestLocation() { best = getCopyOfCurrentLocation(); } /** Stores a <I>copy</I> of the arg1 <CODE>Location</CODE> into the * <CODE>best Location</CODE>. * @param newLocation The new Location to be copied. */ protected void setBestLocation(Location newLocation) { best = newLocation.getCopyOfLocation(); } /** Returns a <I>reference</I> to the <CODE>Location</CODE> object in the * <CODE>locations</CODE> array pointed to by the * <CODE>getNext()</CODE> method of the <CODE>CircularIndex</CODE> object. * @return a <I>reference</I> to the <CODE>next Location</CODE> object. */ protected Location getNextLocation() { return locations[ci.getNext()]; } /** Returns a <I>reference</I> to the <CODE>Location</CODE> object in the * <CODE>locations</CODE> array pointed to by the * <CODE>getCurrent()</CODE> method of the <CODE>CircularIndex</CODE> object. * @throws IllegalStateException if no <CODE>current</CODE> <CODE>Location</CODE> has been saved (via <CODE>updateLocations()</CODE>) * @return a <I>reference</I> to the <CODE>current Location</CODE> object. */ protected Location getCurrentLocation() throws IllegalStateException { return locations[ci.getCurrent()]; } /** Returns a <I>reference</I> to the <CODE>best Location</CODE> object. * @return a <I>reference</I> to the <CODE>best Location</CODE> object. */ protected Location getBestLocation() { return best; } /** Returns a specified number (<CODE>arg1</CODE> <CODE>count</CODE>) of <CODE>Location</CODE> objects in * "most recent to oldest" order. Does <B>not</B> return the <CODE>next</CODE> Location. * <B>Note</B>: The number of returned Locations will depend on how many Locations * have been saved via <CODE>updateLocation()</CODE> ans, therefore, * may not match the number requested in <CODE>arg1</CODE>. * @param count Number of Locations to return. * @throws IllegalStateException if there is no <CODE>current Location</CODE>. * @return a <CODE>Location</CODE> array with most recent locations * (depending upon how many were requested). */ protected Location[] getLastLocations(int count) throws IllegalStateException { int[] last = ci.getLast(count); int n = last.length; Location[] lastLocations = new Location[n]; for (int i = 0; i < n; i++) { lastLocations[i] = locations[last[i]]; } return lastLocations; } /** Sends <CODE>saveNext()</CODE> message to the <CODE>CircularIndex</CODE> object which, * in effect, makes the <CODE>next</CODE> location <CODE>current</CODE> and sets the * <CODE>next</CODE> pointer to the following <CODE>locations</CODE> array index. */ protected void updateLocation() { ci.saveNext(); } /** Same as <CODE>getNextLocation()</CODE> except a copy is returned instead of a reference. * @return Same as <CODE>getNextLocation()</CODE> except a copy is returned instead of a reference. */ public Location getCopyOfNextLocation() { return locations[ci.getNext()].getCopyOfLocation(); } /** Same as <CODE>getCurrentLocation()</CODE> except a copy is returned instead of a reference. * @throws IllegalStateException if there is no <CODE>current Location</CODE>. * @return Same as <CODE>getCurrentLocation()</CODE> except a copy is returned instead of a reference. */ public Location getCopyOfCurrentLocation() throws IllegalStateException { return locations[ci.getCurrent()].getCopyOfLocation(); } /** Same as <CODE>getBestLocation()</CODE> except a copy is returned instead of a reference. * @return Same as <CODE>getBestLocation()</CODE> except a copy is returned instead of a reference. */ public Location getCopyOfBestLocation() { return best.getCopyOfLocation(); } /** Same as <CODE>getLastLocations()</CODE> except a copies are returned instead of a references. * @param count Number of Locations to return. * @throws IllegalStateException if no <CODE>current Location</CODE>. * @return Same as <CODE>getLastLocations()</CODE> except a copies are returned instead of a references. */ public Location[] getCopyOfLastLocations(int count) throws IllegalStateException { int[] last = ci.getLast(count); int n = last.length; Location[] lastLocations = new Location[n]; for (int i = 0; i < n; i++) { lastLocations[i] = locations[last[i]].getCopyOfLocation(); } return lastLocations; } /** Returns the number of dimensions (in solution space) defined for all Locations. * @return number of dimensions. */ public int getDimensionsCount() { return dimensions; } /** Overrides <CODE>toString()</CODE> returning a string with formatted list of * <CODE>current</CODE> and <CODE>best</CODE> Locations. * @return a string with formatted list of <CODE>current</CODE> and <CODE>best</CODE> Locations. */ public String toString() { StringBuffer s = new StringBuffer(); s.append("Current: \n"); s.append(locations[ci.getCurrent()].toString()); s.append("Best: \n"); s.append(best.toString()); return s.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -