Ejemplo n.º 1
0
        public static INode AddNode(this IGraph myIGraph, Int64 myInt64Id)
        {
            if (myIGraph == null)
                throw new ArgumentNullException("myIGraph must not be null!");

            return myIGraph.AddNode(myInt64Id.ToString());
        }
Ejemplo n.º 2
0
        public static INode AddNode(this IGraph myIGraph)
        {
            if (myIGraph == null)
                throw new ArgumentNullException("myIGraph must not be null!");

            return myIGraph.AddNode(new Node());
        }
Ejemplo n.º 3
0
        public static INode AddNode(this IGraph myIGraph, String myId)
        {
            if (myIGraph == null)
                throw new ArgumentNullException("myIGraph must not be null!");

            if (myId.IsNullOrEmpty())
                throw new ArgumentNullException("myId must not be null or empty!");

            return myIGraph.AddNode(new Node(myId));
        }
    /// <summary>
    /// Clones the <see cref="XmlDocComment"/> comment from the interface class to the contract class.
    /// </summary>
    /// <param name="contractClass">The contract class.</param>
    /// <param name="interfaceMember">The interface member.</param>
    public static void CloneDocComment(this Class contractClass, Member interfaceMember)
    {
      if (contractClass == null)
      {
        throw new ArgumentNullException("contractClass", "contractClass is null.");
      }

      if (interfaceMember == null)
      {
        throw new ArgumentNullException("interfaceMember", "interfaceMember is null.");
      }

      Contract.EndContractBlock();
      if (interfaceMember.DocComment != null)
      {
        contractClass.AddNode(interfaceMember.DocComment.Clone() as XmlDocComment);
      }
    }
Ejemplo n.º 5
0
        /// <summary>
        /// Links a Network from nodes and edges.
        /// </summary>
        /// <param name="nodes">An array of nodes in the network</param>
        /// <param name="edges">An array of edges between the nodes in the network.</param>
        public static Network LinkNodes(this Network network, IEnumerable<Neuron> nodes, IEnumerable<Edge> edges)
        {
            int inputLayerId = nodes.Min(m => m.LayerId);
            int outputLayerId = nodes.Max(m => m.LayerId);

            network.In = nodes.Where(w => w.LayerId == inputLayerId).ToArray();

            foreach (var node in network.In)
                network.AddNode(node);

            int hiddenLayer = inputLayerId + 1;
            // relink nodes
            Neuron[] last = null;
            for (int layerIdx = hiddenLayer; layerIdx < outputLayerId; layerIdx++)
            {
                Neuron[] layer = nodes.Where(w => w.LayerId == layerIdx).ToArray();

                foreach (var node in layer)
                    network.AddNode(node);

                if (layerIdx > hiddenLayer)
                {
                    // create hidden to hidden (full)
                    for (int i = 0; i < last.Length; i++)
                        for (int x = 1; x < layer.Length; x++)
                            network.AddEdge(Edge.Create(last[i], layer[x],
                                weight: edges.First(f => f.ParentId == last[i].Id && f.ChildId == layer[x].Id).Weight));
                }
                else if (layerIdx == hiddenLayer)
                {
                    // create input to hidden (full)
                    for (int i = 0; i < network.In.Length; i++)
                        for (int j = 1; j < layer.Length; j++)
                            network.AddEdge(Edge.Create(network.In[i], layer[j],
                                weight: edges.First(f => f.ParentId == network.In[i].Id && f.ChildId == layer[j].Id).Weight));
                }

                last = layer;
            }

            network.Out = nodes.Where(w => w.LayerId == outputLayerId).ToArray();

            foreach (var node in network.Out)
                network.AddNode(node);

            for (int i = 0; i < network.Out.Length; i++)
                for (int j = 0; j < last.Length; j++)
                    network.AddEdge(Edge.Create(last[j], network.Out[i],
                        weight: edges.First(f => f.ParentId == last[j].Id && f.ChildId == network.Out[i].Id).Weight));

            return network;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new fully connected deep neural network based on the supplied size and depth parameters.
        /// </summary>
        /// <param name="inputLayer">Neurons in the input layer.</param>
        /// <param name="outputLayer">Neurons in the output layer.</param>
        /// <param name="activationFunction">Activation function for the hidden and output layers.</param>
        /// <param name="outputFunction">(Optional) Output function of the the Nodes in the output layer (overrides the Activation function).</param>
        /// <param name="fnNodeInitializer">(Optional) Function to call for initializing new Nodes - supplying parameters for the layer and node index.</param>
        /// <param name="fnWeightInitializer">(Optional) Function to call for initializing the weights of each connection (including bias nodes).
        /// <para>Where int1 = Source layer (0 is input layer), int2 = Source Node, int3 = Target node in the next layer.</para></param>
        /// <param name="epsilon">Weight initialization parameter for random weight selection.  Weight will be in the range of: -epsilon to +epsilon.</param>
        /// <param name="hiddenLayers">An array of hidden neuron dimensions, where each element is the size of each layer (excluding bias nodes).</param>
        /// <returns>Returns an untrained neural network model.</returns>
        public static Network Create(this Network network, int inputLayer, int outputLayer, IFunction activationFunction, IFunction outputFunction = null, Func<int, int, Neuron> fnNodeInitializer = null, 
            Func<int, int, int, double> fnWeightInitializer = null, double epsilon = double.NaN, params int[] hiddenLayers)
        {
            IFunction ident = new Ident();

            if (hiddenLayers == null || hiddenLayers.Length == 0)
                hiddenLayers = new int[] { (int) System.Math.Ceiling((inputLayer + outputLayer + 1) * (2.0 / 3.0)) };

            List<double> layers = new List<double>();
            layers.Add(inputLayer);
            foreach (int l in hiddenLayers)
                layers.Add(l + 1);
            layers.Add(outputLayer);

            if (fnNodeInitializer == null)
                fnNodeInitializer = new Func<int, int, Neuron>((i, j) => new Neuron());

            if (fnWeightInitializer == null)
                fnWeightInitializer = new Func<int, int, int, double>((l, i, j) => {
                    double inputs = (l > 0 ? layers[l - 1] : 0);
                    double outputs = (l < layers.Count - 1 ? layers[l + 1] : 0);
                    double eps = (double.IsNaN(epsilon) ? Edge.GetEpsilon(activationFunction.Minimum, activationFunction.Maximum, inputs, outputs) : epsilon);
                    return Edge.GetWeight(eps);
                });

            // creating input nodes
            network.In = new Neuron[inputLayer + 1];
            network.In[0] = network.AddNode(new Neuron(true) { Label = "B0", ActivationFunction = ident, NodeId = 0, LayerId = 0 });

            for (int i = 1; i < inputLayer + 1; i++)
            {
                network.In[i] = fnNodeInitializer(0, i);
                network.In[i].Label = (network.In[i].Label ?? string.Format("I{0}", i));
                network.In[i].ActivationFunction = (network.In[i].ActivationFunction ?? ident);
                network.In[i].LayerId = 0;
                network.In[i].NodeId = i;

                network.AddNode(network.In[i]);
            }

            Neuron[] last = null;
            for (int layerIdx = 0; layerIdx < hiddenLayers.Length; layerIdx++)
            {
                // creating hidden nodes
                Neuron[] layer = new Neuron[hiddenLayers[layerIdx] + 1];
                layer[0] = network.AddNode(new Neuron(true) { Label = $"B{layerIdx + 1}", ActivationFunction = ident, LayerId = layerIdx + 1, NodeId = 0 });
                for (int i = 1; i < layer.Length; i++)
                {
                    layer[i] = fnNodeInitializer(layerIdx + 1, i);
                    layer[i].Label = (layer[i].Label ?? String.Format("H{0}.{1}", layerIdx + 1, i));
                    layer[i].ActivationFunction = (layer[i].ActivationFunction ?? activationFunction);
                    layer[i].OutputFunction = layer[i].OutputFunction;
                    layer[i].LayerId = layerIdx + 1;
                    layer[i].NodeId = i;

                    network.AddNode(layer[i]);
                }

                if (layerIdx > 0 && layerIdx < hiddenLayers.Length)
                {
                    // create hidden to hidden (full)
                    for (int i = 0; i < last.Length; i++)
                        for (int x = 1; x < layer.Length; x++)
                            network.AddEdge(Edge.Create(last[i], layer[x], weight: fnWeightInitializer(layerIdx, i, x), epsilon: epsilon));
                }
                else if (layerIdx == 0)
                {
                    // create input to hidden (full)
                    for (int i = 0; i < network.In.Length; i++)
                        for (int j = 1; j < layer.Length; j++)
                            network.AddEdge(Edge.Create(network.In[i], layer[j], weight: fnWeightInitializer(layerIdx, i, j), epsilon: epsilon));
                }

                last = layer;
            }

            // creating output nodes
            network.Out = new Neuron[outputLayer];
            for (int i = 0; i < outputLayer; i++)
            {
                network.Out[i] = fnNodeInitializer(hiddenLayers.Length + 1, i);
                network.Out[i].Label = (network.Out[i].Label ?? String.Format("O{0}", i));
                network.Out[i].ActivationFunction = (network.Out[i].ActivationFunction ?? activationFunction);
                network.Out[i].OutputFunction = (network.Out[i].OutputFunction ?? outputFunction);
                network.Out[i].LayerId = hiddenLayers.Length + 1;
                network.Out[i].NodeId = i;

                network.AddNode(network.Out[i]);
            }

            // link from (last) hidden to output (full)
            for (int i = 0; i < network.Out.Length; i++)
                for (int j = 0; j < last.Length; j++)
                    network.AddEdge(Edge.Create(last[j], network.Out[i], weight: fnWeightInitializer(hiddenLayers.Length, j, i), epsilon: epsilon));

            return network;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Add a node to the current graph.
 /// </summary>
 /// <param name="writer">The writer to which the node is added.</param>
 /// <param name="identifier">The identifier of the current node.</param>
 /// <param name="dotAttributes">A <see cref="T:IEnumerable`1"/> of optional attributes to be added to the
 /// node that will be added.</param>
 /// <remarks>
 /// <para>The identifier must be effective for the operation to take place.</para>
 /// <para>If the given list of attributes is not effective, no attributes are added to the node.</para>
 /// <para>If the given <paramref name="writer"/> is not effective, nothing happens.</para>
 /// </remarks>
 public static void AddNode(this IDotTextWriter writer, string identifier, params INodeDotAttribute[] dotAttributes)
 {
     if (writer != null) {
         writer.AddNode (identifier, (IEnumerable<INodeDotAttribute>)dotAttributes);
     }
 }
Ejemplo n.º 8
0
 public static void AddComment(this Node parent, HtmlToken token)
 {
     parent.AddNode(new Comment(parent.Owner, token.Data));
 }
Ejemplo n.º 9
0
 public static TreeNode AddNode(this TreeNode self, string text)
 {
     TreeNode node = new TreeNode(text);
     self.AddNode(node);
     return node;
 }
    /// <summary>
    /// Adds the interface namespace references to the container referred to by
    /// <paramref name="cursor"/>.
    /// </summary>
    /// <param name="cursor">The cursor.</param>
    /// <param name="namespaceNodes">The namespace nodes.</param>
    /// <returns>A reference to the object on which the method was invoked.</returns>
    public static LanguageElement AddNamespaceReferences(
      this LanguageElement cursor,
      IEnumerable<NamespaceReference> namespaceNodes)
    {
      if (cursor == null)
      {
        throw new ArgumentNullException("cursor", "cursor is null.");
      }

      if (namespaceNodes == null)
      {
        throw new ArgumentNullException("namespaceNodes", "namespaceNodes is null.");
      }

      Contract.EndContractBlock();

      foreach (NamespaceReference ns in namespaceNodes)
      {
        Contract.Assert(cursor != null);
        if (ns == null)
        {
          continue;
        }

        if (ns.Comments != null)
        {
          ns.Comments.Clear();
        }

        cursor.AddNode(ns);
      }

      return cursor;
    }