private double selectDeath(RandomGenerator e, C conf, M modif, double[] out) {
   int size = conf.size(clazz);
   if (size < this.n) {
     return 0.;
   }
   int denom = 1;
   int d[] = new int[this.n];
   for (int i = 0; i < this.n; ++i, --size) {
     d[i] = (size == 1) ? 0 : sample(e, size);
     for (int j = 0; j < i; ++j) if (d[j] <= d[i]) ++d[i]; // skip already selected indices
     for (int j = 0; j < i; ++j)
       if (d[j] == d[i]) {
         LOGGER.error("sampled " + d[i] + " twice");
       }
     Iterator<T> it = conf.iterator(clazz);
     for (int j = 0; j < d[i]; j++) {
       it.next();
     }
     T t = it.next();
     modif.insertDeath(t);
     double[] outTmp = new double[this.builder.size()];
     this.builder.setCoordinates(t, outTmp);
     for (int j = 0; j < this.builder.size(); j++) {
       out[i * this.builder.size() + j] = outTmp[j];
     }
     denom *= size;
   }
   return 1. / (double) denom;
 }
 public void write(Bytes bytes, C c) {
   if (c == null) {
     bytes.writeStopBit(NULL_LENGTH);
     return;
   }
   bytes.writeStopBit(c.size());
   for (E e : c) {
     eBytesMarshaller.write(bytes, e);
   }
 }
Exemple #3
0
  /**
   * 根据指定根节点集合<br>
   * 以及具有parentId的集合生成树(将子节点set入树中) <功能详细描述>
   *
   * @param treeAbleCollection
   * @param parentNodeCollection
   * @param maxLevelIndex 生成树的最大层数如果maxLevelIndex<=0则认为无需限制层数<br>
   *     maxLevelIndex = 1直接返回根节点 maxLevelIndex = 2认为有两层节点
   * @return [参数说明]
   * @return C [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  @SuppressWarnings("rawtypes")
  public static <C extends Collection<T>, T extends TreeAble> C changToTree(
      C treeAbleCollection, C parentNodeCollection, int maxLevelIndex) {
    // 判断入参是否合法
    if (treeAbleCollection == null
        || parentNodeCollection == null
        || treeAbleCollection.size() == 0
        || parentNodeCollection.size() == 0) {
      // 如果root或需要转换的集合其中有一个为空,直接返回root集合当作结果
      return parentNodeCollection;
    }

    // 循环得到当前传入list上级节点有多少个
    // 并进行非法节点过滤,如果某节点id  = parentId将会造成死循环 ,将这样的节点抛弃
    Map<String, C> parentIdIndexMap = new HashMap<String, C>();
    for (T treeAbleTemp : treeAbleCollection) {
      String superId = treeAbleTemp.getParentId();
      String id = treeAbleTemp.getId();
      if (StringUtils.isEmpty(id) || id.equals(superId)) {
        // 并进行非法节点过滤,如果某节点id  = parentId将会造成死循环
        // doNothing
        continue;
      }

      // 根据父节点形成父ID与集合的映射
      if (parentIdIndexMap.containsKey(superId)) {
        parentIdIndexMap.get(superId).add(treeAbleTemp);
      } else {
        C collectionTemp = newCollectionInstance(treeAbleCollection);
        collectionTemp.add(treeAbleTemp);
        parentIdIndexMap.put(superId, collectionTemp);
      }
    }

    return changeToTreeByParentIdIndexMap(parentNodeCollection, maxLevelIndex, parentIdIndexMap);
  }
 private double selectBirth(RandomGenerator e, C conf, M modif, double[] in) {
   int beg = conf.size(clazz) - modif.getDeath().size() + 1;
   int end = beg + this.n;
   int denom = 1;
   int current = 0;
   for (int size = beg; size < end; ++size) {
     double[] v = new double[this.dimension];
     for (int i = 0; i < this.dimension; i++) {
       v[i] = in[i + current];
     }
     modif.insertBirth(this.builder.build(v));
     current += this.dimension;
     denom *= size;
   }
   return 1. / (double) denom;
 }
Exemple #5
0
  /**
   * <功能简述> <功能详细描述>
   *
   * @param parentNodeCollection
   * @param maxLevelIndex
   * @param parentIdIndexMap
   * @return [参数说明]
   * @return C [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  @SuppressWarnings("rawtypes")
  private static <C extends Collection<T>, T extends TreeAble> C changeToTreeByParentIdIndexMap(
      C parentNodeCollection, int maxLevelIndex, Map<String, C> parentIdIndexMap) {
    // 如果合法的节点映射得到的集合为空
    if (parentIdIndexMap.size() == 0) {
      return parentNodeCollection;
    }

    /** 将子节点放入父节点 */
    /** 并迭代进行下一级的节点放入 */
    C allNextChildCollecion = setNextLevelTree(parentIdIndexMap, parentNodeCollection);

    /** 开始迭代 */
    int nowTreeIndex = 1;
    while (allNextChildCollecion != null && allNextChildCollecion.size() > 0) {
      if (maxLevelIndex > 0 && nowTreeIndex >= nowTreeIndex) {
        // 如果指定了tree的最大迭代层次,则根据该值不再迭代获取当前代的下一代子节点
        break;
      }
      allNextChildCollecion = setNextLevelTree(parentIdIndexMap, allNextChildCollecion);
    }

    return parentNodeCollection;
  }
 @Override
 public <C extends Collection<?>> Expression<Integer> size(C c) {
   int size = c == null ? 0 : c.size();
   return new LiteralExpression<>(this, Integer.class, size);
 }
 /** {@inheritDoc} */
 @Override
 public int size() {
   return collection.size();
 }
Exemple #8
0
 public static <C extends Collection<?> & Observable> EventStream<Integer> sizeOf(C collection) {
   return create(() -> collection.size(), collection);
 }
 /** {@inheritDoc} */
 @Override
 public <C extends Collection<?>> Expression<Integer> size(C collection) {
   return new EntityConstantExpression<Integer>(null, collection != null ? collection.size() : 0);
 }
        @Override
        public void readMarshallable(WireIn wire) throws IllegalStateException {
          @SuppressWarnings("ConstantConditions")
          final Bytes<?> outBytes = outWire.bytes();

          try {

            final StringBuilder eventName = Wires.acquireStringBuilder();
            @SuppressWarnings("ConstantConditions")
            final ValueIn valueIn = inWire.readEventName(eventName);

            outWire.writeDocument(
                true, w -> w.writeEventName(CoreFields.tid).int64(CollectionWireHandler.this.tid));

            outWire.writeDocument(
                false,
                out -> {

                  // note :  remove on the key-set returns a boolean and on the map returns the
                  // old value
                  if (EventId.remove.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(fromWire.apply(valueIn)));
                    return;
                  }

                  // note :  remove on the key-set returns a boolean and on the map returns the
                  // old value
                  if (EventId.iterator.contentEquals(eventName)) {
                    final ValueOut valueOut = out.writeEventName(CoreFields.reply);
                    valueOut.sequence(v -> underlyingCollection.forEach(e -> toWire.accept(v, e)));
                    return;
                  }

                  if (EventId.numberOfSegments.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).int32(1);
                    return;
                  }

                  if (EventId.isEmpty.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).bool(underlyingCollection.isEmpty());
                    return;
                  }

                  if (EventId.size.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).int32(underlyingCollection.size());
                    return;
                  }

                  if (EventId.clear.contentEquals(eventName)) {
                    underlyingCollection.clear();
                    return;
                  }

                  if (EventId.contains.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.contains(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.add.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.add(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.remove.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.containsAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(collectionFromWire()));
                    return;
                  }

                  if (EventId.addAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.addAll(collectionFromWire()));
                    return;
                  }

                  if (EventId.removeAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.removeAll(collectionFromWire()));
                    return;
                  }

                  if (EventId.retainAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.retainAll(collectionFromWire()));
                    return;
                  }

                  throw new IllegalStateException("unsupported event=" + eventName);
                });
          } catch (Exception e) {
            LOG.error("", e);
          } finally {

            if (YamlLogging.showServerWrites) {
              long len = outBytes.writePosition();
              if (len >= SIZE_OF_SIZE) {
                String s = Wires.fromSizePrefixedBlobs(outBytes);

                LOG.info("server writes:\n\n" + s);
              }
            }
          }
        }