public void Write(Dictionary <string, List <List <double> > > data, string name)
        {
            _DataBuilder = new DataBuilder();
            _DataStruct  = _DataBuilder.NewStructureArray(new[] { name }, 1);

            foreach (string var in data.Keys)
            {
                List <List <double> > values = data[var];

                int n = values.Count;
                int m = values.Max(c => c.Count);

                _DataStruct[name, 0] = MatlabUtilities.AddFieldToStructureArray(_DataBuilder, _DataStruct[name, 0], var, 1);
                (_DataStruct[name, 0] as IStructureArray)[var, 0] = _DataBuilder.NewArray <double>(n, m);
                IArrayOf <double> array = (IArrayOf <double>)(_DataStruct[name, 0] as IStructureArray)[var, 0];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (values[i] != null && j < values[i].Count)
                        {
                            array[i, j] = values[i][j];
                        }
                    }
                }
            }
        }
 private void CompareNumericalArrays <T>(IArrayOf <T> expected, IArrayOf <T> actual)
 {
     Assert.That(actual, Is.Not.Null);
     Assert.That(expected.Dimensions, Is.EqualTo(actual.Dimensions));
     Assert.That(expected.Data, Is.EqualTo(actual.Data));
 }
Beispiel #3
0
        public void createNetwork(bool matlabFile)
        {
            if (_nodesInputLayer <= 0 || _nodesOutputLayer <= 0)
            {
                throw new Exception("Invalid amount of input or output layer nodes");
            }

            //For creating txt
            FileStream   outFile       = null;
            StreamWriter fileWriterTxt = null;
            //For creating mat-file
            MatFileWriter  fileWriterMatlab = null;
            DataBuilder    dataBuilder      = null;
            IArrayOf <int> array            = null;

            //Total number of nodes
            int nodesTotal = _nodesInputLayer + _nodesOutputLayer;

            foreach (int nodes in _nodesHiddenLayers)
            {
                nodesTotal += nodes;
            }

            try{
                if (!matlabFile)
                {
                    outFile       = File.Create(Path.Combine(Directory.GetCurrentDirectory(), "network.nn"));
                    fileWriterTxt = new System.IO.StreamWriter(outFile);
                }
                else
                {
                    outFile          = new System.IO.FileStream("network.mat", System.IO.FileMode.Create);
                    fileWriterMatlab = new MatFileWriter(outFile);
                    dataBuilder      = new DataBuilder();
                    //Create the two dimensional array
                    array = dataBuilder.NewArray <int>(nodesTotal, nodesTotal);
                }

                int layerVertical   = 0;
                int layerHorizontal = 0;

                if (!matlabFile)
                {
                    fileWriterTxt.WriteLine("network = [");
                }

                for (int row = 0; row < nodesTotal; row++)
                {
                    layerVertical = currentLayer(row);

                    for (int column = 0; column < nodesTotal; column++)
                    {
                        layerHorizontal = currentLayer(column);

                        /*
                         * Sets node to one if when following condition is full filed
                         *
                         *    L0 | L1 | L2 | L3 |
                         * ----+----+----+----+----+
                         * L0 |    | X  |    |
                         * ----+----+----+----+----+
                         * L1 |    |    | X  |
                         * ----+----+----+----+----+
                         * L2 |    |    |    | X
                         * ----+----+----+----+----+
                         * L3 |    |    |    |
                         * ----+----+----+----+----+
                         */
                        if (layerVertical == (layerHorizontal - 1))
                        {
                            if (!matlabFile)
                            {
                                fileWriterTxt.Write(" 1");
                            }
                            else
                            {
                                array[row, column] = 1;
                            }
                        }
                        else
                        {
                            if (!matlabFile)
                            {
                                fileWriterTxt.Write(" 0");
                            }
                            else
                            {
                                array[row, column] = 0;
                            }
                        }
                    }
                    if (!matlabFile && row != nodesTotal)
                    {
                        fileWriterTxt.Write(";\n");
                    }
                }

                if (!matlabFile)
                {
                    fileWriterTxt.WriteLine("];");
                }
                else
                {
                    //Create the variable
                    IVariable network = dataBuilder.NewVariable("network", array);
                    //Create the matfile
                    IMatFile matFile = dataBuilder.NewFile(new[] { network });
                    //Write to the file
                    fileWriterMatlab.Write(matFile);
                }
            }catch (Exception ex) {
                Console.WriteLine(ex);
            } finally{
                if (!matlabFile)
                {
                    fileWriterTxt.Dispose();
                }
                outFile.Dispose();
            }
        }