/**
   * Neural networks with only one type of activation function offer certain optimization options.
   * This method determines if only a single activation function is used.
   *
   * @return The number of the single activation function, or -1 if there are no activation
   *     functions or more than one type of activation function.
   */
  public Class<?> hasSameActivationFunction() {
    final List<Class<?>> map = new ArrayList<Class<?>>();

    for (final ActivationFunction activation : this.activationFunctions) {
      if (!map.contains(activation.getClass())) {
        map.add(activation.getClass());
      }
    }

    if (map.size() != 1) {
      return null;
    } else {
      return map.get(0);
    }
  }
  /** {@inheritDoc} */
  @Override
  public void save(final OutputStream os, final Object obj) {
    final EncogWriteHelper out = new EncogWriteHelper(os);
    final RBFNetwork net = (RBFNetwork) obj;
    final FlatNetworkRBF flat = (FlatNetworkRBF) net.getFlat();
    out.addSection("RBF-NETWORK");
    out.addSubSection("PARAMS");
    out.addProperties(net.getProperties());
    out.addSubSection("NETWORK");
    out.writeProperty(BasicNetwork.TAG_BEGIN_TRAINING, flat.getBeginTraining());
    out.writeProperty(BasicNetwork.TAG_CONNECTION_LIMIT, flat.getConnectionLimit());
    out.writeProperty(BasicNetwork.TAG_CONTEXT_TARGET_OFFSET, flat.getContextTargetOffset());
    out.writeProperty(BasicNetwork.TAG_CONTEXT_TARGET_SIZE, flat.getContextTargetSize());
    out.writeProperty(BasicNetwork.TAG_END_TRAINING, flat.getEndTraining());
    out.writeProperty(BasicNetwork.TAG_HAS_CONTEXT, flat.getHasContext());
    out.writeProperty(PersistConst.INPUT_COUNT, flat.getInputCount());
    out.writeProperty(BasicNetwork.TAG_LAYER_COUNTS, flat.getLayerCounts());
    out.writeProperty(BasicNetwork.TAG_LAYER_FEED_COUNTS, flat.getLayerFeedCounts());
    out.writeProperty(BasicNetwork.TAG_LAYER_CONTEXT_COUNT, flat.getLayerContextCount());
    out.writeProperty(BasicNetwork.TAG_LAYER_INDEX, flat.getLayerIndex());
    out.writeProperty(PersistConst.OUTPUT, flat.getLayerOutput());
    out.writeProperty(PersistConst.OUTPUT_COUNT, flat.getOutputCount());
    out.writeProperty(BasicNetwork.TAG_WEIGHT_INDEX, flat.getWeightIndex());
    out.writeProperty(PersistConst.WEIGHTS, flat.getWeights());
    out.writeProperty(BasicNetwork.TAG_BIAS_ACTIVATION, flat.getBiasActivation());
    out.addSubSection("ACTIVATION");
    for (final ActivationFunction af : flat.getActivationFunctions()) {
      out.addColumn(af.getClass().getSimpleName());
      for (int i = 0; i < af.getParams().length; i++) {
        out.addColumn(af.getParams()[i]);
      }
      out.writeLine();
    }
    out.addSubSection("RBF");
    for (final RadialBasisFunction rbf : flat.getRBF()) {
      out.addColumn(rbf.getClass().getSimpleName());
      out.addColumn(rbf.getWidth());
      out.addColumn(rbf.getPeak());
      for (int i = 0; i < rbf.getCenters().length; i++) {
        out.addColumn(rbf.getCenters()[i]);
      }
      out.writeLine();
    }

    out.flush();
  }
  /** {@inheritDoc} */
  @Override
  public void save(final OutputStream os, final Object obj) {
    final EncogWriteHelper out = new EncogWriteHelper(os);
    final BasicNetwork net = (BasicNetwork) obj;
    final FlatNetwork flat = net.getStructure().getFlat();
    out.addSection("BASIC");
    out.addSubSection("PARAMS");
    out.addProperties(net.getProperties());
    out.addSubSection("NETWORK");

    out.writeProperty(BasicNetwork.TAG_BEGIN_TRAINING, flat.getBeginTraining());
    out.writeProperty(BasicNetwork.TAG_CONNECTION_LIMIT, flat.getConnectionLimit());
    out.writeProperty(BasicNetwork.TAG_CONTEXT_TARGET_OFFSET, flat.getContextTargetOffset());
    out.writeProperty(BasicNetwork.TAG_CONTEXT_TARGET_SIZE, flat.getContextTargetSize());
    out.writeProperty(BasicNetwork.TAG_END_TRAINING, flat.getEndTraining());
    out.writeProperty(BasicNetwork.TAG_HAS_CONTEXT, flat.getHasContext());
    out.writeProperty(PersistConst.INPUT_COUNT, flat.getInputCount());
    out.writeProperty(BasicNetwork.TAG_LAYER_COUNTS, flat.getLayerCounts());
    out.writeProperty(BasicNetwork.TAG_LAYER_FEED_COUNTS, flat.getLayerFeedCounts());
    out.writeProperty(BasicNetwork.TAG_LAYER_CONTEXT_COUNT, flat.getLayerContextCount());
    out.writeProperty(BasicNetwork.TAG_LAYER_INDEX, flat.getLayerIndex());
    out.writeProperty(PersistConst.OUTPUT, flat.getLayerOutput());
    out.writeProperty(PersistConst.OUTPUT_COUNT, flat.getOutputCount());
    out.writeProperty(BasicNetwork.TAG_WEIGHT_INDEX, flat.getWeightIndex());
    out.writeProperty(PersistConst.WEIGHTS, flat.getWeights());
    out.writeProperty(BasicNetwork.TAG_BIAS_ACTIVATION, flat.getBiasActivation());
    out.addSubSection("ACTIVATION");
    for (final ActivationFunction af : flat.getActivationFunctions()) {
      String sn = af.getClass().getSimpleName();
      // if this is an Encog class then only add the simple name, so it works with C#
      if (sn.startsWith("org.encog.")) {
        out.addColumn(sn);
      } else {
        out.addColumn(af.getClass().getName());
      }
      for (int i = 0; i < af.getParams().length; i++) {
        out.addColumn(af.getParams()[i]);
      }
      out.writeLine();
    }

    out.flush();
  }
  /** {@inheritDoc} */
  @Override
  public ActivationFunction createActivationFunction(String fn) {
    String name;
    double[] params;

    int index = fn.indexOf('[');
    if (index != -1) {
      name = fn.substring(0, index).toLowerCase();
      int index2 = fn.indexOf(']');
      if (index2 == -1) {
        throw new EncogError("Unbounded [ while parsing activation function.");
      }
      String a = fn.substring(index + 1, index2);
      params = NumberList.fromList(CSVFormat.EG_FORMAT, a);

    } else {
      name = fn.toLowerCase();
      params = new double[0];
    }

    ActivationFunction af = allocateAF(name);

    if (af == null) {
      return null;
    }

    if (af.getParamNames().length != params.length) {
      throw new EncogError(
          name
              + " expected "
              + af.getParamNames().length
              + ", but "
              + params.length
              + " were provided.");
    }

    for (int i = 0; i < af.getParamNames().length; i++) {
      af.setParam(i, params[i]);
    }

    return af;
  }
  /** {@inheritDoc} */
  @Override
  public void calculateError(
      ActivationFunction af,
      double[] b,
      double[] a,
      double[] ideal,
      double[] actual,
      double[] error,
      double derivShift,
      double significance) {

    for (int i = 0; i < actual.length; i++) {
      double deriv = af.derivativeFunction(b[i], a[i]) + derivShift;
      error[i] = ((ideal[i] - actual[i]) * significance) * deriv;
    }
  }
  /** {@inheritDoc} */
  @Override
  public Object read(final InputStream is) {
    final BasicNetwork result = new BasicNetwork();
    final FlatNetwork flat = new FlatNetwork();
    final EncogReadHelper in = new EncogReadHelper(is);
    EncogFileSection section;

    while ((section = in.readNextSection()) != null) {
      if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("PARAMS")) {
        final Map<String, String> params = section.parseParams();
        result.getProperties().putAll(params);
      }
      if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("NETWORK")) {
        final Map<String, String> params = section.parseParams();

        flat.setBeginTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_BEGIN_TRAINING));
        flat.setConnectionLimit(
            EncogFileSection.parseDouble(params, BasicNetwork.TAG_CONNECTION_LIMIT));
        flat.setContextTargetOffset(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_OFFSET));
        flat.setContextTargetSize(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_SIZE));
        flat.setEndTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_END_TRAINING));
        flat.setHasContext(EncogFileSection.parseBoolean(params, BasicNetwork.TAG_HAS_CONTEXT));
        flat.setInputCount(EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT));
        flat.setLayerCounts(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_COUNTS));
        flat.setLayerFeedCounts(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_FEED_COUNTS));
        flat.setLayerContextCount(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_CONTEXT_COUNT));
        flat.setLayerIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_INDEX));
        flat.setLayerOutput(section.parseDoubleArray(params, PersistConst.OUTPUT));
        flat.setLayerSums(new double[flat.getLayerOutput().length]);
        flat.setOutputCount(EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT));
        flat.setWeightIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_WEIGHT_INDEX));
        flat.setWeights(section.parseDoubleArray(params, PersistConst.WEIGHTS));
        flat.setBiasActivation(section.parseDoubleArray(params, BasicNetwork.TAG_BIAS_ACTIVATION));
      } else if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("ACTIVATION")) {
        int index = 0;

        flat.setActivationFunctions(new ActivationFunction[flat.getLayerCounts().length]);

        for (final String line : section.getLines()) {
          ActivationFunction af = null;
          final List<String> cols = EncogFileSection.splitColumns(line);

          // if this is a class name with a path, then do not default to inside of the Encog
          // package.
          String name;
          if (cols.get(0).indexOf('.') != -1) {
            name = cols.get(0);
          } else {
            name = "org.encog.engine.network.activation." + cols.get(0);
          }

          try {
            final Class<?> clazz = Class.forName(name);
            af = (ActivationFunction) clazz.newInstance();
          } catch (final ClassNotFoundException e) {
            throw new PersistError(e);
          } catch (final InstantiationException e) {
            throw new PersistError(e);
          } catch (final IllegalAccessException e) {
            throw new PersistError(e);
          }

          for (int i = 0; i < af.getParamNames().length; i++) {
            af.setParam(i, CSVFormat.EG_FORMAT.parse(cols.get(i + 1)));
          }

          flat.getActivationFunctions()[index++] = af;
        }
      }
    }

    result.getStructure().setFlat(flat);
    result.updateProperties();
    return result;
  }
 private double calculateRange(ActivationFunction af, double r) {
   double[] d = {r};
   af.activationFunction(d, 0, 1);
   return d[0];
 }
  /** {@inheritDoc} */
  @Override
  public Object read(final InputStream is) {
    final RBFNetwork result = new RBFNetwork();
    final FlatNetworkRBF flat = (FlatNetworkRBF) result.getFlat();

    final EncogReadHelper in = new EncogReadHelper(is);
    EncogFileSection section;

    while ((section = in.readNextSection()) != null) {
      if (section.getSectionName().equals("RBF-NETWORK")
          && section.getSubSectionName().equals("PARAMS")) {
        final Map<String, String> params = section.parseParams();
        result.getProperties().putAll(params);
      }
      if (section.getSectionName().equals("RBF-NETWORK")
          && section.getSubSectionName().equals("NETWORK")) {
        final Map<String, String> params = section.parseParams();

        flat.setBeginTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_BEGIN_TRAINING));
        flat.setConnectionLimit(
            EncogFileSection.parseDouble(params, BasicNetwork.TAG_CONNECTION_LIMIT));
        flat.setContextTargetOffset(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_OFFSET));
        flat.setContextTargetSize(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_CONTEXT_TARGET_SIZE));
        flat.setEndTraining(EncogFileSection.parseInt(params, BasicNetwork.TAG_END_TRAINING));
        flat.setHasContext(EncogFileSection.parseBoolean(params, BasicNetwork.TAG_HAS_CONTEXT));
        flat.setInputCount(EncogFileSection.parseInt(params, PersistConst.INPUT_COUNT));
        flat.setLayerCounts(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_COUNTS));
        flat.setLayerFeedCounts(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_FEED_COUNTS));
        flat.setLayerContextCount(
            EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_CONTEXT_COUNT));
        flat.setLayerIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_LAYER_INDEX));
        flat.setLayerOutput(section.parseDoubleArray(params, PersistConst.OUTPUT));
        flat.setLayerSums(new double[flat.getLayerOutput().length]);
        flat.setOutputCount(EncogFileSection.parseInt(params, PersistConst.OUTPUT_COUNT));
        flat.setWeightIndex(EncogFileSection.parseIntArray(params, BasicNetwork.TAG_WEIGHT_INDEX));
        flat.setWeights(section.parseDoubleArray(params, PersistConst.WEIGHTS));
        flat.setBiasActivation(section.parseDoubleArray(params, BasicNetwork.TAG_BIAS_ACTIVATION));
      } else if (section.getSectionName().equals("RBF-NETWORK")
          && section.getSubSectionName().equals("ACTIVATION")) {
        int index = 0;

        flat.setActivationFunctions(new ActivationFunction[flat.getLayerCounts().length]);

        for (final String line : section.getLines()) {
          ActivationFunction af = null;
          final List<String> cols = EncogFileSection.splitColumns(line);
          final String name = "org.encog.engine.network.activation." + cols.get(0);
          try {
            final Class<?> clazz = Class.forName(name);
            af = (ActivationFunction) clazz.newInstance();
          } catch (final ClassNotFoundException e) {
            throw new PersistError(e);
          } catch (final InstantiationException e) {
            throw new PersistError(e);
          } catch (final IllegalAccessException e) {
            throw new PersistError(e);
          }

          for (int i = 0; i < af.getParamNames().length; i++) {
            af.setParam(i, CSVFormat.EG_FORMAT.parse(cols.get(i + 1)));
          }

          flat.getActivationFunctions()[index++] = af;
        }

      } else if (section.getSectionName().equals("RBF-NETWORK")
          && section.getSubSectionName().equals("RBF")) {
        int index = 0;

        final int hiddenCount = flat.getLayerCounts()[1];
        final int inputCount = flat.getLayerCounts()[2];

        flat.setRBF(new RadialBasisFunction[hiddenCount]);

        for (final String line : section.getLines()) {
          RadialBasisFunction rbf = null;
          final List<String> cols = EncogFileSection.splitColumns(line);
          final String name = "org.encog.mathutil.rbf." + cols.get(0);
          try {
            final Class<?> clazz = Class.forName(name);
            rbf = (RadialBasisFunction) clazz.newInstance();
          } catch (final ClassNotFoundException e) {
            throw new PersistError(e);
          } catch (final InstantiationException e) {
            throw new PersistError(e);
          } catch (final IllegalAccessException e) {
            throw new PersistError(e);
          }

          rbf.setWidth(CSVFormat.EG_FORMAT.parse(cols.get(1)));
          rbf.setPeak(CSVFormat.EG_FORMAT.parse(cols.get(2)));
          rbf.setCenters(new double[inputCount]);

          for (int i = 0; i < inputCount; i++) {
            rbf.getCenters()[i] = CSVFormat.EG_FORMAT.parse(cols.get(i + 3));
          }

          flat.getRBF()[index++] = rbf;
        }
      }
    }

    return result;
  }