private boolean isBestValueOptimized(Measure measure) { return measure.getDescription() == null && measure.getData() == null && !measure.hasQualityGateStatus() && hasNoVariation(measure) && (measure.getValueType() == NO_VALUE || isBestValue(measure, metric.getBestValue())); }
private static void addAlbertiBass(Measure currentMeasure, Chord currentChord) { Triad currentTriad = currentChord.getTriad(); Note[] triad = new Note[3]; triad = currentTriad.getNotes().toArray(triad); int[] noteOctaves = getNoteOctaves(triad); Beat newBeat = new Beat(); Note currentNote = new Note(triad[0].getTone().toString(), BeatDuration.Eighth); currentNote.setOctave(new Octave(noteOctaves[0])); newBeat.addNote(currentNote); currentMeasure.addBeat(newBeat); newBeat = new Beat(); currentNote = new Note(triad[2].getTone().toString(), BeatDuration.Eighth); currentNote.setOctave(new Octave(noteOctaves[2])); newBeat.addNote(currentNote); currentMeasure.addBeat(newBeat); newBeat = new Beat(); currentNote = new Note(triad[1].getTone().toString(), BeatDuration.Eighth); currentNote.setOctave(new Octave(noteOctaves[1])); newBeat.addNote(currentNote); currentMeasure.addBeat(newBeat); newBeat = new Beat(); currentNote = new Note(triad[2].getTone().toString(), BeatDuration.Eighth); currentNote.setOctave(new Octave(noteOctaves[2])); newBeat.addNote(currentNote); currentMeasure.addBeat(newBeat); }
private int[][] skyLinesToArray() { int numSkyLines = skyLinesList.size(); int[][] skyLinesArray = new int[numSkyLines][Measure.NUM_OF_PARAMETERS]; for (int skyLinePosition = 0; skyLinePosition < numSkyLines; skyLinePosition++) { Measure skyLine = skyLinesList.get(skyLinePosition); skyLinesArray[skyLinePosition] = skyLine.toArray(); } return skyLinesArray; }
public static Measure quarterNoteBass(Measure sustainedChords) { Measure newMeasure = new Measure(); for (Beat currentBeat : sustainedChords.getBeats()) { for (VoiceElement currentChord : currentBeat.getVoiceElements()) { Beat newBeat = new Beat(); newBeat.addChord(new Chord(((Chord) currentChord).getTriad(), BeatDuration.Quarter)); for (int i = 0; i < 2; i++) newMeasure.addBeat(newBeat); } } return newMeasure; }
@Override protected void loadAttributes(Attributes attrs) throws ValidateException { super.loadAttributes(attrs); Measure top = getAttrValueAsMeasure(ATTRIB_MARGIN_TOP); if (top.getValue() < 0) { throw new ValidateException( this, "per l'attributo marginTop non sono ammessi valori negativi."); } Measure bot = getAttrValueAsMeasure(ATTRIB_MARGIN_BOTTOM); if (bot.getValue() < 0) { throw new ValidateException( this, "per l'attributo marginBottom non sono ammessi valori negativi."); } }
/** * Find a matching measure by name in this food * * @param measureName */ public void setMeasure(String measureName) { if (food == null) { return; } List measures = getFoodProxy().getFood().getMeasures(); for (int i = 0; i < measures.size(); i++) { Measure m = (Measure) measures.get(i); if (m.getDescription().equals(measureName)) { setMeasure(m); return; } } // if nothing found, default to GRAMS setMeasure(Measure.GRAM); }
private void generateSkyLinesByHeights() { skyLinesList = new ArrayList<Measure>(); int position = 0; int heightBuilding = NO_HEIGHT_BUILDING; int startBuildingPosition = 0; while (position < heights.length) { heightBuilding = heights[position]; while ((position + 1 < heights.length) && (heightBuilding == heights[position + 1])) position++; position++; Measure line = new Line(startBuildingPosition, heightBuilding, position); if (!line.isZeroHeight()) skyLinesList.add(line); startBuildingPosition = position; } }
public Optional<Measure> toMeasure(@Nullable BatchReport.Measure batchMeasure, Metric metric) { Objects.requireNonNull(metric); if (batchMeasure == null) { return Optional.absent(); } Measure.NewMeasureBuilder builder = Measure.newMeasureBuilder(); String data = batchMeasure.hasStringValue() ? batchMeasure.getStringValue() : null; switch (metric.getType().getValueType()) { case INT: return toIntegerMeasure(builder, batchMeasure, data); case LONG: return toLongMeasure(builder, batchMeasure, data); case DOUBLE: return toDoubleMeasure(builder, batchMeasure, data); case BOOLEAN: return toBooleanMeasure(builder, batchMeasure, data); case STRING: return toStringMeasure(builder, batchMeasure); case LEVEL: return toLevelMeasure(builder, batchMeasure); case NO_VALUE: return toNoValueMeasure(builder, batchMeasure); default: throw new IllegalArgumentException( "Unsupported Measure.ValueType " + metric.getType().getValueType()); } }
public static Measure arpegiattedBass(Measure sustainedChords) { Measure newMeasure = new Measure(); for (Beat currentBeat : sustainedChords.getBeats()) { for (VoiceElement currentChord : currentBeat.getVoiceElements()) { addArpegiattedBass(newMeasure, (Chord) currentChord); } } return newMeasure; }
public static Measure halfNoteRoot(Measure sustainedChords) { Measure newMeasure = new Measure(); for (Beat currentBeat : sustainedChords.getBeats()) { for (VoiceElement currentChord : currentBeat.getVoiceElements()) { addHalfNoteBass(newMeasure, (Chord) currentChord); } } return newMeasure; }
// populate (could optionally have reflective populate) public synchronized void populate(SQLRow row) { row.setValue("source", food.getSource().getName()); row.setValue("food", food.getSourceID()); if (date != 0) { row.setValue("time", new Timestamp(date)); } row.setValue("grams", new Double(grams)); if (measure != Measure.GRAM) { row.setValue("measure", measure.getDescription()); } }
private void cpdSpd(Measure m) { double fr = m.time - 2; double to = m.time + 2; double spd = 0; double cnt = 0; double dsSpd = 0; double dsCnt = 0; double usSpd = 0; double usCnt = 0; for (Id id : m.in) { AgentInfo ai = this.ais.get(id); Entry<Double, Integer> frEntr = ai.timePos.floorEntry(fr); Entry<Double, Integer> toEntr = ai.timePos.ceilingEntry(to); if (frEntr == null) { frEntr = ai.timePos.ceilingEntry(fr); } if (toEntr == null) { toEntr = ai.timePos.floorEntry(to); } double tt = toEntr.getKey() - frEntr.getKey(); int cells = toEntr.getValue() - frEntr.getValue(); double dist = Math.abs(cells) * this.cellsize; double s = dist / tt; spd += s; cnt++; if (ai.dir == 1) { dsSpd += s; dsCnt++; } else { usSpd += s; usCnt++; } } m.spd = spd / cnt; if (dsCnt > 0) { m.dsSpd = dsSpd / dsCnt; } if (usCnt > 0) { m.usSpd = usSpd / usCnt; } }
@XmlElementWrapper(name = "healthProfile") @XmlElement(name = "measure") public List<Measure> getCurrentMeasures() { // When the currentMeasures is not empty it means it's reading the measures of a person that // will be created with some initial measures. if (this.currentMeasures == null) this.currentMeasures = Measure.getCurrentMeasuresFromPerson( this .personId); // If the currentMeasures is empty then it loads the lates measures of // a person to represent the Health Profile return currentMeasures; }
public static void main(String[] args) throws IOException { Airport a = new Airport(); Motor.setStartDate(); try { CsvManager.csvManagerInit(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Error to initiate Csv Manager"); } for (int i = 0; i < 3 * 30 * 24 * 60; i++) { // Step up the system Motor.stepUp(); // add new plane if (randomPlaneArriver()) { int planeNumber = a.getTower().registerNewAproache(a); a.getTower().addToLandList(planeNumber); System.out.println("Plane P" + planeNumber + " is waiting to land"); } a.getTrack().handler(a); a.getTaxiways()[Taxiway.TAXIWAY_ARRIVE].handler(a); a.getTaxiways()[Taxiway.TAXIWAY_LEAVE].handler(a); for (int j = 0; j < a.getGates().length; j++) { a.getGates()[j].handler(a); } System.out.println(" "); } for (int i = 0; i < a.getTower().planeList.size(); i++) { a.getTower().getPlaneById(i).writeLog(); } Measure.getRet().printAll(); Measure.getFreq().printFreqLog(); }
/** * Set preferred measure of wind speed. * * @param _preferredMeasure * @throws IllegalArgumentException if measure not supported * @see #getMeasure() * @see #getSupportedMeasure() */ public static void setPreferredMeasure(final Measure _preferredMeasure) throws IllegalArgumentException { if (_preferredMeasure == null) { throw new NullPointerException("Preferred Measure"); } if (!supportedMeasure.contains(_preferredMeasure)) { throw new IllegalArgumentException( "cant set preferred measure to " + _preferredMeasure.getDescription() + " expected: " + supportedMeasure.toString()); } preferredMeasure = _preferredMeasure; }
public synchronized XMLNode toXML() { XMLNode node = new XMLNode("serving"); node.addAttribute("source", food.getSource().getName()); node.addAttribute("food", food.getSourceID()); if (date != 0) { node.addAttribute("date", date); } node.addAttribute("grams", grams); if (measure != Measure.GRAM) { node.addAttribute("measure", measure.getDescription()); } if (meal != -1) { node.addAttribute("meal", meal); } return node; }
/** * Used by SystemTranslator to allocate the dynamics marks * * @param glyph underlying glyph * @param measure measure where the mark is located * @param point location for the mark */ public static void populate(Glyph glyph, Measure measure, SystemPoint point) { // Can we gather with another dynamics letter? (e.g. m + p -> mp) for (TreeNode node : measure.getChildren()) { if (node instanceof Dynamics) { Dynamics d = (Dynamics) node; if (d.isCompatibleWith(point)) { d.addGlyph(glyph); glyph.setTranslation(d); return; } } } // Otherwise, create a brand new instance glyph.setTranslation(new Dynamics(measure, point, findChord(measure, point), glyph)); }
private static boolean isBestValue(Measure measure, Double bestValue) { switch (measure.getValueType()) { case BOOLEAN: return bestValue.intValue() == 1 ? measure.getBooleanValue() : !measure.getBooleanValue(); case INT: return bestValue.intValue() == measure.getIntValue(); case LONG: return bestValue.longValue() == measure.getLongValue(); case DOUBLE: return bestValue.compareTo(measure.getDoubleValue()) == 0; default: return false; } }
private static float checkSlab( int tokType, P3 v, float val, float distance, P4 plane, P3[] ptCenters, BS bs) { float d; switch (tokType) { case T.decimal: return (val >= 0 && bs.get((int) val) ? 1 : -1); case T.min: d = distance - val; break; case T.max: d = val - distance; break; case T.plane: d = Measure.distanceToPlane(plane, v); break; case T.distance: d = minDist(v, ptCenters) - distance; break; default: d = -minDist(v, ptCenters) - distance; break; } return (Math.abs(d) < 0.0001f ? 0 : d); }
@Override public void convertTo(final Measure _measure) { if (_measure == null) { throw new NullPointerException("Measure"); } if (orginalValue.getMeasure() == _measure) { convert = orginalValue; return; } if (convert != null && convert.getMeasure() == _measure) { return; } if (!supportedMeasure.contains(_measure)) { throw new IllegalArgumentException( "cant convert " + orginalValue.getMeasure().getDescription() + " into " + _measure.getShortDisplayName()); } // System.out.println("Converting into: "+_measure.getDescription()); // TODO: Move calculation into switch block for better performance // final float v = orginalValue.getValue(); // always Knots!!! final float ms = v * 0.514444f; // m s-1 final float kmh = v * 1.852f; // km/h final float knot = v; // knots final float mph = v * 1.150779f; // mi h-1 final float fts = (v * 1.687810f); // ft s-1 final float mmi = (ms * 60.f); // m min-1 final float ftm = (fts * 60.f); // ft min-1 final int beaufort = knotToBeaufort(knot); switch (_measure) { case MPS: convert = WindSpeed.create(ms, Measure.MPS); break; case KMH: convert = WindSpeed.create(kmh, Measure.KMH); break; case KNOTS: convert = WindSpeed.create(knot, Measure.KNOTS); break; case MPH: convert = WindSpeed.create(mph, Measure.MPH); break; case FPS: convert = WindSpeed.create(fts, Measure.FPS); break; case MMI: convert = WindSpeed.create(mmi, Measure.MMI); break; case FTM: convert = WindSpeed.create(ftm, Measure.FTM); break; case BEAUFORT: convert = WindSpeed.create(beaufort, Measure.BEAUFORT); break; default: throw new IllegalArgumentException("unknown Measure" + _measure.getDescription()); } }
@Override public void report(BufferedWriter bw) throws IOException { int in = this.dsIn + this.usIn - this.dsOut - this.usOut; System.out.println( in + " " + " dsIn:" + this.dsIn + " dsOut:" + this.dsOut + " usIn:" + this.usIn + " usOut:" + this.usOut); List<Tuple<Integer, Integer>> ranges = new ArrayList<>(); Variance vd = new Variance(); Variance vs = new Variance(); Variance vf = new Variance(); int cnt = 0; double tm = 15; int from = 0; int to = 0; for (Measure m : this.ms) { cpdSpd(m); bw.append( m.time + " " + m.dsRho + " " + m.dsSpd + " " + m.usRho + " " + m.usSpd + " " + m.rho + " " + " " + m.spd + "\n"); if (true) continue; if (m.time < 15 || m.time > 100) { to++; from++; continue; } cpdSpd(m); vd.addVar(m.rho); vs.addVar(m.spd); vf.addVar(m.rho * m.spd); cnt++; if (cnt < 5) { to++; continue; } double md = vd.getMean(); double std = Math.sqrt(vd.getVar()); double ms = vs.getMean(); double sts = Math.sqrt(vs.getVar()); double mf = vf.getMean(); double stf = Math.sqrt(vf.getVar()); double bal = m.dsRho / m.usRho; bal = bal > 1 ? 1 / bal : bal; // bal = 1; double timespan = m.time - tm; if (md > 1 && std > 0.1 * md || sts > 0.1 * ms || bal < 0.5 || timespan > 20) { if (timespan > 5) { Tuple<Integer, Integer> t = new Tuple<>(from, to); ranges.add(t); } tm = m.time; from = to; cnt = 0; vd = new Variance(); vs = new Variance(); } to++; } int rng = ranges.size(); int cntt = 0; for (Tuple<Integer, Integer> r : ranges) { cntt++; // if (cntt < rng / 2 + rng / 4 || cntt > rng / 2 + rng / 4 + rng / // 8) { // continue; // } from = r.getFirst(); to = r.getSecond(); Measure m = new Measure(); double range = to - from; double ccnt = 0; double lastTime = 0; for (int idx = from; idx < to; idx++) { Measure mm = this.ms.get(idx); if (lastTime + 1 > mm.time) { continue; } ccnt++; m.rho += mm.rho; m.spd += mm.spd; m.time += mm.time; m.dsRho += mm.dsRho; m.usRho += mm.usRho; m.dsSpd += mm.dsSpd; m.usSpd += mm.usSpd; bw.append( mm.time + " " + mm.dsRho + " " + mm.dsSpd + " " + mm.usRho + " " + mm.usSpd + " " + mm.rho + " " + " " + mm.spd + "\n"); lastTime = mm.time; } m.rho /= ccnt; m.spd /= ccnt; m.dsRho /= ccnt; m.dsSpd /= ccnt; m.usRho /= ccnt; m.usSpd /= ccnt; m.time /= ccnt; // bw.append(m.time + " " + m.dsRho + " " + m.dsSpd + " " + m.usRho // + " " + m.usSpd + " " + m.rho + " " + " " + m.spd + "\n"); } // bw.append(m.time + " " + m.rho + " " + m.spd + "\n"); }
@Override public void trigger(double time) { Measure m = new Measure(); m.time = time; int cnt = 0; double dsCnt = 0; double usCnt = 0; double dsRho = 0; double usRho = 0; for (int lane = 0; lane < this.lanes; lane++) { CAMoveableEntity[] p = this.l.getParticles(lane); for (int i = this.from; i <= this.to; i++) { if (p[i] != null) { cnt++; m.in.add(p[i].getId()); AgentInfo ai = this.ais.get(p[i].getId()); // updateTimeRho(i, ai.dir, ai, time, p); if (p[i].getDir() == 1) { dsCnt++; // dsRho += ai.timeRho.get(time); } else { usCnt++; // usRho += ai.timeRho.get(time); } } } } double rho = cnt / this.a; m.rho = rho; m.dsRho = dsCnt / this.a; m.usRho = usCnt / this.a; // m.dsRho = dsRho / dsCnt; // m.usRho = usRho / usCnt; for (int lane = 0; lane < this.lanes; lane++) { CAMoveableEntity[] p = this.l.getParticles(lane); for (int i = 0; i < this.l.getNumOfCells(); i++) { CAMoveableEntity part = p[i]; if (part != null) { Id id = part.getId(); AgentInfo ai = this.ais.get(id); if (ai == null && (i == 0 || i == this.l.getNumOfCells() - 1)) { ai = new AgentInfo(part.getDir()); // ai.lastPos = i; this.ais.put(id, ai); } if (ai.lastPos != i) { ai.timePos.put(time, i); ai.lastPos = i; if (i == 0) { if (part.getDir() == 1) { this.dsIn++; } else { this.usOut++; } } else if (i == this.l.getNumOfCells() - 1) { if (part.getDir() == 1) { this.dsOut++; } else { this.usIn++; } } } } } } if (m.rho > 0) { this.ms.add(m); } }
@Test public void testPh() { assertEquals(measure.ph(100), Central.getDisplayHeight()); }
@Test public void testPw() { assertEquals(measure.pw(100), Central.getDisplayWidth()); }
public static void main(String[] args) { // TEST MEASURE // Point p1 = new Point(-1d, -1d); // Point p2 = new Point(2d, 3d); // System.out.println(measure.d(p1, p2)); // System.out.println(measure.s(p1, p2)); // return; Double[][] data = FileHandler.readFile(fileName); // cannot display points if dimension is > 2 if (data[0].length != 2) canDisplay = false; // build graphic points from coords' array buildPointsFromData(data); Config.computeBoundingRect(points); // init display if (canDisplay) { disp = new Display(); disp.setVisible(true); for (Point p : points) { disp.addObject(p); } } testResults = new double[nbTests]; for (int t = 0; t < nbTests; ++t) { // define K clusters and K temporary centres clusters = new ArrayList<Cluster>(); for (int i = 0; i < K; ++i) { clusters.add(new Cluster()); } setRandomCenters(); for (Cluster c : clusters) { System.out.println("center for cluster " + c + ": " + c.getCenter()); } if (canDisplay) pause(1000); // variables used in for loops double minDist, currDist, diff; Double[] prevCoords, newCoords; Cluster alloc; Point newCenter; for (int i = 0; i < maxIter; ++i) { if (canDisplay) { disp.setLabel("[ iteration #" + (i + 1) + " ]"); } else { System.out.println("------> iteration #" + (i + 1)); } // allocate points to group which center is closest for (Point p : points) { minDist = Config.MAX; alloc = clusters.get(0); // default initialization for (Cluster c : clusters) { currDist = measure.d(p, c.getCenter()); if (currDist < minDist) { minDist = currDist; alloc = c; } } alloc.addPoint(p); } // recenter: calculate gravity centers for formed groups diff = 0; prevCoords = null; for (Cluster c : clusters) { // delete previous center if it not a Point of the Cluster if (canDisplay && !c.getPoints().contains(c.getCenter())) { disp.removeObject(c.getCenter()); } if (stopOnConverge) { prevCoords = c.getCenter().getCoords(); } newCenter = c.makeGravityCenter(); if (stopOnConverge) { newCoords = c.getCenter().getCoords(); for (int k = 0; k < prevCoords.length; ++k) { diff += Math.abs(prevCoords[k] - newCoords[k]); } } if (canDisplay) { disp.addObject(newCenter); } else { // System.out.println("\tcenter for " + c + ": " + c.getCenter()); System.out.println(c.getCenter()); } } // loop over clusters if (canDisplay) { disp.repaint(); } // if Clusters' centers don't change anymore, then stop (algorithm converged) if (diff == 0 && stopOnConverge) { testResults[t] = (double) i; if (canDisplay) { disp.setLabel("[ Converged at iteration #" + (i) + " ]"); disp.repaint(); } else { System.out.println("[ Converged at iteration #" + (i) + " ]"); } break; } pause(100); } // loop over iterations if (testResults[t] == 0) { System.out.println("!!!!!!!!!! Test #" + t + " did not converge."); if (stopOnConverge) return; } // reset display if (canDisplay && t + 1 < nbTests) { for (Point p : points) p.setCluster(null); for (Cluster c : clusters) disp.removeObject(c.getCenter()); } } // loop over tests // display test results and compute means DescriptiveStatistics stats = new DescriptiveStatistics(testResults); System.out.println("=========> Results:"); for (int t = 0; t < nbTests; ++t) { System.out.println("--> [ " + testResults[t] + " ]"); } System.out.println("=========> Means: " + stats.getMean()); System.out.println("=========> Std dev: " + stats.getStandardDeviation()); }
private static boolean hasNoVariation(Measure measure) { return !measure.hasVariations() || hasOnlyZeroVariations(measure.getVariations()); }
@Override public List<Report595DTO> getTopSkippedQuestions( List<Integer> clinicIds, String fromDate, String toDate) { List<Report595DTO> resultList = new ArrayList<>(); Query q = entityManager.createNativeQuery( "SELECT count(*), measure_id FROM veteran_assessment_question_presence vsaq " + "INNER JOIN veteran_assessment va ON vsaq.veteran_assessment_id = va.veteran_assessment_id " + " WHERE skipped = -1 AND date_completed >= :fromDate AND date_completed <= :toDate " + "AND clinic_id IN (:clinicIds) AND date_completed IS NOT NULL " + " GROUP BY measure_id " + " ORDER BY count(*) DESC "); setParametersFor593(q, fromDate, toDate, clinicIds); List r = q.getResultList(); if (r != null && r.size() > 0) { for (Object aRow : q.getResultList()) { Object[] data = (Object[]) aRow; int count = ((Number) data[0]).intValue(); if (count == 0) break; int measureId = ((Number) data[1]).intValue(); Measure m = measureRepository.findOne(measureId); // only allow singleSelect or multiSelect int measureType = m.getMeasureType().getMeasureTypeId(); if (measureType != 1 && measureType != 2 && measureType != 3) { continue; } Report595DTO dto = new Report595DTO(); Query q1 = entityManager.createNativeQuery( "select count(*) from veteran_assessment_question_presence vsaq " + "inner join veteran_assessment va on vsaq.veteran_assessment_id = va.veteran_assessment_id " + " where date_completed >= :fromDate and date_completed <= :toDate " + "and clinic_id in (:clinicIds) and date_completed is not null " + " and measure_id =" + measureId); setParametersFor593(q1, fromDate, toDate, clinicIds); int total = ((Number) q1.getSingleResult()).intValue(); DecimalFormat formatter = new DecimalFormat("###.##"); String percent = formatter.format(count * 100d / total); dto.setPercentage(percent + "% (" + count + "/" + total + ")"); dto.setQuestions(((Number) data[1]).intValue() + ""); dto.setQuestions(m.getMeasureText()); dto.setVariableName(m.getVariableName()); resultList.add(dto); } } Collections.sort( resultList, new Comparator<Report595DTO>() { @Override public int compare(Report595DTO o1, Report595DTO o2) { String[] a1 = o1.getPercentage().split("%"); String[] a2 = o2.getPercentage().split("%"); float one = Float.parseFloat(a1[0]); float two = Float.parseFloat(a2[0]); if (one > two) return 1; else if (one < two) return -1; return 0; } }); int index = 1; List<Report595DTO> results = new ArrayList<>(20); for (Report595DTO dto : resultList) { dto.setOrder(Integer.toString(index++)); results.add(dto); if (index > 20) break; } return results; }
public static void main(String[] args) throws Exception { final SimpleJSAP jsap = new SimpleJSAP( Sparsifier.class.getName(), "Estimates and sparsifies a propagation model from a set of observations.", new Parameter[] { new FlaggedOption( "social-network", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 's', "social-network", "The file containing the social network graph"), new FlaggedOption( "probabilities", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 'p', "probabilities", "The file containing the propagation probabilities"), new FlaggedOption( "candidate-selection-policy", JSAP.STRING_PARSER, CandidateSelectionPolicy.DEFAULT_CANDIDATE_SELECTION_POLICY .getClass() .getSimpleName(), JSAP.REQUIRED, 'c', "candidate-selection-policy", "The name of the candidate selection policy"), new FlaggedOption( "auxiliary-basename", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, JSAP.NO_SHORTFLAG, "auxiliary-basename", "The base name for reading a pre-computed auxiliary structure"), new FlaggedOption( "input", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 'i', "input", "The file containing the observations"), new FlaggedOption( "sparsifier", JSAP.STRING_PARSER, DEFAULT_SPARSIFIER.getSimpleName(), JSAP.NOT_REQUIRED, 'f', "sparsifier", "The sparsifier to run, from this list: " + StringUtils.join(Reflection.subClasses(Sparsifier.class), ',')), new FlaggedOption( "sparse-model-size", JSAP.INTEGER_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'k', "sparse-model-size", "The size of the sparse model"), new FlaggedOption( "number-of-chunks", JSAP.INTEGER_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'r', "number-of-chunks", "The number of chunks to be sparsified in parralel"), new FlaggedOption( "output", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'o', "output", "File to dump sparsified model to"), new FlaggedOption( "measures-file", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'z', "measures-file", "Save measures of partial models to file"), new FlaggedOption( "debug-file", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'd', "debug-file", "Save debug information to file"), new Switch( "with-fraction", 'n', "with-fraction", "Disable the computation of the 'fraction of covered propagations'."), new Switch( "incremental-likelihood", JSAP.NO_SHORTFLAG, "incremental-likelihood", "Performs incremental computation of likelihood, for sparsifications methods that support this option (faster, experimental)."), }); final JSAPResult jsapResult = jsap.parse(args); if (jsap.messagePrinted()) { return; } // Load social network and input String snFilename = jsapResult.getString("social-network"); SocialNetwork socNet = new SocialNetwork(Utilities.getIterator(snFilename)); String obsFilename = jsapResult.getString("input"); ObservationsReader observations = new ObservationsReader(obsFilename); // Load original model ICModel originalModel = new ICModel(socNet, Utilities.getIterator(jsapResult.getString("probabilities"))); // Load candidate selection policy String selectionPolicyName = jsapResult.getString("candidate-selection-policy"); Class<?> selectionPolicyClass = Class.forName( CandidateSelectionPolicy.class.getPackage().getName() + "." + selectionPolicyName); CandidateSelectionPolicy candidateSelectionPolicy = (CandidateSelectionPolicy) selectionPolicyClass.getConstructor(new Class[] {}).newInstance(new Object[] {}); // Create sparsifier String sparsifierName = jsapResult.getString("sparsifier"); Class<?> sparsifierClass = Class.forName(Sparsifier.class.getPackage().getName() + "." + sparsifierName); Sparsifier sparsifier = (Sparsifier) sparsifierClass .getConstructor(new Class[] {originalModel.getClass()}) .newInstance(new Object[] {originalModel}); LOGGER.info("Created a " + sparsifier.getClass().getSimpleName()); // Set sparsifier options if (!jsapResult.getBoolean("with-fraction")) { sparsifier.disableComputationOfPartialFractionOfPropagations(); LOGGER.info("Disabled the computation of fraction of propagations"); } if (jsapResult.getBoolean("incremental-likelihood")) { if (sparsifier instanceof GreedySparsifier) { ((GreedySparsifier) sparsifier).setIncrementalLikelihoodComputation(); LOGGER.info("Enabled incremental computation of likelihood (faster, experimental)"); } else { LOGGER.warn( "This type of sparsifier does not accept the --incrementa-likelihood switch, ignoring"); } } if (jsapResult.userSpecified("auxiliary-basename")) { // Use existing auxiliary file String auxiliaryBasename = jsapResult.getString("auxiliary-basename"); ICEstimateAuxiliary auxiliary = new ICEstimateAuxiliary(socNet, observations, null); LOGGER.info("Loading pre-computed auxiliary variables"); auxiliary.read(auxiliaryBasename); sparsifier.useAuxiliary(auxiliary); if (auxiliary .getCandidateSelectionPolicy() .toSpec() .equals(candidateSelectionPolicy.toSpec())) { LOGGER.info( "Candidate selection policy: " + auxiliary.getCandidateSelectionPolicy().toSpec()); } else { throw new IllegalArgumentException( "The candidate selection policies do not match: auxiliary has '" + auxiliary.getCandidateSelectionPolicy().toSpec() + "', sparsifier has '" + candidateSelectionPolicy.toSpec() + "'"); } } else { // Compute auxiliary variables LOGGER.info("Computing auxiliary variables"); sparsifier.computeAuxiliary(observations, candidateSelectionPolicy); } int maxSparseSize; if (jsapResult.userSpecified("sparse-model-size")) { maxSparseSize = jsapResult.getInt("sparse-model-size"); } else { maxSparseSize = originalModel.getProbs().cardinality(); LOGGER.info( "Setting target number of arcs to number of arcs with non-zero probability in the original model"); } // Open debug file if (jsapResult.userSpecified("debug-file")) { String debugFilename = jsapResult.getString("debug-file"); LOGGER.info("Will write debug output to " + debugFilename); sparsifier.openDebugFile(debugFilename); } int numOfChunks = 1; if (jsapResult.userSpecified("number-of-chunks")) { numOfChunks = jsapResult.getInt("number-of-chunks"); } ICModel sparseModel = runSparsifier( socNet, observations, originalModel, maxSparseSize, sparsifier, numOfChunks, true); // Write partial results to file if necessary if (jsapResult.userSpecified("measures-file")) { for (Measure m : sparsifier.partialResults.keySet()) { String logFilename = jsapResult.getString("measures-file"); switch (m) { case LOG_L: logFilename = logFilename + ".logL"; break; case FRACTION_OF_PROPAGATIONS: logFilename = logFilename + ".frac"; break; default: break; } PrintWriter report = new PrintWriter(new BufferedWriter(new FileWriter(new File(logFilename)))); LOGGER.info("Writing partial " + m.toString() + " results to " + logFilename); report.println("#k\t" + m.toString()); int[] ks = sparsifier.partialResults.get(m).keySet().toArray(new int[] {}); Arrays.sort(ks); for (int k : ks) { report.println(k + "\t" + sparsifier.partialResults.get(m).get(k)); } report.close(); } } // Dump probabilities if (jsapResult.userSpecified("output")) { String probsFilename = jsapResult.getString("output"); PrintWriter pw = Utilities.getPW(probsFilename); LOGGER.info("Dumping probabilities to " + probsFilename); sparseModel.dumpProbabilities(pw); pw.close(); } sparsifier.closeDebugFile(); }