Exemple #1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Structure <GH_Point>  allPoints       = new GH_Structure <GH_Point>();
            List <GH_Point>          winnerPoints    = new List <GH_Point>();
            GH_Structure <GH_Number> allValuesAsTree = new GH_Structure <GH_Number>();
            GH_Path  winnerPath = new GH_Path();
            CrowNetP Net        = new CrowNetP();



            if (!DA.GetData <CrowNetP>(0, ref Net))
            {
                return;
            }

            if (Net.netType == "som")
            {
                for (int i = 0; i < Net.layerWidth; i++)
                {
                    for (int j = 0; j < Net.layerHeight; j++)
                    {
                        allPoints.Append(new GH_Point(new Point3d(Net.columnX[i][j], Net.columnY[i][j], Net.columnZ[i][j])), new GH_Path(i));
                        if (Net.isWinner[i, j])
                        {
                            winnerPoints.Add(new GH_Point(new Point3d(Net.columnX[i][j], Net.columnY[i][j], Net.columnZ[i][j])));
                            winnerPath = new GH_Path(i, j);
                        }
                    }
                }
            }

            DA.SetDataList(0, allPoints);
            DA.SetDataList(1, winnerPoints);
            DA.SetDataTree(2, Net.allTrainingValues);
            DA.SetData(3, winnerPath);
        }
Exemple #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <GH_Line> rowLines    = new List <GH_Line>();
            List <GH_Line> columnLines = new List <GH_Line>();
            List <GH_Line> hexLines    = new List <GH_Line>();
            CrowNetP       Net         = new CrowNetP();


            if (!DA.GetData <CrowNetP>(0, ref Net))
            {
                return;
            }

            if (Net.netType == "som")
            {
                for (int i = 0; i < Net.layerHeight; i++)
                {
                    Point3d iPointA;
                    Point3d iPointB;

                    for (int j = 0; j < Net.layerWidth; j++)
                    {
                        if (j != Net.layerWidth - 1)
                        {
                            iPointA = new Point3d(Net.rowX[i][j], Net.rowY[i][j], Net.rowZ[i][j]);
                            iPointB = new Point3d(Net.rowX[i][j + 1], Net.rowY[i][j + 1], Net.rowZ[i][j + 1]);
                            rowLines.Add(new GH_Line(new Line(iPointA, iPointB)));
                        }
                        else
                        {
                            if (Net.isCircularRows)
                            {
                                iPointA = new Point3d(Net.rowX[i][j], Net.rowY[i][j], Net.rowZ[i][j]);
                                iPointB = new Point3d(Net.rowX[i][0], Net.rowY[i][0], Net.rowZ[i][0]);
                                rowLines.Add(new GH_Line(new Line(iPointA, iPointB)));
                            }
                        }
                    }
                }



                for (int i = 0; i < Net.layerWidth; i++)
                {
                    Point3d iPointA;
                    Point3d iPointB;

                    for (int j = 0; j < Net.layerHeight; j++)
                    {
                        if (j != Net.layerHeight - 1)
                        {
                            iPointA = new Point3d(Net.columnX[i][j], Net.columnY[i][j], Net.columnZ[i][j]);
                            iPointB = new Point3d(Net.columnX[i][j + 1], Net.columnY[i][j + 1], Net.columnZ[i][j + 1]);
                            columnLines.Add(new GH_Line(new Line(iPointA, iPointB)));
                        }
                        else
                        {
                            if (Net.isCircularColumns)
                            {
                                iPointA = new Point3d(Net.columnX[i][j], Net.columnY[i][j], Net.columnZ[i][j]);
                                iPointB = new Point3d(Net.columnX[i][0], Net.columnY[i][0], Net.columnZ[i][0]);
                                columnLines.Add(new GH_Line(new Line(iPointA, iPointB)));
                            }
                        }
                    }
                }

                if (Net.latticeTopology)
                {
                    for (int i = 0; i < Net.layerWidth - 1; i++)
                    {
                        Point3d iPointA;
                        Point3d iPointB;
                        for (int j = 0; j < Net.layerHeight; j++)
                        {
                            if (j != Net.layerHeight - 1)
                            {
                                iPointA = new Point3d(Net.hexagonalX[i][j], Net.hexagonalY[i][j], Net.hexagonalZ[i][j]);
                                iPointB = new Point3d(Net.hexagonalX[i][j + 1], Net.hexagonalY[i][j + 1], Net.hexagonalZ[i][j + 1]);
                                columnLines.Add(new GH_Line(new Line(iPointA, iPointB)));
                            }
                        }
                        if (Net.isCircularColumns)
                        {
                            /*iPointA = new Point3d(hexagonalX[layerWidth-1][layerHeight], hexagonalY[layerWidth-1][layerHeight], 0);
                             * iPointB = new Point3d(hexagonalX[0][layerHeight], hexagonalY[0][layerHeight], 0);
                             * columnLines.Add(new GH_Line(new Line(iPointA, iPointB)));*/
                        }
                    }
                }
            }
            if (Net.netType == "tsp")
            {
                bool circle = Net.isCircularRows;

                for (int i = 0; i < Net.xVal.Length - 2; i++)
                {
                    Point3d iPointA = new Point3d(Net.xVal[i], Net.yVal[i], Net.zVal[i]);
                    Point3d iPointB = new Point3d(Net.xVal[i + 1], Net.yVal[i + 1], Net.zVal[i + 1]);
                    rowLines.Add(new GH_Line(new Line(iPointA, iPointB)));
                    if (i == Net.xVal.Length - 3 && circle)
                    {
                        Point3d A = new Point3d(Net.xVal[i], Net.yVal[i], Net.zVal[i]);
                        Point3d B = new Point3d(Net.xVal[0], Net.yVal[0], Net.zVal[0]);
                        rowLines.Add(new GH_Line(new Line(A, B)));
                    }
                }
            }

            DA.SetDataList(0, rowLines);
            DA.SetDataList(1, columnLines);
            DA.SetDataList(2, hexLines);
        }
Exemple #3
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Mesh  CMesh = new GH_Mesh();
            CrowNetP Net   = new CrowNetP();

            if (!DA.GetData <CrowNetP>(0, ref Net))
            {
                return;
            }

            int  H         = Net.layerHeight;
            int  W         = Net.layerWidth;
            Mesh RhinoMesh = new Mesh();


            for (int i = 0; i < W; i++)
            {
                for (int j = 0; j < H; j++)
                {
                    if (!Net.latticeTopology)
                    {
                        RhinoMesh.Vertices.Add(new Point3d(Net.columnX[i][j], Net.columnY[i][j], Net.columnZ[i][j]));
                    }
                    else
                    {
                        RhinoMesh.Vertices.Add(new Point3d(Net.hexagonalX[i][j], Net.hexagonalY[i][j], Net.hexagonalZ[i][j]));
                    }
                }
            }


            int pointCount = H * W;


            if (!Net.latticeTopology)   // quad mesh
            {
                #region quad mesh
                if (!Net.isCircularRows && !Net.isCircularColumns)         // no circular rows or columns
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - H - 1))
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                    }
                }

                if (!Net.isCircularRows && Net.isCircularColumns)         // circular columns but no circular rows
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - H - 1))
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                        else
                        {
                            if (!(i > pointCount - H - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i - (H - 1), i + 1, i + H);
                            }
                        }
                    }
                }

                if (Net.isCircularRows && !Net.isCircularColumns)         // no circular columns but circular rows
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - H - 1))
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                        else
                        {
                            if (i > (pointCount - H - 1) && !(i == pointCount - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i + 1, (i + 1) - (H * (W - 1)), i - (H * (W - 1)));
                            }
                        }
                    }
                }


                if (Net.isCircularRows && Net.isCircularColumns)          // circular rows and circular columns
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - H - 1))  // if is not in upper or left side
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                        else
                        {
                            if (!(i > pointCount - H - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i - (H - 1), i + 1, i + H);
                            }                                                                                         //if is in upper corner
                            if (i > (pointCount - H - 1) && !(i == pointCount - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i + 1, (i + 1) - (H * (W - 1)), i - (H * (W - 1)));
                            }                                                                                                                                          // if is in left side
                            if (i == pointCount - 1)
                            {
                                RhinoMesh.Faces.AddFace(i, i - H + 1, 0, H - 1);
                            }
                        }
                    }
                }
                #endregion
            }

            else                        // tri mesh
            {
                #region tri mesh
                //int o = 1; //flip orientation with 0 and 1

                if (!Net.isCircularRows && !Net.isCircularColumns)         // no circular rows or columns
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - 2 * H - 1))
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                    }
                }

                if (!Net.isCircularRows && Net.isCircularColumns)         // circular columns but no circular rows
                {
                    for (int w = 0; w < W; w++)
                    {
                        for (int h = 0; h < H; h++)
                        {
                            int i = w * H + h;
                            if (i < pointCount - 2 * H - 1)
                            {
                                if (h % 2 == 0)
                                {
                                    if (h != H - 1)
                                    {
                                        RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1);
                                        RhinoMesh.Faces.AddFace(i, i + H + 1, i + H);
                                    }
                                    else
                                    {
                                        RhinoMesh.Faces.AddFace(i, i + 1, i + H);
                                        RhinoMesh.Faces.AddFace(i + 1, i + H + 1, i + H);
                                    }
                                }
                                else
                                {
                                    RhinoMesh.Faces.AddFace(i, i + 1, i + H);
                                    RhinoMesh.Faces.AddFace(i + 1, i + H + 1, i + H);
                                }
                            }
                        }
                    }
                }

                if (Net.isCircularRows && !Net.isCircularColumns)         // no circular columns but circular rows
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - H - 1))
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                        else
                        {
                            if (i > (pointCount - H - 1) && !(i == pointCount - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i + 1, (i + 1) - (H * (W - 1)), i - (H * (W - 1)));
                            }
                        }
                    }
                }


                if (Net.isCircularRows && Net.isCircularColumns)          // circular rows and circular columns
                {
                    for (int i = 0; i < pointCount; i++)
                    {
                        if (i % H != H - 1 && !(i > pointCount - H - 1))  // if is not in upper or left side
                        {
                            RhinoMesh.Faces.AddFace(i, i + 1, i + H + 1, i + H);
                        }
                        else
                        {
                            if (!(i > pointCount - H - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i - (H - 1), i + 1, i + H);
                            }                                                                                         //if is in upper corner
                            if (i > (pointCount - H - 1) && !(i == pointCount - 1))
                            {
                                RhinoMesh.Faces.AddFace(i, i + 1, (i + 1) - (H * (W - 1)), i - (H * (W - 1)));
                            }                                                                                                                                          // if is in left side
                            if (i == pointCount - 1)
                            {
                                RhinoMesh.Faces.AddFace(i, i - H + 1, 0, H - 1);
                            }
                        }
                    }
                }
                #endregion
            }



            CMesh.CastFrom(RhinoMesh);
            DA.SetData(0, CMesh);
        }
Exemple #4
0
        void Solve()
        {
            CrowNetP NetP = new CrowNetP();

            if (netUP.netType == "som")
            {
                #region self organizing maps

                #region prepare and assign
                trainingSet.Clear();
                int trainVectorDimension = 3;
                if (trainDataArePoints)
                {
                    for (int i = 0; i < pointsList.Count; i++)
                    {
                        trainingSet.Add(new TrainingSample(new double[] { pointsList[i].Value.X, pointsList[i].Value.Y, pointsList[i].Value.Z }));
                    }
                }
                else
                {
                    trainVectorDimension = trainingVectorTree.Branches[0].Count;
                    trainingSet          = new TrainingSet(trainVectorDimension);
                    for (int i = 0; i < trainingVectorTree.Branches.Count; i++)
                    {
                        double[] values = new double[trainVectorDimension];

                        for (int j = 0; j < trainVectorDimension; j++)
                        {
                            values[j] = trainingVectorTree.Branches[i][j].Value;
                        }

                        trainingSet.Add(new TrainingSample(values));
                    }
                }


                ///  process
                ///  start learning

                int learningRadius = Math.Max(layerWidth, layerHeight) / 2;

                INeighborhoodFunction neighborhoodFunction = new GaussianFunction(learningRadius, netUP.neighborDistance) as INeighborhoodFunction;
                if (neighborhood)
                {
                    neighborhoodFunction = new MexicanHatFunction(learningRadius) as INeighborhoodFunction;
                }

                LatticeTopology topology = LatticeTopology.Rectangular;
                if (latticeTopology)
                {
                    topology = LatticeTopology.Hexagonal;
                }

                KohonenLayer     inputLayer  = new KohonenLayer(trainVectorDimension);
                KohonenLayer     outputLayer = new KohonenLayer(new Size(layerWidth, layerHeight), neighborhoodFunction, topology);
                KohonenConnector connector   = new KohonenConnector(inputLayer, outputLayer);
                connector.Initializer = randomizer;

                outputLayer.SetLearningRate(learningRate, 0.05d);
                outputLayer.IsRowCircular    = isCircularRows;
                outputLayer.IsColumnCircular = isCircularColumns;
                network = new KohonenNetwork(inputLayer, outputLayer);
                network.useRandomTrainingOrder = opt.UseRandomTraining;
                #endregion

                #region delegates
                network.BeginEpochEvent += new TrainingEpochEventHandler(
                    delegate(object senderNetwork, TrainingEpochEventArgs args)
                {
                    #region TrainingCycle
                    if (network == null || !GO)
                    {
                        return;
                    }


                    int iPrev     = layerWidth - 1;
                    allValuesTree = new GH_Structure <GH_Number>();
                    for (int i = 0; i < layerWidth; i++)
                    {
                        for (int j = 0; j < layerHeight; j++)
                        {
                            IList <ISynapse> synapses = (network.OutputLayer as KohonenLayer)[i, j].SourceSynapses;
                            double x = synapses[0].Weight;
                            double y = synapses[1].Weight;
                            double z = synapses[2].Weight;

                            for (int k = 0; k < trainVectorDimension; k++)
                            {
                                allValuesTree.Append(new GH_Number(synapses[k].Weight), new GH_Path(i, j));
                            }

                            rowX[j][i]    = x;
                            rowY[j][i]    = y;
                            rowZ[j][i]    = z;
                            columnX[i][j] = x;
                            columnY[i][j] = y;
                            columnZ[i][j] = z;

                            if (j % 2 == 1)
                            {
                                hexagonalX[i][j] = x;
                                hexagonalY[i][j] = y;
                                hexagonalZ[i][j] = z;
                            }
                            else
                            {
                                hexagonalX[iPrev][j] = x;
                                hexagonalY[iPrev][j] = y;
                                hexagonalZ[iPrev][j] = z;
                            }
                        }
                        iPrev = i;
                    }

                    if (isCircularRows)
                    {
                        for (int i = 0; i < layerHeight; i++)
                        {
                            rowX[i][layerWidth] = rowX[i][0];
                            rowY[i][layerWidth] = rowY[i][0];
                            rowZ[i][layerWidth] = rowZ[i][0];
                        }
                    }

                    if (isCircularColumns)
                    {
                        for (int i = 0; i < layerWidth; i++)
                        {
                            columnX[i][layerHeight]    = columnX[i][0];
                            columnY[i][layerHeight]    = columnY[i][0];
                            columnZ[i][layerHeight]    = columnZ[i][0];
                            hexagonalX[i][layerHeight] = hexagonalX[i][0];
                            hexagonalY[i][layerHeight] = hexagonalY[i][0];
                            hexagonalZ[i][layerHeight] = hexagonalZ[i][0];
                        }
                    }

                    Array.Clear(isWinner, 0, layerHeight * layerWidth);

                    #endregion
                    NetP = new CrowNetP("som", layerWidth, layerHeight, isCircularRows, isCircularColumns, latticeTopology, neighborhood, isWinner, rowX, rowY, rowZ, columnX, columnY, columnZ, hexagonalX, hexagonalY, hexagonalZ, allValuesTree);
                    counter++;
                });

                network.EndSampleEvent += new TrainingSampleEventHandler(
                    delegate(object senderNetwork, TrainingSampleEventArgs args)
                {
                    isWinner[network.Winner.Coordinate.X, network.Winner.Coordinate.Y] = true;
                });
                #endregion

                #endregion
            }

            network.Learn(trainingSet, cycles);
        }
Exemple #5
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //get stuff
            trainDataArePoints = true;

            if (!DA.GetData(0, ref cycles))
            {
                return;
            }
            if (!DA.GetData(1, ref netUP))
            {
                return;
            }
            DA.GetData(2, ref opt);
            if (!DA.GetDataList(3, pointsList))
            {
                trainDataArePoints = false;
                if (!DA.GetDataTree(4, out trainingVectorTree))
                {
                    return;
                }
            }
            if (!DA.GetData(5, ref GO))
            {
                return;
            }



            layerHeight       = netUP.layerHeight;                /// read unprocessed CrowNet
            layerWidth        = netUP.layerWidth;
            isCircularRows    = netUP.isCircularRows;
            isCircularColumns = netUP.isCircularColumns;
            latticeTopology   = netUP.latticeTopology;
            neighborhood      = netUP.neighborhood;
            learningRate      = netUP.learningRate;

            if (counter == 0)
            {
                #region initialize processing params

                isWinner   = new bool[layerWidth, layerHeight];
                rowX       = new double[layerHeight][];
                rowY       = new double[layerHeight][];
                rowZ       = new double[layerHeight][];
                columnX    = new double[layerWidth][];
                columnY    = new double[layerWidth][];
                columnZ    = new double[layerWidth][];
                hexagonalX = new double[layerWidth][];
                hexagonalY = new double[layerWidth][];
                hexagonalZ = new double[layerWidth][];

                circle = netUP.isCircularRows;
                xVal   = new double[pointsList.Count * 2 + 1];
                yVal   = new double[pointsList.Count * 2 + 1];
                zVal   = new double[pointsList.Count * 2 + 1];
                #endregion


                #region prepare for NetP
                int lengthOfRow    = layerWidth;
                int lengthOfColumn = layerHeight;

                if (isCircularRows)
                {
                    lengthOfRow++;
                }
                if (isCircularColumns)
                {
                    lengthOfColumn++;
                }

                for (int i = 0; i < layerWidth; i++)
                {
                    columnX[i]    = new double[lengthOfColumn];
                    columnY[i]    = new double[lengthOfColumn];
                    columnZ[i]    = new double[lengthOfColumn];
                    hexagonalX[i] = new double[lengthOfColumn];
                    hexagonalY[i] = new double[lengthOfColumn];
                    hexagonalZ[i] = new double[lengthOfColumn];
                }

                for (int i = 0; i < layerHeight; i++)
                {
                    rowX[i] = new double[lengthOfRow];
                    rowY[i] = new double[lengthOfRow];
                    rowZ[i] = new double[lengthOfRow];
                }
                # endregion
            }

            if (GO)
            {
                if (counter == 0)
                {
                    Task.Factory.StartNew(() => Solve());
                }
                DA.SetData(0, counter);
                netP = new CrowNetP("som", layerWidth, layerHeight, isCircularRows, isCircularColumns, latticeTopology, neighborhood, isWinner, rowX, rowY, rowZ, columnX, columnY, columnZ, hexagonalX, hexagonalY, hexagonalZ, allValuesTree);
                ExpireSolution(true);
            }
            else
            {
                counter = 0;
            }


            DA.SetData(1, netP);



            pointsList = new List <GH_Point>();
        }
Exemple #6
0
        public CrowNetP Duplicate()
        {
            CrowNetP dup = new CrowNetP(netType, layerWidth, layerHeight, isCircularRows, isCircularColumns, latticeTopology, neighborhood, isWinner, rowX, rowY, rowZ, columnX, columnY, columnZ, hexagonalX, hexagonalY, hexagonalZ, allTrainingValues);

            return(dup);
        }