Esempio n. 1
0
 /**
  * Returns a dense array representing the potential pool permanences
  *
  * <p>Note: Only called from tests for now...
  *
  * @param c
  * @return
  */
 public double[] getDensePermanences(Connections c) {
   double[] retVal = new double[c.getNumInputs()];
   int[] keys = synapsesBySourceIndex.keys();
   for (int inputIndex : keys) {
     retVal[inputIndex] = synapsesBySourceIndex.get(inputIndex).getPermanence();
   }
   return retVal;
 }
Esempio n. 2
0
  /**
   * Returns an array of permanence values
   *
   * @return
   */
  public double[] getSparsePermanences() {
    double[] retVal = new double[size];
    int[] keys = synapsesBySourceIndex.keys();
    for (int x = 0, j = size - 1; x < size; x++, j--) {
      retVal[j] = synapsesBySourceIndex.get(keys[x]).getPermanence();
    }

    return retVal;
  }
  public void setUp() {
    if (!map.isEmpty()) return;

    for (int j = 0; j < 2; j++) {
      for (Integer i : BenchmarkRunner.INTEGERS) {
        map.put(i.intValue(), i);
      }
    }
  }
Esempio n. 4
0
  public void testSplitClassesIdxs() throws Exception {
    final TIntObjectMap<TIntList> classesIdxs = MCTools.splitClassesIdxs(target);

    final TIntArrayList expectedForClass15 = new TIntArrayList(new int[] {0, 1, 2, 3, 4});
    assertTrue(Arrays.equals(expectedForClass15.toArray(), classesIdxs.get(15).toArray()));

    final TIntArrayList expectedForClass3 = new TIntArrayList(new int[] {9, 10, 11});
    assertTrue(Arrays.equals(expectedForClass3.toArray(), classesIdxs.get(3).toArray()));
  }
  public int[] mergeValues(final int x, final int z, final int sizeX, final int sizeZ) {
    final int gridX = x - 1;
    final int gridZ = z - 1;
    final int gridSizeX = sizeX + 2;
    final int gridSizeZ = sizeZ + 2;

    final int[] values = this.belowLayer.generateValues(gridX, gridZ, gridSizeX, gridSizeZ);
    final int[] eValues = this.variationLayer.generateValues(gridX, gridZ, gridSizeX, gridSizeZ);

    final int[] finalValues = new int[sizeX * sizeZ];
    for (int i = 0; i < sizeZ; i++) {
      for (int j = 0; j < sizeX; j++) {
        this.setCoordsSeed(x + j, z + i);
        final int centerValue = values[(j + 1 + ((i + 1) * gridSizeX))];
        final int variationValue = eValues[(j + 1 + ((i + 1) * gridSizeX))];
        if ((centerValue != 0) && (variationValue == 3) && (centerValue < VARIATION_MOD)) {
          finalValues[(j + (i * sizeX))] =
              (getByBiomeId(centerValue + VARIATION_MOD) != null)
                  ? (centerValue + VARIATION_MOD)
                  : centerValue;
        } else if ((variationValue == 2) || (this.nextInt(3) == 0)) {
          int val = centerValue;
          if (VARIATIONS.containsKey(centerValue)) {
            val = VARIATIONS.get(centerValue).get(this.nextInt(VARIATIONS.get(centerValue).size()));
          } else if ((centerValue == DEEP_OCEAN.getBiomeId()) && (this.nextInt(3) == 0)) {
            val = ISLANDS.get(this.nextInt(ISLANDS.size()));
          }
          if ((variationValue == 2) && (val != centerValue)) {
            val = (getByBiomeId(val + VARIATION_MOD) != null) ? (val + VARIATION_MOD) : centerValue;
          }
          if (val != centerValue) {
            int count = 0;
            if (values[(j + 1 + (i * gridSizeX))] == centerValue) { // upper value
              count++;
            }
            if (values[(j + 1 + ((i + 2) * gridSizeX))] == centerValue) { // lower value
              count++;
            }
            if (values[(j + ((i + 1) * gridSizeX))] == centerValue) { // left value
              count++;
            }
            if (values[(j + 2 + ((i + 1) * gridSizeX))] == centerValue) { // right value
              count++;
            }
            // spread mountains if not too close from an edge
            finalValues[(j + (i * sizeX))] = (count < 3) ? centerValue : val;
          } else {
            finalValues[(j + (i * sizeX))] = val;
          }
        } else {
          finalValues[(j + (i * sizeX))] = centerValue;
        }
      }
    }
    return finalValues;
  }
 /**
  * Actual setter.
  *
  * @param id Database ID
  * @param index column index
  * @param value new value
  * @param <T> type
  * @return previous value
  */
 @SuppressWarnings("unchecked")
 protected <T> T set(DBIDRef id, int index, T value) {
   Object[] d = data.get(DBIDUtil.asInteger(id));
   if (d == null) {
     d = new Object[rlen];
     data.put(DBIDUtil.asInteger(id), d);
   }
   T ret = (T) d[index];
   d[index] = value;
   return ret;
 }
Esempio n. 7
0
 /**
  * Updates this {@code Pool}'s store of permanences for the specified {@link Synapse}
  *
  * @param c the connections memory
  * @param s the synapse who's permanence is recorded
  * @param permanence the permanence value to record
  */
 public void updatePool(Connections c, Synapse s, double permanence) {
   int inputIndex = s.getInputIndex();
   if (synapsesBySourceIndex.get(inputIndex) == null) {
     synapsesBySourceIndex.put(inputIndex, s);
   }
   if (permanence > c.getSynPermConnected()) {
     synapseConnections.add(inputIndex);
   } else {
     synapseConnections.remove(inputIndex);
   }
 }
Esempio n. 8
0
  @Override
  public Path calcPath(int from, int to) {
    if (alreadyRun) throw new IllegalStateException("Create a new instance per call");
    alreadyRun = true;
    TIntObjectMap<AStarEdge> map = new TIntObjectHashMap<AStarEdge>();
    PriorityQueue<AStarEdge> prioQueueOpenSet = new PriorityQueue<AStarEdge>(1000);
    double toLat = graph.getLatitude(to);
    double toLon = graph.getLongitude(to);
    double currWeightToGoal, distEstimation, tmpLat, tmpLon;
    AStarEdge fromEntry = new AStarEdge(EdgeIterator.NO_EDGE, from, 0, 0);
    AStarEdge currEdge = fromEntry;
    while (true) {
      int currVertex = currEdge.endNode;
      EdgeIterator iter = neighbors(currVertex);
      while (iter.next()) {
        if (!accept(iter)) continue;
        int neighborNode = iter.adjNode();
        double alreadyVisitedWeight =
            weightCalc.getWeight(iter.distance(), iter.flags()) + currEdge.weightToCompare;
        AStarEdge nEdge = map.get(neighborNode);
        if (nEdge == null || nEdge.weightToCompare > alreadyVisitedWeight) {
          tmpLat = graph.getLatitude(neighborNode);
          tmpLon = graph.getLongitude(neighborNode);
          currWeightToGoal = dist.calcDist(toLat, toLon, tmpLat, tmpLon);
          currWeightToGoal = weightCalc.getMinWeight(currWeightToGoal);
          distEstimation = alreadyVisitedWeight + currWeightToGoal;
          if (nEdge == null) {
            nEdge = new AStarEdge(iter.edge(), neighborNode, distEstimation, alreadyVisitedWeight);
            map.put(neighborNode, nEdge);
          } else {
            prioQueueOpenSet.remove(nEdge);
            nEdge.edge = iter.edge();
            nEdge.weight = distEstimation;
            nEdge.weightToCompare = alreadyVisitedWeight;
          }
          nEdge.parent = currEdge;
          prioQueueOpenSet.add(nEdge);
          updateShortest(nEdge, neighborNode);
        }
      }

      visitedCount++;
      if (finished(currEdge, to)) break;
      if (prioQueueOpenSet.isEmpty()) return new Path(graph, flagEncoder);

      currEdge = prioQueueOpenSet.poll();
      if (currEdge == null) throw new AssertionError("cannot happen?");
    }

    return extractPath(currEdge);
  }
  public static void main(String[] args) {

    if (args.length != 2) {
      System.err.println(
          "Usage: MemoryBasedLeft2Right <in score file> <out aggregated scores redis file>");
      System.exit(0);
    }

    String infile = args[0];
    String redisSimilarityFile = args[1];

    try {

      BufferedReader reader = new BufferedReader(new FileReader(new File(infile)));
      String line;
      TIntObjectMap<TIntDoubleMap> scoresMap = new TIntObjectHashMap<TIntDoubleMap>();

      while ((line = reader.readLine()) != null) {
        String[] toks = line.split("\t");

        int id1 = Integer.parseInt(toks[0]);
        int id2 = Integer.parseInt(toks[1]);
        double score = Double.parseDouble(toks[2]);
        TIntDoubleMap scores = scoresMap.get(id1);
        if (scores == null) {
          scores = new TIntDoubleHashMap();
          scoresMap.put(id1, scores);
        }
        scores.put(id2, score);
      }
      reader.close();

      RedisBasedIDKeyPersistentBasicMap<LinkedHashMap<Integer, Double>> redis =
          new RedisBasedIDKeyPersistentBasicMap<LinkedHashMap<Integer, Double>>(
              redisSimilarityFile, false);
      redis.clear();
      TIntObjectIterator<TIntDoubleMap> it = scoresMap.iterator();
      while (it.hasNext()) {
        it.advance();
        int id1 = it.key();
        TIntDoubleMap scores = it.value();
        redis.put(id1, SortUtil.sortMapByValue(scores, true));
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * Actual getter.
  *
  * @param id Database ID
  * @param index column index
  * @param <T> type
  * @return current value
  */
 @SuppressWarnings("unchecked")
 protected <T> T get(DBIDRef id, int index) {
   Object[] d = data.get(DBIDUtil.asInteger(id));
   if (d == null) {
     return null;
   }
   return (T) d[index];
 }
Esempio n. 11
0
 /**
  * Replaces the contents of this vertex data by the provided one. This is a deep copy. The vertex
  * attribute are each individually cloned.
  *
  * @param data The data to copy.
  */
 public void copy(VertexData data) {
   clear();
   indices.addAll(data.indices);
   final TIntObjectIterator<VertexAttribute> iterator = data.attributes.iterator();
   while (iterator.hasNext()) {
     iterator.advance();
     attributes.put(iterator.key(), iterator.value().clone());
   }
   nameToIndex.putAll(data.nameToIndex);
 }
  public void run() {
    int total = 0;

    for (int i = 0; i < 5; i++) {
      TIntObjectIterator<Integer> iterator = map.iterator();
      while (iterator.hasNext()) {
        iterator.advance();
        total += iterator.key();
      }
    }
  }
  public FrequencyRandomOffsets(RaptorWorkerData data) {
    this.data = data;

    if (!data.hasFrequencies) return;

    data.timetablesForPattern
        .stream()
        .filter(tt -> tt.hasFrequencyTrips())
        .forEach(
            tt -> {
              offsets.put(tt.dataIndex, new int[tt.getFrequencyTripCount()]);
            });
  }
  public void randomize() {
    for (TIntObjectIterator<int[]> it = offsets.iterator(); it.hasNext(); ) {
      it.advance();
      int[] newVal = new int[it.value().length];

      RaptorWorkerTimetable tt = data.timetablesForPattern.get(it.key());

      for (int i = 0; i < newVal.length; i++) {
        newVal[i] = mt.nextInt(tt.headwaySecs[i]);
      }

      it.setValue(newVal);
    }
  }
Esempio n. 15
0
 /**
  * Uses 2 streetSearches to get P+R path
  *
  * <p>First CAR search from fromLat/fromLon to all car parks. Then from those found places WALK
  * search.
  *
  * <p>Result is then used as access part. Since P+R in direct mode is useless.
  *
  * @param request profileRequest from which from/to destination is used
  * @param streetRouter where profileRequest was already set
  * @return null if path isn't found
  */
 private StreetRouter findParkRidePath(ProfileRequest request, StreetRouter streetRouter) {
   streetRouter.streetMode = StreetMode.CAR;
   streetRouter.timeLimitSeconds = request.maxCarTime * 60;
   streetRouter.flagSearch = VertexStore.VertexFlag.PARK_AND_RIDE;
   streetRouter.dominanceVariable = StreetRouter.State.RoutingVariable.DURATION_SECONDS;
   if (streetRouter.setOrigin(request.fromLat, request.fromLon)) {
     streetRouter.route();
     TIntObjectMap<StreetRouter.State> carParks =
         streetRouter.getReachedVertices(VertexStore.VertexFlag.PARK_AND_RIDE);
     LOG.info("CAR PARK: Found {} car parks", carParks.size());
     StreetRouter walking = new StreetRouter(transportNetwork.streetLayer);
     walking.streetMode = StreetMode.WALK;
     walking.profileRequest = request;
     walking.timeLimitSeconds = request.maxCarTime * 60;
     walking.transitStopSearch = true;
     walking.setOrigin(carParks, CAR_PARK_DROPOFF_TIME_S, CAR_PARK_DROPOFF_COST, LegMode.CAR_PARK);
     walking.dominanceVariable = StreetRouter.State.RoutingVariable.DURATION_SECONDS;
     walking.route();
     walking.previousRouter = streetRouter;
     return walking;
   } else {
     return null;
   }
 }
Esempio n. 16
0
  @Test
  public void testTroveMapPreservesSynapseOrdering() {
    Synapse s = new Synapse();
    TIntObjectMap<Synapse> t = new TIntObjectHashMap<>();
    t.put(2, s);
    t.put(4, s);
    t.put(6, s);
    t.put(8, s);
    t.put(10, s);
    t.put(12, s);

    int[] expectedKeys = {12, 10, 8, 6, 4, 2};
    assertTrue(Arrays.equals(expectedKeys, t.keys()));

    // Insert a cell index in the middle, check that order is still
    // preserved following inserts
    t.put(3, s);
    expectedKeys = new int[] {12, 10, 8, 6, 4, 3, 2};
    assertTrue(Arrays.equals(expectedKeys, t.keys()));

    // Check reversing produces ascending ordered Synapses
    expectedKeys = new int[] {2, 3, 4, 6, 8, 10, 12};
    assertTrue(Arrays.equals(expectedKeys, ArrayUtils.reverse(t.keys())));
  }
Esempio n. 17
0
 public JumpTrack getTrack(int id) {
   return _jumpingTracks.get(id);
 }
Esempio n. 18
0
 public void addTrack(JumpTrack track) {
   _jumpingTracks.put(track.getId(), track);
 }
Esempio n. 19
0
 /**
  * Inserts a city.
  *
  * @param city The city to insert.
  * @return This CityManager.
  */
 public CityManager insertCity(City city) {
   cities.put(city.getId(), city);
   registerLandOwner(city);
   return this;
 }
Esempio n. 20
0
 /**
  * Gets a list of all cities on the server.
  *
  * @return
  */
 public List<City> getCityList() {
   return new ArrayList<City>(cities.valueCollection());
 }
Esempio n. 21
0
 /**
  * Gets a city from its integer id.
  *
  * @param id
  * @return
  */
 public City getCity(int id) {
   return cities.get(id);
 }
Esempio n. 22
0
 /**
  * Adds an attribute.
  *
  * @param attribute The attribute to add
  */
 public void addAttribute(int index, VertexAttribute attribute) {
   attributes.put(index, attribute);
   nameToIndex.put(attribute.getName(), index);
 }
Esempio n. 23
0
 /** Clears all the vertex data. */
 public void clear() {
   indices.clear();
   attributes.clear();
   nameToIndex.clear();
 }
Esempio n. 24
0
 /**
  * Returns the attribute count.
  *
  * @return The number of attributes
  */
 public int getAttributeCount() {
   return attributes.size();
 }
Esempio n. 25
0
 /**
  * Removes the attribute at the provided index. If no attribute is found, nothing will be removed.
  *
  * @param index The index of the attribute to remove
  */
 public void removeAttribute(int index) {
   attributes.remove(index);
   nameToIndex.remove(getAttributeName(index));
 }
Esempio n. 26
0
 /**
  * Returns true in an attribute can be found at the provided index.
  *
  * @param index The index to lookup
  * @return Whether or not an attribute is at the index
  */
 public boolean hasAttribute(int index) {
   return attributes.containsKey(index);
 }
Esempio n. 27
0
 public BgOpLogPartition get(int tableId) {
   return tableOplogMap.get(tableId);
 }
Esempio n. 28
0
 @Override
 public int size() {
   return _jumpingTracks.size();
 }
Esempio n. 29
0
 @Override
 public void clear() {
   _jumpingTracks.clear();
 }
Esempio n. 30
0
 /**
  * Returns the {@link VertexAttribute} at the desired index, or null if none is associated to the
  * index.
  *
  * @param index The index to lookup
  * @return The attribute, or null if none is associated to the index.
  */
 public VertexAttribute getAttribute(int index) {
   return attributes.get(index);
 }