Beispiel #1
0
        private static Expression CreateAssignment(GeneLink b, int i,
                                                   Func <string, Expression, Expression> accumulatorFunc)
        {
            Expression ass;
            var        from = Formater.FormatParameter(b.From, i);
            var        to   = Formater.FormatParameter(b.To, i + 1);

            if (b.IsPositive)
            {
                //ass = new Assignment(b.To,
                //    new Variable(b.From));

                ass = accumulatorFunc.Invoke(to, new Variable(from));
            }
            else
            {
                ass = accumulatorFunc.Invoke(to,
                                             new PrimitiveApplication(
                                                 PrimitiveApplication.AND, new PrimitiveApplication(nOT, new Variable(from))));

                //ass = new PrimitiveApplication(PrimitiveApplication.EQUAL,
                //            new Variable(to),
                //            new PrimitiveApplication(
                //                PrimitiveApplication.AND, new PrimitiveApplication(nOT, new Variable(from))));
            }
            return(ass);
        }
Beispiel #2
0
    public Genome copy()
    {
        /*
         * int nrInput = this.nodes[0].Count;
         * int nrOutput = this.nodes[this.nrLayers - 1].Count;
         * int[] nrHiddenNodes = new int[this.nrHiddenLayers];
         */

        int[] layers = new int[nrLayers];

        for (int i = 0; i < this.nodes.Count; i++)
        {
            layers[i] = this.nodes[i].Count;
        }

        /*
         * for (int i = 0; i < this.nrHiddenLayers; i++)
         * {
         *      nrHiddenNodes[i] = this.nodes[i + 1].Count;
         * }
         */

        //Genome copy = new Genome(nrInput, nrHiddenNodes, nrOutput);
        Genome copy = new Genome(layers);

        // copy all nodes
        for (int i = 0; i < this.nrLayers; i++)
        {
            for (int j = 0; j < this.nodes[i].Count; j++)
            {
                GeneNode nodeCopy     = copy.nodes[i][j];
                GeneNode nodeOriginal = this.nodes[i][j];

                nodeCopy.bias = nodeOriginal.bias;
                nodeCopy.id   = nodeOriginal.id;

                // value should always be zero. it is only used for calculating the feed through it
                //nodeCopy.value = nodeOriginal.value;
            }
        }

        // copy all links
        for (int i = 0; i < this.nrLayers - 1; i++)
        {
            for (int j = 0; j < this.links[i].Count; j++)
            {
                GeneLink linkCopy     = copy.links[i][j];
                GeneLink linkOriginal = this.links[i][j];

                linkCopy.in_layer  = linkOriginal.in_layer;
                linkCopy.in_place  = linkOriginal.in_place;
                linkCopy.out_layer = linkOriginal.out_layer;
                linkCopy.out_place = linkOriginal.out_place;

                linkCopy.weight = linkOriginal.weight;
            }
        }

        return(copy);
    }
Beispiel #3
0
    private void feedForwardFrom(int fromLayer)
    {
        for (int i = 0; i < this.links[fromLayer].Count; i++)
        {
            GeneLink link = this.links[fromLayer][i];

            int in_layer = link.in_layer;
            int in_place = link.in_place;

            int out_layer = link.out_layer;
            int out_place = link.out_place;

            GeneNode inNode  = this.nodes[in_layer][in_place];
            GeneNode outNode = this.nodes[out_layer][out_place];

            outNode.value += (inNode.value * link.weight);
            //inNode.value = 0; // So it doesnt retain value from the last iteration; cause of the activationFunc(0) = 0.5 -> activationFunc(0.5) = 0.62.. -> activationFunc(0.62..)
        }

        // run though all nodes and reset value to 0
        foreach (GeneNode node in this.nodes[fromLayer])
        {
            node.value = 0;
        }
    }
Beispiel #4
0
    /*
     * private ArrayList<GeneNode> getNodesOfType(int type){
     *  ArrayList<GeneNode> nodesOfType = new ArrayList<GeneNode>();
     *  for (GeneNode node : this.nodes){
     *      if (node.type == type){
     *          nodesOfType.add(node);
     *      }
     *  }
     *
     *  return nodesOfType;
     * }
     */

    public string toString()
    {
        string str = "";

        for (int i = 0; i < nrLayers; i++)
        {
            for (int j = 0; j < this.nodes[i].Count; j++)
            {
                GeneNode node = this.nodes[i][j];
                str += "(" + node.value + " : " + node.bias + ") ";
            }

            str += "\n";

            if (i < nrLayers - 1)
            {
                for (int j = 0; j < this.links[i].Count; j++)
                {
                    GeneLink link = this.links[i][j];
                    str += "(" + link.in_layer + "," + link.in_place + ")=" + link.weight + "=(" + link.out_layer + "," + link.out_place + ")  ";
                }
            }

            str += "\n";
        }
        return(str);
    }
Beispiel #5
0
        private GeneLink CreateLinkFromLine(string s)
        {
            var data = s.Split('\t');

            var result = new GeneLink();

            result.From       = data[0];
            result.To         = data[1];
            result.IsPositive = data[2].StartsWith("positive");

            if (data.Length > 3)
            {
                result.IsOptional = data[3].StartsWith("optional");
            }

            return(result);
        }