@Override
  public boolean addJobToCurrent(Job j) throws Exception {

    IAtomicReference<Job> r = h.getAtomicReference("job-" + j.getWorkerId());

    if (r.get() != null || !r.isNull()) {
      boolean sent = false;
      while (!sent) {
        // always update
        for (String s : workers()) {
          if (jobFor(s) == null) {
            log.info(
                "Redirecting worker "
                    + j.getWorkerId()
                    + " to "
                    + s
                    + " due to work already being allocated");
            r = h.getAtomicReference("job-" + s);
            j.setWorkerId(s);
            sent = true;
          }
        }
      }
    }

    r.set(j);

    // iterate over jobs without the work/data
    j.setWork(null);

    jobs.add(j);

    return true;
  }
Example #2
0
  private static double computePo(
      final IScope scope,
      final IAgentFilter filter,
      final Map<Object, Integer> categoriesId,
      final GamaMatrix<Double> fuzzytransitions,
      final Double distance,
      final IList<Object> valsInit,
      final IList<Object> valsObs,
      final IList<Object> valsSim,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents,
      final int nbCat,
      final int nb,
      final IList<Double> similarities,
      final IList<Object> weights) {
    Map<IAgent, Integer> agsId = new TOrderedHashMap<IAgent, Integer>();
    for (int i = 0; i < agents.length(scope); i++) {
      agsId.put(agents.get(scope, i), i);
    }

    for (int i = 0; i < nb; i++) {
      Object valObs = valsObs.get(i);
      Object valSim = valsSim.get(i);
      Object valInit = valsInit.get(i);
      int valObsId = categoriesId.get(valObs);
      int valSimId = categoriesId.get(valSim);
      int valInitId = categoriesId.get(valInit);
      IAgent agent = agents.get(scope, i);
      double[] XaXs =
          computeXaXs(
              scope,
              filter,
              categoriesId,
              agsId,
              valObsId,
              valSimId,
              valInitId,
              fuzzytransitions,
              distance,
              agent,
              valsInit,
              valsObs,
              valsSim,
              agents,
              nbCat);
      similarities.add(FastMath.min(XaXs[0], XaXs[1]));
    }
    double meanSimilarity = 0;
    double total = 0;
    for (int i = 0; i < nb; i++) {
      double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
      double val = weight * similarities.get(i);
      total += weight;
      meanSimilarity += val;
    }
    meanSimilarity /= total;
    return meanSimilarity;
  }
 @Override
 public void addWorker(String worker) {
   heartbeat.put(worker, System.currentTimeMillis());
   if (!workers.contains(worker)) {
     log.info("Adding worker " + worker);
     workers.add(worker);
     log.info("Number of workers is now " + workers.size());
   }
 }
 /**
  * Adds an update to the current mini batch
  *
  * @param id the id of the worker who did the update
  * @param update the update to add
  */
 @Override
 public void addUpdate(String id, E update) {
   try {
     updateSaver().save(id, update);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
   updates.add(id);
 }
 @Override
 public IList<ISpecies> getSubSpecies(final IScope scope) {
   IList<ISpecies> subspecies = GamaListFactory.create(Types.SPECIES);
   GamlModelSpecies model = (GamlModelSpecies) scope.getModel().getSpecies();
   for (ISpecies s : model.getAllSpecies().values()) {
     if (s.getParentSpecies() == this) {
       subspecies.add(s);
     }
   }
   return subspecies;
 }
Example #6
0
  private static int buildRings(
      final IScope scope,
      final IAgentFilter filter,
      final Double distance,
      final List<Double> rings,
      final Map<Double, Integer> ringsPn,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents) {

    IList<ILocation> locs = GamaListFactory.create(Types.POINT);
    for (IAgent ag : agents.iterable(scope)) {
      locs.add(ag.getLocation());
    }
    ILocation centralLoc = (ILocation) Stats.getMean(scope, locs);
    IAgent centralAg = scope.getTopology().getAgentClosestTo(scope, centralLoc, filter);
    List<IAgent> neighbors =
        distance == 0 || filter == null
            ? new ArrayList<IAgent>()
            : new ArrayList<IAgent>(
                scope.getTopology().getNeighborsOf(scope, centralAg, distance, filter));

    for (IAgent ag : neighbors) {
      double dist = centralLoc.euclidianDistanceTo(ag.getLocation());
      if (dist == 0) {
        continue;
      }
      if (!rings.contains(dist)) {
        rings.add(dist);
        ringsPn.put(dist, 1);
      } else {
        ringsPn.put(dist, 1 + ringsPn.get(dist));
      }
    }
    Collections.sort(rings);

    for (int i = 1; i < rings.size(); i++) {
      double dist = rings.get(i);
      double dist1 = rings.get(i - 1);
      ringsPn.put(dist, ringsPn.get(dist) + ringsPn.get(dist1));
    }

    return rings.size();
  }
Example #7
0
  private static double computeSimilarity(
      final IScope scope,
      final IAgentFilter filter,
      final Double distance,
      final IList<Object> vals1,
      final IList<Object> vals2,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents,
      final int nbCat,
      final int nb,
      final double[][] crispVector1,
      final double[][] crispVector2,
      final boolean[] sim,
      final double[][] fuzzyVector1,
      final double[][] fuzzyVector2,
      final IList<Double> similarities,
      final IList<Object> weights) {
    Map<IAgent, Integer> agsId = new TOrderedHashMap<IAgent, Integer>();
    for (int i = 0; i < agents.length(scope); i++) {
      agsId.put(agents.get(scope, i), i);
    }

    for (int i = 0; i < nb; i++) {
      if (sim[i]) {
        similarities.add(1.0);
      } else {
        IAgent agent = agents.get(scope, i);
        // double sizeNorm = agent.getPerimeter() / 4.0;
        double sizeNorm = FastMath.sqrt(agent.getEnvelope().getArea());
        List<IAgent> neighbors =
            distance == 0 || filter == null
                ? new ArrayList<IAgent>()
                : new ArrayList<IAgent>(
                    scope.getTopology().getNeighborsOf(scope, agent, distance, filter));

        Map<IAgent, Double> distancesCoeff = new TOrderedHashMap<IAgent, Double>();
        distancesCoeff.put(agent, 1.0);
        for (IAgent ag : neighbors) {
          double euclidDist = agent.getLocation().euclidianDistanceTo(ag.getLocation());
          distancesCoeff.put(ag, 1 / (1.0 + euclidDist / sizeNorm));
        }
        for (int j = 0; j < nbCat; j++) {
          double max1 = 0.0;
          double max2 = 0.0;
          for (IAgent ag : neighbors) {
            int id = agsId.get(ag);
            double val1 = crispVector1[id][j] * distancesCoeff.get(ag);
            double val2 = crispVector2[id][j] * distancesCoeff.get(ag);

            if (val1 > max1) {
              max1 = val1;
            }
            if (val2 > max2) {
              max2 = val2;
            }
          }
          fuzzyVector1[i][j] = max1;
          fuzzyVector2[i][j] = max2;
        }
        double s1Max = -1 * Double.MAX_VALUE;
        double s2Max = -1 * Double.MAX_VALUE;

        for (int j = 0; j < nbCat; j++) {
          double s1 = FastMath.min(fuzzyVector1[i][j], crispVector2[i][j]);
          double s2 = FastMath.min(fuzzyVector2[i][j], crispVector1[i][j]);
          if (s1 > s1Max) {
            s1Max = s1;
          }
          if (s2 > s2Max) {
            s2Max = s2;
          }
        }
        similarities.add(FastMath.min(s1Max, s2Max));
      }
    }
    double meanSimilarity = 0;
    double total = 0;
    for (int i = 0; i < nb; i++) {
      double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
      double val = weight * similarities.get(i);
      total += weight;
      meanSimilarity += val;
    }
    meanSimilarity /= total;
    return meanSimilarity;
  }
Example #8
0
  private static void computeXaXsTransitions(
      final IScope scope,
      final IAgentFilter filter,
      final GamaMatrix<Double> fuzzytransitions,
      final Double distance,
      final IContainer<Integer, IAgent> agents,
      final int nbCat,
      final Map<List<Integer>, Map<Double, Double>> XaPerTransition,
      final Map<List<Integer>, Map<Double, Double>> XsPerTransition,
      final Set<Double> Xvals) {

    IList<ILocation> locs = GamaListFactory.create(Types.POINT);
    for (IAgent ag : agents.iterable(scope)) {
      locs.add(ag.getLocation());
    }
    ILocation centralLoc = (ILocation) Stats.getMean(scope, locs);
    if (filter != null) {
      IAgent centralAg = scope.getTopology().getAgentClosestTo(scope, centralLoc, filter);
      List<IAgent> neighbors =
          distance == 0
              ? new ArrayList<IAgent>()
              : new ArrayList<IAgent>(
                  scope.getTopology().getNeighborsOf(scope, centralAg, distance, filter));
      double sizeNorm = FastMath.sqrt(centralAg.getEnvelope().getArea());

      Map<IAgent, Double> distancesCoeff = new TOrderedHashMap<IAgent, Double>();
      distancesCoeff.put(centralAg, 1.0);
      for (IAgent ag : neighbors) {
        double euclidDist = centralAg.getLocation().euclidianDistanceTo(ag.getLocation());
        double dist = 1 / (1.0 + euclidDist / sizeNorm);
        distancesCoeff.put(ag, dist);
      }

      for (int i = 0; i < nbCat; i++) {
        for (int j = 0; j < nbCat; j++) {
          for (int k = 0; k < nbCat; k++) {
            List<Integer> ca = new ArrayList();
            ca.add(i);
            ca.add(j);
            ca.add(k);
            double xa = 0;
            double xs = 0;
            for (IAgent ag : distancesCoeff.keySet()) {
              double dist = distancesCoeff.get(ag);
              double xatmp = fuzzyTransition(scope, fuzzytransitions, nbCat, i, k, i, j) * dist;
              double xstmp = fuzzyTransition(scope, fuzzytransitions, nbCat, i, j, i, k) * dist;
              if (xatmp > xa) {
                xa = xatmp;
              }
              if (xstmp > xs) {
                xs = xstmp;
              }
            }
            if (xa > 0) {

              Map<Double, Double> mapxa = XaPerTransition.get(ca);
              if (mapxa == null) {
                mapxa = new TOrderedHashMap<Double, Double>();
                mapxa.put(xa, 1.0);
                XaPerTransition.put(ca, mapxa);
              } else {
                if (mapxa.containsKey(xa)) {
                  mapxa.put(xa, mapxa.get(xa) + 1.0);
                } else {
                  mapxa.put(xa, 1.0);
                }
              }
              Xvals.add(xa);
            }
            if (xs > 0) {
              Map<Double, Double> mapxs = XsPerTransition.get(ca);
              if (mapxs == null) {
                mapxs = new TOrderedHashMap<Double, Double>();
                mapxs.put(xs, 1.0);
                XsPerTransition.put(ca, mapxs);
              } else {
                if (mapxs.containsKey(xa)) {
                  mapxs.put(xs, mapxs.get(xs) + 1.0);
                } else {
                  mapxs.put(xs, 1.0);
                }
              }
              Xvals.add(xs);
            }
          }
        }
      }
    }
  }
 @Override
 public void availableForWork(String id) {
   if (!workers.contains(id)) workers.add(id);
 }
 @Override
 public void addTopic(String topic) throws Exception {
   topics.add(topic);
 }
 /**
  * Adds a worker to the list to be replicate d
  *
  * @param workerId the worker id to add
  */
 @Override
 public void addReplicate(String workerId) {
   if (!replicate.contains(workerId)) replicate.add(workerId);
 }