Package org.encog.ml.prg.extension

Examples of org.encog.ml.prg.extension.ProgramExtensionTemplate


   
   
    // populate the opcodes
    if (this.getScript().getOpcodes().size() > 0) {
      for (ScriptOpcode op : this.getScript().getOpcodes()) {
        ProgramExtensionTemplate temp = EncogOpcodeRegistry.INSTANCE
            .findOpcode(op.getName(), op.getArgCount());
        pop.getContext().getFunctions().addExtension(temp);
      }
    }
   
View Full Code Here


    final List<ProgramExtensionTemplate> opcodeSet = getContext()
        .getFunctions().findOpcodes(types, getContext(),
            includeTerminal, includeFunction);

    // choose a random opcode
    final ProgramExtensionTemplate temp = generateRandomOpcode(rnd,
        opcodeSet);
    if (temp == null) {
      throw new EACompileError(
          "Trying to generate a random opcode when no opcodes exist.");
    }

    // create the child nodes
    final int childNodeCount = temp.getChildNodeCount();
    final ProgramNode[] children = new ProgramNode[childNodeCount];

    if (temp.getNodeType().isOperator() && children.length >= 2) {

      // for an operator of size 2 or greater make sure all children are
      // the same time
      final List<ValueType> childTypes = temp.getParams().get(0)
          .determineArgumentTypes(types);
      final ValueType selectedType = childTypes.get(rnd
          .nextInt(childTypes.size()));
      childTypes.clear();
      childTypes.add(selectedType);

      // now create the children of a common type
      for (int i = 0; i < children.length; i++) {
        children[i] = createNode(rnd, program, depthRemaining - 1,
            childTypes);
      }
    } else {

      // otherwise, let the children have their own types
      for (int i = 0; i < children.length; i++) {
        final List<ValueType> childTypes = temp.getParams().get(i)
            .determineArgumentTypes(types);
        children[i] = createNode(rnd, program, depthRemaining - 1,
            childTypes);
      }
    }

    // now actually create the node
    final ProgramNode result = new ProgramNode(program, temp, children);
    temp.randomize(rnd, types, result, getMinConst(), getMaxConst());
    return result;
  }
View Full Code Here

   * @param types The types that we might generate.
   * @return The terminal program node.
   */
  public ProgramNode createTerminalNode(final Random rnd,
      final EncogProgram program, final List<ValueType> types) {
    final ProgramExtensionTemplate temp = generateRandomOpcode(
        rnd,
        getContext().getFunctions().findOpcodes(types, this.context,
            true, false));
    if (temp == null) {
      throw new EACompileError("No opcodes exist for the type: "
          + types.toString());
    }
    final ProgramNode result = new ProgramNode(program, temp,
        new ProgramNode[] {});

    temp.randomize(rnd, types, result, this.minConst, this.maxConst);
    return result;
  }
View Full Code Here

      return null;
    }

    int tries = 10000;

    ProgramExtensionTemplate result = null;

    while (result == null) {
      final int opcode = rnd.nextInt(maxOpCode);
      result = opcodes.get(opcode);
      tries--;
View Full Code Here

    int varIndex = (int)node.getData()[0].toIntValue();
    return this.program.getVariables().getVariableName(varIndex);
  }

  private String handleFunction(ProgramNode node) {
    ProgramExtensionTemplate temp = node.getTemplate();
    StringBuilder result = new StringBuilder();

    if (temp == StandardExtensions.EXTENSION_SQRT) {
      result.append("\\sqrt{");
      result.append(renderNode(node.getChildNode(0)));
      result.append("}");
    } else {
      result.append(temp.getName());
      result.append('(');
      for (int i = 0; i < temp.getChildNodeCount(); i++) {
        if (i > 0) {
          result.append(',');
        }
        result.append(renderNode(node.getChildNode(i)));
      }
View Full Code Here

    }
    return result.toString();
  }

  private String handleOperator(ProgramNode node) {
    ProgramExtensionTemplate temp = node.getTemplate();
    StringBuilder result = new StringBuilder();

    if (temp.getChildNodeCount() == 2) {
      String a = renderNode(node.getChildNode(0));
      String b = renderNode(node.getChildNode(1));

      if (temp == StandardExtensions.EXTENSION_DIV) {
        result.append("\\frac{");
        result.append(b);
        result.append("}{");
        result.append(a);
        result.append("}");
      } else if (temp == StandardExtensions.EXTENSION_MUL) {
        result.append("(");
        result.append(b);
        result.append("\\cdot ");
        result.append(a);
        result.append(")");
      } else {
        result.append("(");
        result.append(b);
        result.append(temp.getName());
        result.append(a);
        result.append(")");
      }

    } else if (temp.getChildNodeCount() == 1) {
      String a = renderNode(node.getChildNode(0));
      result.append("(");
      result.append(temp.getName());
      result.append(a);
      result.append(")");
    } else {
      throw new EACompileError(
          "An operator must have an arity of 1 or 2, probably should be made a function.");
View Full Code Here

        v = this.holder.getFunctions().factorProgramNode("-", holder,
            new ProgramNode[] { v });
      }
      outputQueue(v);
    } else {
      ProgramExtensionTemplate temp = this.holder.getFunctions()
          .findFunction(varName.toString());
      if (temp == null) {
        throw new EACompileError("Undefined function: "
            + varName.toString());
      }
View Full Code Here

    char ch2 = 0;
    if (!this.parser.eol()) {
      ch2 = this.parser.peek();
    }

    ProgramExtensionTemplate temp = this.holder.getFunctions()
        .findOperator(ch1, ch2);

    // did we find anything?
    if (temp != null) {
      // if we found a 2-char operator, then advance beyond the 2nd
      // char
      if (temp.getName().length() > 1) {
        this.parser.advance();
      }
      functionQueue(temp);
    } else {
      throw new EACompileError("Unknown symbol: " + ch1);
View Full Code Here

      }
    }

    // pop off any functions still on the stack
    while (!this.functionStack.isEmpty()) {
      ProgramExtensionTemplate f = this.functionStack.pop();
      outputQueue(f);
    }

    // were there no operators?
    if (this.rootNode == null) {
View Full Code Here

     
      // parse out the command
      StringTokenizer tok = new StringTokenizer(cmd.toString(),":");
      String name = tok.nextToken();
      int childCount = Integer.parseInt(tok.nextToken());
      ProgramExtensionTemplate temp = EncogOpcodeRegistry.INSTANCE.findOpcode(name, childCount);
      if( temp==null ) {
        throw new EACompileError("Invalid instruction: " + name);
      }
     
      // build the arguments
      ProgramNode[] args = new ProgramNode[childCount];
      for(int i=args.length-1;i>=0;i--) {
        args[i] = this.nodeStack.pop();
      }
     
      // factor the node
      ProgramNode node = this.holder.getFunctions().factorProgramNode(name, this.holder, args);
      this.nodeStack.push(node);
     
      // add any needed data to the node
      for(int i=0;i<temp.getDataSize();i++) {
        String str = tok.nextToken().trim();
        int idx = str.indexOf('#');
        if( idx!=-1) {
          int enumType = Integer.parseInt(str.substring(0,idx));
          int enumVal = Integer.parseInt(str.substring(idx+1));
View Full Code Here

TOP

Related Classes of org.encog.ml.prg.extension.ProgramExtensionTemplate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.