|
From: Brendan M. <mc...@us...> - 2007-01-08 22:02:24
|
Update of /cvsroot/jigs/jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv13703/src/edu/whitman/halfway/util Modified Files: ProcessUtil.java MiscUtil.java CernUtil.java EADoubleFactory1D.java TimingTree.java Log Message: Index: ProcessUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/ProcessUtil.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** ProcessUtil.java 11 Nov 2006 02:04:56 -0000 1.6 --- ProcessUtil.java 8 Jan 2007 22:02:12 -0000 1.7 *************** *** 26,30 **** Process proc; try{ ! log.debug("Trying to execute command " + Arrays.asList(command)); proc = Runtime.getRuntime().exec(command, null, workingDir); }catch(IOException e){ --- 26,30 ---- Process proc; try{ ! if(log.isDebugEnabled())log.debug("Trying to execute command " + Arrays.asList(command)); proc = Runtime.getRuntime().exec(command, null, workingDir); }catch(IOException e){ *************** *** 33,37 **** } ! log.debug("Got process object, waiting to return."); if(out != null) { --- 33,37 ---- } ! if(log.isDebugEnabled())log.debug("Got process object, waiting to return."); if(out != null) { Index: MiscUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/MiscUtil.java,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** MiscUtil.java 28 Nov 2006 20:13:06 -0000 1.26 --- MiscUtil.java 8 Jan 2007 22:02:12 -0000 1.27 *************** *** 168,176 **** indexOfMin = i; } ! // else if(array[i] == minVal){ ! // if(Math.random() < 0.5){ ! // indexOfMin = i; ! // } ! // } } value = minVal; --- 168,176 ---- indexOfMin = i; } ! // else if(array[i] == minVal){ ! // if(Math.random() < 0.5){ ! // indexOfMin = i; ! // } ! // } } value = minVal; *************** *** 225,228 **** --- 225,229 ---- public static double value; public static int arg; + public final static double max(double[] r) { argMax(r); *************** *** 258,279 **** return min; } - public static double distValue = Double.NaN; public static double softValue = Double.NaN; ! ! public static double[] softMaxDistribution(double[] c, double alpha){ //if(alpha > 1.01){ // log.warn("Large alphas make the softmax value a bad approx to max (off by a alpha log(length(c)) factor"); //} double[] e = new double[c.length]; ! // first do hard max and subtract off exponents for // numerical stability double maxOverAlpha = -Double.MAX_VALUE; for (int i = 0; i < c.length; i++) { ! double ei = c[i]/alpha; e[i] = ei; ! if(ei > maxOverAlpha){ maxOverAlpha = ei; } --- 259,279 ---- return min; } public static double distValue = Double.NaN; public static double softValue = Double.NaN; ! ! public static double[] softMaxDistribution(double[] c, double alpha) { //if(alpha > 1.01){ // log.warn("Large alphas make the softmax value a bad approx to max (off by a alpha log(length(c)) factor"); //} double[] e = new double[c.length]; ! // first do hard max and subtract off exponents for // numerical stability double maxOverAlpha = -Double.MAX_VALUE; for (int i = 0; i < c.length; i++) { ! double ei = c[i] / alpha; e[i] = ei; ! if (ei > maxOverAlpha) { maxOverAlpha = ei; } *************** *** 281,286 **** //System.out.println("alpha = " + alpha); //System.out.println("maxOverAlpha = " + maxOverAlpha); ! ! double d=0; for (int i = 0; i < c.length; i++) { e[i] = Math.exp(e[i] - maxOverAlpha); --- 281,286 ---- //System.out.println("alpha = " + alpha); //System.out.println("maxOverAlpha = " + maxOverAlpha); ! ! double d = 0; for (int i = 0; i < c.length; i++) { e[i] = Math.exp(e[i] - maxOverAlpha); *************** *** 288,300 **** } //System.out.println("d = " + d + ", log(d) = " + Math.log(d)); ! ! softValue = alpha*(Math.log(d) + maxOverAlpha); ! if(alpha > 0){ ! double max = alpha*maxOverAlpha; ! assert softValue >= max -1e-10 : String.format("max = %s, softMax = %s", max, softValue); ! assert softValue <= alpha*maxOverAlpha + alpha*Math.log(c.length) + 1e-10; } ! distValue = 0; for (int i = 0; i < c.length; i++) { e[i] = e[i] / d; --- 288,302 ---- } //System.out.println("d = " + d + ", log(d) = " + Math.log(d)); ! ! softValue = alpha * (Math.log(d) + maxOverAlpha); ! if (alpha > 0) { ! double max = alpha * maxOverAlpha; ! assert softValue >= max - 1e-10 : String.format("max = %s, softMax = %s", ! max, softValue); ! assert softValue <= alpha * maxOverAlpha + alpha * Math.log(c.length) ! + 1e-10; } ! distValue = 0; for (int i = 0; i < c.length; i++) { e[i] = e[i] / d; *************** *** 303,382 **** return e; } ! ! public static double dot(double[] v1, double[] v2){ assert v1.length == v2.length; double d = 0; ! for(int i=0; i<v1.length; i++){ ! d += v1[i]*v2[i]; } return d; } ! ! // public static double[] softMinDistribution(double[] c, double alpha){ ! // double[] cp = new double[c.length]; ! // for(int i=0; i<cp.length; i++){ ! // cp[i] = -1.0 * c[i]; ! // } ! // double[] dist = softMaxDistribution(cp, alpha); ! // distValue *= -1.0; ! // return dist; ! // } ! ! public static double[] softMinDistribution(double[] c, double alpha){ //testing a better impl ! ! // double[] dist = softMinDistribution(c, alpha); ! // double td = distValue; ! // double sv = softValue; ! double[] dist2 = softMaxDistribution(c, -alpha); ! // assert MiscUtil.manhattanNorm(dist, dist2) < dist.length * 1e-8 : ! // String.format("%ndist1=%s\ndist2=%s%n", toString(dist), toString(dist2)); ! // assert MiscUtil.equals(td, distValue, 1e-8) : String.format("td1=%s, td2=%s", td, distValue); ! // assert MiscUtil.equals(sv, softValue, 1e-8) : String.format("sv1=%s, sv2=%s", sv, softValue); ! // ! sanityCheckSoftMin(c, alpha, softValue); return dist2; } ! ! private static boolean sanityCheckSoftMin(double[] c, double alpha, double softMin){ double min = min(c); assert softMin <= min + 1e-8; ! assert softMin >= min - alpha*Math.log(c.length) - 1e-10; return true; } ! ! public static double[] hardMaxDistribution(double[] c){ double max = -Double.MAX_VALUE; double maxCount = 0; for (int i = 0; i < c.length; i++) { ! if( c[i] > max){ max = c[i]; maxCount = 1; ! }else if(c[i] == max){ ! maxCount++; ! } } //System.out.println("br: maxCount = " + maxCount); double[] p = new double[c.length]; ! double probEachMax = 1.0/maxCount; for (int i = 0; i < c.length; i++) { ! if(c[i] == max){ p[i] = probEachMax; } ! } distValue = max; assert dot(c, p) == distValue; assert distValue == max(c) : distValue + " " + max(c) + "\n" ! + MiscUtil.toString(c, "%.5g"); return p; } ! /** Returns distribution that puts uniform weight on all indexes * of c corresponding to maximum values. */ ! public static double[] hardMinDistributionUniform(double[] c){ double[] cp = new double[c.length]; ! for(int i=0; i<cp.length; i++){ cp[i] = -1.0 * c[i]; } --- 305,387 ---- return e; } ! ! public static double dot(double[] v1, double[] v2) { assert v1.length == v2.length; double d = 0; ! for (int i = 0; i < v1.length; i++) { ! d += v1[i] * v2[i]; } return d; } ! ! // public static double[] softMinDistribution(double[] c, double alpha){ ! // double[] cp = new double[c.length]; ! // for(int i=0; i<cp.length; i++){ ! // cp[i] = -1.0 * c[i]; ! // } ! // double[] dist = softMaxDistribution(cp, alpha); ! // distValue *= -1.0; ! // return dist; ! // } ! ! public static double[] softMinDistribution(double[] c, double alpha) { //testing a better impl ! ! // double[] dist = softMinDistribution(c, alpha); ! // double td = distValue; ! // double sv = softValue; ! double[] dist2 = softMaxDistribution(c, -alpha); ! // assert MiscUtil.manhattanNorm(dist, dist2) < dist.length * 1e-8 : ! // String.format("%ndist1=%s\ndist2=%s%n", toString(dist), toString(dist2)); ! // assert MiscUtil.equals(td, distValue, 1e-8) : String.format("td1=%s, td2=%s", td, distValue); ! // assert MiscUtil.equals(sv, softValue, 1e-8) : String.format("sv1=%s, sv2=%s", sv, softValue); ! // ! sanityCheckSoftMin(c, alpha, softValue); return dist2; } ! ! private static boolean sanityCheckSoftMin(double[] c, double alpha, ! double softMin) { double min = min(c); assert softMin <= min + 1e-8; ! double lowerBound = min - alpha * Math.log(c.length); ! assert softMin >= lowerBound - 1e-5 : String.format( ! "softMin: %.5f, lower bound: %.5f", softMin, lowerBound); return true; } ! ! public static double[] hardMaxDistribution(double[] c) { double max = -Double.MAX_VALUE; double maxCount = 0; for (int i = 0; i < c.length; i++) { ! if (c[i] > max) { max = c[i]; maxCount = 1; ! } else if (c[i] == max) { ! maxCount++; ! } } //System.out.println("br: maxCount = " + maxCount); double[] p = new double[c.length]; ! double probEachMax = 1.0 / maxCount; for (int i = 0; i < c.length; i++) { ! if (c[i] == max) { p[i] = probEachMax; } ! } distValue = max; assert dot(c, p) == distValue; assert distValue == max(c) : distValue + " " + max(c) + "\n" ! + MiscUtil.toString(c, "%.5g"); return p; } ! /** Returns distribution that puts uniform weight on all indexes * of c corresponding to maximum values. */ ! public static double[] hardMinDistributionUniform(double[] c) { double[] cp = new double[c.length]; ! for (int i = 0; i < cp.length; i++) { cp[i] = -1.0 * c[i]; } *************** *** 386,405 **** return dist; } ! ! public static double[] hardMinDistributionArbitrary(double[] c){ ! double[] p = new double[c.length]; p[argMin(c)] = 1.0; distValue = value; // value set by argMin ! return p; } ! ! public static double[] hardMaxDistributionArbitrary(double[] c){ double[] p = new double[c.length]; p[argMax(c)] = 1.0; distValue = value;//value set by argMax ! return p; } ! ! public static void testDist(double[] c, double alpha){ double min = MiscUtil.min(c); double max = MiscUtil.max(c); --- 391,410 ---- return dist; } ! ! public static double[] hardMinDistributionArbitrary(double[] c) { ! double[] p = new double[c.length]; p[argMin(c)] = 1.0; distValue = value; // value set by argMin ! return p; } ! ! public static double[] hardMaxDistributionArbitrary(double[] c) { double[] p = new double[c.length]; p[argMax(c)] = 1.0; distValue = value;//value set by argMax ! return p; } ! ! public static void testDist(double[] c, double alpha) { double min = MiscUtil.min(c); double max = MiscUtil.max(c); *************** *** 409,449 **** //double[] minDist2 = softMinDistribution2(c, alpha); //double softMin2 = softValue; ! double[] maxDist = softMaxDistribution(c, alpha); double softMax = softValue; ! double minMix = dot(c, minDist); //double minMix2 = dot(c, minDist2); ! double maxMix = dot(c, maxDist); ! ! ! ! System.out.printf( ! "c=%s\t\talpha=%.4g%n"+ ! "\tsoftMaxDist =%s\tmax=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n" + ! "\tsoftMinDist =%s\tmin=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n", ! //"\tsoftMinDist2=%s\tmin=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n", ! MiscUtil.toString(c, "%.3g"), alpha, ! MiscUtil.toString(maxDist, "%.3g"), max, maxMix, softMax, ! MiscUtil.toString(minDist, "%.3g"), min, minMix, softMin ! //MiscUtil.toString(minDist2, "%.3g"), min, minMix2, softMin2 ! ); } ! ! ! ! public static void testSoftDistributions(){ double[][] tests = { { 1, 1, 1 }, { 0.3, 0.8, 1.0 }, { 990, 38, 70 }, { 9, 8.3, 11.3 }, { 0.01, 0.02, 0.015 } }; ! double[] alphas = { 15, 1, 0.1, 0.01, 0.0001 }; for (double[] c : tests) { for (double alpha : alphas) { ! testDist(c, alpha); } System.out.println(); } } - /** assumes rectangular, non-null array*/ --- 414,448 ---- //double[] minDist2 = softMinDistribution2(c, alpha); //double softMin2 = softValue; ! double[] maxDist = softMaxDistribution(c, alpha); double softMax = softValue; ! double minMix = dot(c, minDist); //double minMix2 = dot(c, minDist2); ! double maxMix = dot(c, maxDist); ! ! System.out.printf("c=%s\t\talpha=%.4g%n" ! + "\tsoftMaxDist =%s\tmax=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n" ! + "\tsoftMinDist =%s\tmin=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n", ! //"\tsoftMinDist2=%s\tmin=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n", ! MiscUtil.toString(c, "%.3g"), alpha, MiscUtil.toString(maxDist, ! "%.3g"), max, maxMix, softMax, MiscUtil.toString(minDist, ! "%.3g"), min, minMix, softMin ! //MiscUtil.toString(minDist2, "%.3g"), min, minMix2, softMin2 ! ); } ! ! public static void testSoftDistributions() { double[][] tests = { { 1, 1, 1 }, { 0.3, 0.8, 1.0 }, { 990, 38, 70 }, { 9, 8.3, 11.3 }, { 0.01, 0.02, 0.015 } }; ! double[] alphas = { 15, 1, 0.1, 0.01, 0.0001 }; for (double[] c : tests) { for (double alpha : alphas) { ! testDist(c, alpha); } System.out.println(); } } /** assumes rectangular, non-null array*/ *************** *** 558,562 **** buffer.append(String.format(format, array[i]) + ", "); } ! buffer.append(String.format(format, array[limit-1]) + "]"); } return buffer.toString(); --- 557,561 ---- buffer.append(String.format(format, array[i]) + ", "); } ! buffer.append(String.format(format, array[limit - 1]) + "]"); } return buffer.toString(); *************** *** 975,986 **** if (input[i] < 0) { throw new IllegalArgumentException( ! "Shouldn't be normalizing an input vector with negative entry."); } output[i] = input[i] / sum; } return output; - } /** Does a linear transformation on d so the min element of d maps * to a and the maximum element of d maps to b. returns a copy. --- 974,992 ---- if (input[i] < 0) { throw new IllegalArgumentException( ! "Shouldn't be normalizing an input vector with negative entry: " ! + input[i]); } output[i] = input[i] / sum; } return output; } + public static final void makeNonNegative(double[] input){ + int n = input.length; + for(int i=0; i<n; i++){ + if(input[i] < 0) input[i] = 0; + } + } + /** Does a linear transformation on d so the min element of d maps * to a and the maximum element of d maps to b. returns a copy. Index: CernUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/CernUtil.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** CernUtil.java 20 Nov 2006 19:52:24 -0000 1.13 --- CernUtil.java 8 Jan 2007 22:02:12 -0000 1.14 *************** *** 35,38 **** --- 35,40 ---- + + public static final String densityStatString(String prefix, DoubleMatrix1D vector, double approxThresh){ Index: EADoubleFactory1D.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/EADoubleFactory1D.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** EADoubleFactory1D.java 12 Jan 2004 20:06:43 -0000 1.2 --- EADoubleFactory1D.java 8 Jan 2007 22:02:12 -0000 1.3 *************** *** 1,3 **** --- 1,4 ---- package edu.whitman.halfway.util; + // import cern.colt.function.DoubleDoubleFunction; import cern.colt.matrix.*; *************** *** 6,67 **** // import cern.colt.list.DoubleArrayList; - import org.apache.log4j.Logger; - /** This is a dense factory that returns ElementAccessDenseDoubleMatrix1D's*/ ! public class EADoubleFactory1D extends DoubleFactory1D{ static Logger log = Logger.getLogger(EADoubleFactory1D.class); ! public DoubleMatrix1D make(DoubleMatrix2D m) { ! if(!(m instanceof EADoubleFactory2D.ElementAccessDenseDoubleMatrix2D)){ ! log.warn("Slow..."); ! m = (CernUtil.dense2D.make(m.rows(), m.columns())).assign(m); ! ! } ! return new ElementAccessDenseDoubleMatrix1D ! ( ((EADoubleFactory2D.ElementAccessDenseDoubleMatrix2D)m).getElements()); ! } ! ! - public DoubleMatrix1D make(int size) { - return new ElementAccessDenseDoubleMatrix1D(size); } ! public DoubleMatrix1D make(double[] values) { ! return new ElementAccessDenseDoubleMatrix1D(values); ! } ! public static class ElementAccessDenseDoubleMatrix1D ! extends DenseDoubleMatrix1D{ ! protected ElementAccessDenseDoubleMatrix1D(int size, double[] elements, int zero, int stride) { ! super(size, elements, zero, stride); ! } ! /** elements is packed in row-major */ ! public ElementAccessDenseDoubleMatrix1D(int size){ ! super(size); ! } ! /** elements is packed in row-major */ ! public ElementAccessDenseDoubleMatrix1D(double[] elements){ ! super(elements.length, elements, 0, 1); ! } ! ! public double[] getElements(){ ! return elements; ! } ! public DoubleMatrix1D like(int size){ ! return new ElementAccessDenseDoubleMatrix1D(size); ! } ! public DoubleMatrix2D like2D(int rows, int columns){ ! return new EADoubleFactory2D.ElementAccessDenseDoubleMatrix2D(rows, columns); ! } } } --- 7,66 ---- // import cern.colt.list.DoubleArrayList; import org.apache.log4j.Logger; /** This is a dense factory that returns ElementAccessDenseDoubleMatrix1D's*/ ! public class EADoubleFactory1D extends DoubleFactory1D { static Logger log = Logger.getLogger(EADoubleFactory1D.class); ! public DoubleMatrix1D make(DoubleMatrix2D m) { ! if (!(m instanceof EADoubleFactory2D.ElementAccessDenseDoubleMatrix2D)) { ! log.warn("Slow..."); ! m = (CernUtil.dense2D.make(m.rows(), m.columns())).assign(m); } + return new ElementAccessDenseDoubleMatrix1D( + ((EADoubleFactory2D.ElementAccessDenseDoubleMatrix2D) m) + .getElements()); + } ! public DoubleMatrix1D make(int size) { ! return new ElementAccessDenseDoubleMatrix1D(size); ! } + public DoubleMatrix1D make(double[] values) { + return new ElementAccessDenseDoubleMatrix1D(values); + } ! public static class ElementAccessDenseDoubleMatrix1D extends ! DenseDoubleMatrix1D { ! protected ElementAccessDenseDoubleMatrix1D(int size, double[] elements, ! int zero, int stride) { ! super(size, elements, zero, stride); ! } ! /** elements is packed in row-major */ ! public ElementAccessDenseDoubleMatrix1D(int size) { ! super(size); ! } ! /** elements is packed in row-major */ ! public ElementAccessDenseDoubleMatrix1D(double[] elements) { ! super(elements.length, elements, 0, 1); ! } ! public double[] getElements() { ! return elements; ! } ! public DoubleMatrix1D like(int size) { ! return new ElementAccessDenseDoubleMatrix1D(size); ! } + public DoubleMatrix2D like2D(int rows, int columns) { + return new EADoubleFactory2D.ElementAccessDenseDoubleMatrix2D(rows, + columns); } + + } } Index: TimingTree.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/TimingTree.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** TimingTree.java 19 Nov 2006 22:24:09 -0000 1.3 --- TimingTree.java 8 Jan 2007 22:02:12 -0000 1.4 *************** *** 18,21 **** --- 18,25 ---- protected Node root = null; protected Node activeNode = null; + + // the time of the last event timed (e.g., time between last start("name") and + // stopAndPop(). + private long lastTime; /** Creates and starts the root timer */ *************** *** 98,105 **** --- 102,114 ---- String stoppedName = activeNode.name; activeNode.timer.stop(); + lastTime = activeNode.timer.getLastEventTime(); activeNode = activeNode.parent; return stoppedName; } + public String lastEventTimeString(){ + return StringUtil.formatMillis(lastTime); + } + public String stopAndPop(String name){ if(!name.equals(activeNode.name)){ |