Update of /cvsroot/pyxida/Pyxida/src/edu/harvard/syrah/pyxida/nc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1591/src/edu/harvard/syrah/pyxida/nc Modified Files: EWMAStatistic.java Coordinate.java Vec.java Added Files: NCClient.java NCClientIF.java Removed Files: VivaldiClient.java Log Message: Started adding the overall class framework for Pyxida Index: Coordinate.java =================================================================== RCS file: /cvsroot/pyxida/Pyxida/src/edu/harvard/syrah/pyxida/nc/Coordinate.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Coordinate.java 17 Nov 2006 14:03:41 -0000 1.1 --- Coordinate.java 20 Nov 2006 17:22:31 -0000 1.2 *************** *** 32,47 **** final protected double[] coords; ! final protected byte version; ! final protected int num_dims; - public static double MIN_COORD = 0.1; - - public byte getVersion () { - return version; - } - /** ! * Creates a copy of this <code>Coordinate</code> object, such that ! * updates in this coordinate are not reflected in the returned object. * * @return a copy of these coordinates --- 32,47 ---- final protected double[] coords; ! final protected byte version; ! final protected int num_dims; ! ! public static double MIN_COORD = 0.1; ! ! public byte getVersion() { ! return version; ! } /** ! * Creates a copy of this <code>Coordinate</code> object, such that updates ! * in this coordinate are not reflected in the returned object. * * @return a copy of these coordinates *************** *** 50,65 **** return new Coordinate(coords, true); } ! /** * Creates a new coordinate having a position at the origin. * * @param num_dimensions ! * the number of coordinate dimensions */ public Coordinate(int num_dimensions) { coords = new double[num_dimensions]; ! version = VivaldiClient.CURRENT_VERSION; ! if (VivaldiClient.USE_HEIGHT) num_dimensions--; ! num_dims = num_dimensions; } --- 50,66 ---- return new Coordinate(coords, true); } ! /** * Creates a new coordinate having a position at the origin. * * @param num_dimensions ! * the number of coordinate dimensions */ public Coordinate(int num_dimensions) { coords = new double[num_dimensions]; ! version = NCClient.CURRENT_VERSION; ! if (NCClient.USE_HEIGHT) ! num_dimensions--; ! num_dims = num_dimensions; } *************** *** 71,96 **** */ - public Coordinate(int num_dimensions, DataInputStream dis) throws IOException { - coords = new double[num_dimensions]; - version = dis.readByte(); - for (int i = 0; i < num_dimensions; i++) { - coords[i] = ((double) dis.readFloat()); - } - if (VivaldiClient.USE_HEIGHT) num_dimensions--; - num_dims = num_dimensions; - } ! public void toSerialized(DataOutputStream dos) throws IOException { ! final int num_dims = coords.length; ! dos.writeByte (version); ! for (int i = 0; i < num_dims; ++i) { ! // when writing, cast to float ! dos.writeFloat((float) coords[i]); ! } ! //if (VivaldiClient.USE_HEIGHT) dos.writeFloat((float) coords[num_dims]); ! } ! protected Coordinate(Coordinate c) { this(c.coords, true); --- 72,97 ---- */ + public Coordinate(int num_dimensions, DataInputStream dis) throws IOException { + coords = new double[num_dimensions]; + version = dis.readByte(); + for (int i = 0; i < num_dimensions; i++) { + coords[i] = ((double) dis.readFloat()); + } + if (NCClient.USE_HEIGHT) + num_dimensions--; + num_dims = num_dimensions; } ! public void toSerialized(DataOutputStream dos) throws IOException { ! final int num_dims = coords.length; ! dos.writeByte(version); ! for (int i = 0; i < num_dims; ++i) { ! // when writing, cast to float ! dos.writeFloat((float) coords[i]); ! } ! // if (VivaldiClient.USE_HEIGHT) dos.writeFloat((float) coords[num_dims]); ! } ! protected Coordinate(Coordinate c) { this(c.coords, true); *************** *** 99,131 **** /** * Creates a new coordinate having a position specified by the array ! * <code>init_coords</code>. The number of dimensions is this equal to ! * the array length. * * @param init_pos ! * the position for this coordinate * @param make_copy ! * whether a copy of the array should be made */ protected Coordinate(double[] init_pos, boolean make_copy) { ! int _num_dims = init_pos.length; ! if (make_copy) { coords = new double[_num_dims]; System.arraycopy(init_pos, 0, coords, 0, _num_dims); ! } ! else { coords = init_pos; } ! version = VivaldiClient.CURRENT_VERSION; ! if (VivaldiClient.USE_HEIGHT) _num_dims--; ! num_dims = _num_dims; } /** * Creates a new coordinate having a position specified by the array ! * <code>init_coords</code>. The number of dimensions is this equal to ! * the array length. * * @param init_pos ! * the position for this coordinate */ public Coordinate(float[] init_pos) { --- 100,132 ---- /** * Creates a new coordinate having a position specified by the array ! * <code>init_coords</code>. The number of dimensions is this equal to the ! * array length. * * @param init_pos ! * the position for this coordinate * @param make_copy ! * whether a copy of the array should be made */ protected Coordinate(double[] init_pos, boolean make_copy) { ! int _num_dims = init_pos.length; ! if (make_copy) { coords = new double[_num_dims]; System.arraycopy(init_pos, 0, coords, 0, _num_dims); ! } else { coords = init_pos; } ! version = NCClient.CURRENT_VERSION; ! if (NCClient.USE_HEIGHT) ! _num_dims--; ! num_dims = _num_dims; } /** * Creates a new coordinate having a position specified by the array ! * <code>init_coords</code>. The number of dimensions is this equal to the ! * array length. * * @param init_pos ! * the position for this coordinate */ public Coordinate(float[] init_pos) { *************** *** 135,165 **** coords[i] = init_pos[i]; } ! version = VivaldiClient.CURRENT_VERSION; ! if (VivaldiClient.USE_HEIGHT) _num_dims--; ! num_dims = _num_dims; } ! public boolean isCompatible (Coordinate _other) { ! if (version == _other.version) ! return true; ! return false; ! } ! ! public void bump () { ! for (int i = 0; i < coords.length; ++i) { ! if (Math.abs(coords[i]) < MIN_COORD) { ! double length = VivaldiClient.random.nextDouble()+MIN_COORD; ! // don't set height to be negative, if we are using it ! if ( (!VivaldiClient.USE_HEIGHT || i < coords.length-1) && ! (VivaldiClient.random.nextBoolean())) { ! length *= -1.; ! } ! coords[i] += length; ! } ! } } ! ! /** --- 136,164 ---- coords[i] = init_pos[i]; } ! version = NCClient.CURRENT_VERSION; ! if (NCClient.USE_HEIGHT) ! _num_dims--; ! num_dims = _num_dims; } ! public boolean isCompatible(Coordinate _other) { ! if (version == _other.version) ! return true; ! return false; } ! public void bump() { ! for (int i = 0; i < coords.length; ++i) { ! if (Math.abs(coords[i]) < MIN_COORD) { ! double length = NCClient.random.nextDouble() + MIN_COORD; ! // don't set height to be negative, if we are using it ! if ((!NCClient.USE_HEIGHT || i < coords.length - 1) ! && (NCClient.random.nextBoolean())) { ! length *= -1.; ! } ! coords[i] += length; ! } ! } ! } /** *************** *** 176,202 **** * * @param c ! * the coordinate to find the Euclidian distance to * @return the distance to parameter <code>c</code> */ ! public double distanceTo(Coordinate c) { ! // used for debugging so we can call distanceTo on something null ! assert ((VivaldiClient.USE_HEIGHT && num_dims == coords.length-1) || ! (!VivaldiClient.USE_HEIGHT && num_dims == coords.length)); ! if (c == null) { ! assert (!VivaldiClient.SIMULATION); ! return -1.; ! } ! double sum = 0.0; ! for (int i = 0; i < num_dims; ++i) { ! final double abs_dist = coords[i] - c.coords[i]; ! sum += (abs_dist * abs_dist); ! } ! sum = Math.sqrt(sum); ! if (VivaldiClient.USE_HEIGHT) { ! sum = sum + coords[coords.length-1] + c.coords[coords.length-1]; ! } ! return sum; ! } // Same regardless of using height --- 175,200 ---- * * @param c ! * the coordinate to find the Euclidian distance to * @return the distance to parameter <code>c</code> */ ! public double distanceTo(Coordinate c) { ! // used for debugging so we can call distanceTo on something null ! assert ((NCClient.USE_HEIGHT && num_dims == coords.length - 1) || (!NCClient.USE_HEIGHT && num_dims == coords.length)); ! if (c == null) { ! assert (!NCClient.SIMULATION); ! return -1.; ! } ! double sum = 0.0; ! for (int i = 0; i < num_dims; ++i) { ! final double abs_dist = coords[i] - c.coords[i]; ! sum += (abs_dist * abs_dist); ! } ! sum = Math.sqrt(sum); ! if (NCClient.USE_HEIGHT) { ! sum = sum + coords[coords.length - 1] + c.coords[coords.length - 1]; ! } ! return sum; ! } // Same regardless of using height *************** *** 211,251 **** * protected method, do not expose to client */ ! protected Vec getDirection(Coordinate c) { ! double length = distanceTo(c); ! if (length == 0) return null; ! final Vec new_vec = new Vec(coords.length); ! for (int i = 0; i < num_dims; ++i) { ! new_vec.direction[i] = (c.coords[i] - coords[i])/length; ! } ! if (VivaldiClient.USE_HEIGHT) { ! new_vec.direction[coords.length-1] = ! (c.coords[coords.length-1] + coords[coords.length-1])/length; ! } ! return new_vec; ! } - protected boolean assign (Coordinate c) { - if (coords.length != c.coords.length) return false; - for (int i = 0; i < coords.length; ++i) { - coords[i] = c.coords[i]; - } - return true; - } - - public void checkHeight () { - if (!VivaldiClient.USE_HEIGHT) return; - if (coords[coords.length-1] <= MIN_COORD) { - coords[coords.length-1] = VivaldiClient.random.nextDouble()+MIN_COORD; - } - } - - public boolean atOrigin () { - for (int i = 0; i < coords.length; i++) { - if (coords[i] != 0) - return false; - } - return true; - } - public Vec asVectorFromZero(boolean make_copy) { return new Vec(coords, make_copy); --- 209,252 ---- * protected method, do not expose to client */ ! protected Vec getDirection(Coordinate c) { ! double length = distanceTo(c); ! if (length == 0) ! return null; ! final Vec new_vec = new Vec(coords.length); ! for (int i = 0; i < num_dims; ++i) { ! new_vec.direction[i] = (c.coords[i] - coords[i]) / length; ! } ! if (NCClient.USE_HEIGHT) { ! new_vec.direction[coords.length - 1] = (c.coords[coords.length - 1] + coords[coords.length - 1]) ! / length; ! } ! return new_vec; ! } ! ! protected boolean assign(Coordinate c) { ! if (coords.length != c.coords.length) ! return false; ! for (int i = 0; i < coords.length; ++i) { ! coords[i] = c.coords[i]; ! } ! return true; ! } ! ! public void checkHeight() { ! if (!NCClient.USE_HEIGHT) ! return; ! if (coords[coords.length - 1] <= MIN_COORD) { ! coords[coords.length - 1] = NCClient.random.nextDouble() + MIN_COORD; ! } ! } ! ! public boolean atOrigin() { ! for (int i = 0; i < coords.length; i++) { ! if (coords[i] != 0) ! return false; ! } ! return true; ! } public Vec asVectorFromZero(boolean make_copy) { return new Vec(coords, make_copy); *************** *** 253,266 **** public boolean isValid() { ! final double NEG_MAX_DIST_FROM_ORIGIN = -1 * VivaldiClient.MAX_DIST_FROM_ORIGIN; for (int i = 0; i < coords.length; ++i) { if (Double.isNaN(coords[i])) { ! if (VivaldiClient.SIMULATION) System.err.println("coord isNaN i="+i); return false; } ! if (coords[i] > VivaldiClient.MAX_DIST_FROM_ORIGIN || ! coords[i] < NEG_MAX_DIST_FROM_ORIGIN) { ! if (VivaldiClient.SIMULATION) System.err.println("coord too far from origin i="+i+" coord="+coords[i]); ! return false; } } --- 254,270 ---- public boolean isValid() { ! final double NEG_MAX_DIST_FROM_ORIGIN = -1 * NCClient.MAX_DIST_FROM_ORIGIN; for (int i = 0; i < coords.length; ++i) { if (Double.isNaN(coords[i])) { ! if (NCClient.SIMULATION) ! System.err.println("coord isNaN i=" + i); return false; } ! if (coords[i] > NCClient.MAX_DIST_FROM_ORIGIN ! || coords[i] < NEG_MAX_DIST_FROM_ORIGIN) { ! if (NCClient.SIMULATION) ! System.err.println("coord too far from origin i=" + i + " coord=" ! + coords[i]); ! return false; } } *************** *** 303,314 **** final int num_dims = coords.length; for (int i = 0; true;) { ! if (i == num_dims-1 && VivaldiClient.USE_HEIGHT) { ! sbuf.append('h'); ! } ! sbuf.append(VivaldiClient.nf.format(coords[i])); ! if (++i < num_dims) { ! sbuf.append(","); } ! else { break; } --- 307,317 ---- final int num_dims = coords.length; for (int i = 0; true;) { ! if (i == num_dims - 1 && NCClient.USE_HEIGHT) { ! sbuf.append('h'); } ! sbuf.append(NCClient.nf.format(coords[i])); ! if (++i < num_dims) { ! sbuf.append(","); ! } else { break; } *************** *** 322,329 **** for (int i = 0; i < coords.length; i++) { ! if (i == coords.length - 1 && VivaldiClient.USE_HEIGHT) ! sbuf.append('h'); ! sbuf.append(VivaldiClient.nf.format(coords[i])); ! if (i != coords.length - 1) sbuf.append(" "); } --- 325,332 ---- for (int i = 0; i < coords.length; i++) { ! if (i == coords.length - 1 && NCClient.USE_HEIGHT) ! sbuf.append('h'); ! sbuf.append(NCClient.nf.format(coords[i])); ! if (i != coords.length - 1) sbuf.append(" "); } --- NEW FILE: NCClientIF.java --- package edu.harvard.syrah.pyxida.nc; public interface NCClientIF { } --- NEW FILE: NCClient.java --- package edu.harvard.syrah.pyxida.nc; /* * NCLib - a network coordinate library * * This program 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. * * This program 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 ( * see the LICENSE file ). * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place, Suite 330, Boston, MA 02111-1307 USA */ [...1078 lines suppressed...] */ } synchronized public void shutDown(DataOutputStream os) throws IOException { // could also save a number of neighbors // but then when we come back into the system, // it would be tricky to know how to treat them (they'd be old // so they would get much weight either). // System.err.println("Attempting to save coords during shut down"); // sys_coord.toSerialized(os); // save confidence? // save app_coord also? } } Index: Vec.java =================================================================== RCS file: /cvsroot/pyxida/Pyxida/src/edu/harvard/syrah/pyxida/nc/Vec.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Vec.java 17 Nov 2006 14:03:41 -0000 1.1 --- Vec.java 20 Nov 2006 17:22:31 -0000 1.2 *************** *** 67,73 **** final Vec v = new Vec(num_dims); for (int i = 0; i < num_dims; ++i) { ! double length = VivaldiClient.random.nextDouble() * axisLength; ! if ( (!VivaldiClient.USE_HEIGHT || i < num_dims-1) && ! (VivaldiClient.random.nextBoolean())) { length *= -1.; } --- 67,73 ---- final Vec v = new Vec(num_dims); for (int i = 0; i < num_dims; ++i) { ! double length = NCClient.random.nextDouble() * axisLength; ! if ( (!NCClient.USE_HEIGHT || i < num_dims-1) && ! (NCClient.random.nextBoolean())) { length *= -1.; } *************** *** 80,84 **** public Vec(int _num_dims) { direction = new double[_num_dims]; ! if (VivaldiClient.USE_HEIGHT) _num_dims--; num_dims = _num_dims; } --- 80,84 ---- public Vec(int _num_dims) { direction = new double[_num_dims]; ! if (NCClient.USE_HEIGHT) _num_dims--; num_dims = _num_dims; } *************** *** 98,102 **** } int _num_dims = init_dir.length; ! if (VivaldiClient.USE_HEIGHT) _num_dims--; num_dims = _num_dims; } --- 98,102 ---- } int _num_dims = init_dir.length; ! if (NCClient.USE_HEIGHT) _num_dims--; num_dims = _num_dims; } *************** *** 143,147 **** public double getLength() { double sum = getPlanarLength(); ! if (VivaldiClient.USE_HEIGHT) sum += direction[direction.length-1]; return sum; --- 143,147 ---- public double getLength() { double sum = getPlanarLength(); ! if (NCClient.USE_HEIGHT) sum += direction[direction.length-1]; return sum; *************** *** 195,202 **** final int num_dims = direction.length; for (int i = 0; true; ) { ! if (i == num_dims-1 && VivaldiClient.USE_HEIGHT) { sbuf.append('h'); } ! sbuf.append(VivaldiClient.nf.format(direction[i])); if (++i < num_dims) { sbuf.append(","); --- 195,202 ---- final int num_dims = direction.length; for (int i = 0; true; ) { ! if (i == num_dims-1 && NCClient.USE_HEIGHT) { sbuf.append('h'); } ! sbuf.append(NCClient.nf.format(direction[i])); if (++i < num_dims) { sbuf.append(","); Index: EWMAStatistic.java =================================================================== RCS file: /cvsroot/pyxida/Pyxida/src/edu/harvard/syrah/pyxida/nc/EWMAStatistic.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** EWMAStatistic.java 17 Nov 2006 14:03:41 -0000 1.1 --- EWMAStatistic.java 20 Nov 2006 17:22:31 -0000 1.2 *************** *** 7,49 **** /* ! * NCLib - a network coordinate library ! * ! * This program 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. ! * ! * This program 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 ( see the LICENSE file ). ! * ! * You should have received a copy of the GNU General Public License ! * along with this program; if not, write to the Free Software ! * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class EWMAStatistic { ! public static final double GAIN = 0.01; ! protected final double gain; ! protected double value; ! ! public EWMAStatistic (double g) { ! gain = g; ! value = 0; ! } ! public EWMAStatistic () { ! gain = GAIN; ! value = 0; ! } ! ! synchronized public void add (double item) { ! value = (GAIN*item)+((1.-GAIN)*value); ! } ! ! synchronized public double get () { ! return value; ! } ! } --- 7,48 ---- /* ! * NCLib - a network coordinate library ! * ! * This program 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. ! * ! * This program 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 ( ! * see the LICENSE file ). ! * ! * You should have received a copy of the GNU General Public License along with ! * this program; if not, write to the Free Software Foundation, Inc., 59 Temple ! * Place, Suite 330, Boston, MA 02111-1307 USA */ public class EWMAStatistic { ! public static final double GAIN = 0.01; ! protected final double gain; ! protected double value; ! public EWMAStatistic(double g) { ! gain = g; ! value = 0; ! } ! ! public EWMAStatistic() { ! gain = GAIN; ! value = 0; ! } ! ! synchronized public void add(double item) { ! value = (GAIN * item) + ((1. - GAIN) * value); ! } ! ! synchronized public double get() { ! return value; ! } } --- VivaldiClient.java DELETED --- |