Esempio n. 1
0
        private void BTN_Cyl5_Click(object sender, EventArgs e)
        {
            if (!myledbulb9.On)
            {
                if (!myledbulb4.On)
                {
                    MessageBox.Show("상하 실린더를 확인해주세요");
                }
                else if (!myledbulb6.On)
                {
                    MessageBox.Show("지그 실린더를 확인해주세요");
                }
                else
                {
                    if (!IsNMFCon)
                    {
                        IsNMFCon = nmfdio.Connect(DevNum, IP[0], IP[1], IP[2], 200);
                    }

                    short nRet = 0;

                    Button B     = (Button)sender;
                    int    index = Convert.ToInt32(B.Tag);

                    nRet = NMF.nmf_DOSetTogPin(DevNum, (short)index);
                }
            }
        }
Esempio n. 2
0
        public void FactorizeTestMethod1()
        {
            // arrange
            var reader = new InMemorySparseMatrixReader(
                new SparseVector <double> {
                { 1, 85.18 }, { 2, 11.52 }
            },
                new SparseVector <double> {
                { 1, 37.99 }, { 2, 74.95 }
            },
                new SparseVector <double> ());
            var nmf = new NMF(reader);

            // act
            var sw = Stopwatch.StartNew();

            using (var factorization = nmf.Factorize(2, 1000))
            {
                sw.Stop();

                // assert
                Print("W", factorization.W);
                Print("H", factorization.H);
                Console.WriteLine("Factorization Euclidean distance: " + nmf.GetEuclideanDistance(factorization));
                Console.WriteLine("Time: " + sw.ElapsedMilliseconds + " ms.");
            }
        }
Esempio n. 3
0
        public string RunNMFbasedOMF(int maxEpoch, double learnRate, double regularization, int factorCount,
                                     List <double> quantizer, int topN = 0)
        {
            if (!ReadyForNumerical)
            {
                GetReadyForNumerical();
            }
            StringBuilder log = new StringBuilder();

            log.AppendLine(Utils.PrintHeading("NMF based OMF"));

            // NMF Prediction
            // Get ratings from scorer, for both train and test
            // R_all contains indexes of all ratings both train and test
            DataMatrix R_all = new DataMatrix(R_unknown.UserCount, R_unknown.ItemCount);

            R_all.MergeNonOverlap(R_unknown);
            R_all.MergeNonOverlap(R_train.IndexesOfNonZeroElements());
            Utils.StartTimer();
            DataMatrix R_predictedByNMF = NMF.PredictRatings(R_train, R_all, maxEpoch,
                                                             learnRate, regularization, factorCount);

            log.AppendLine(Utils.StopTimer());

            // OMF Prediction
            log.AppendLine(Utils.PrintHeading("Ordinal Matrix Factorization with NMF as scorer"));
            Utils.StartTimer();
            Dictionary <Tuple <int, int>, List <double> > OMFDistributionByUserItem;
            DataMatrix R_predicted;

            log.AppendLine(OMF.PredictRatings(R_train.Matrix, R_unknown.Matrix, R_predictedByNMF.Matrix,
                                              quantizer, out R_predicted, out OMFDistributionByUserItem));
            log.AppendLine(Utils.StopTimer());

            // Numerical Evaluation
            log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted).ToString("0.0000")));
            log.AppendLine(Utils.PrintValue("MAE", MAE.Evaluate(R_test, R_predicted).ToString("0.0000")));

            // TopN Evaluation
            if (topN != 0)
            {
                var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN);
                for (int n = 1; n <= topN; n++)
                {
                    log.AppendLine(Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
                }
                for (int n = 1; n <= topN; n++)
                {
                    log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
                }
            }

            // Save OMFDistribution to file
            if (!File.Exists(GetDataFileName("RatingOMF_")))
            {
                Utils.IO <Dictionary <Tuple <int, int>, List <double> > > .SaveObject(OMFDistributionByUserItem, GetDataFileName("RatingOMF_"));
            }

            return(log.ToString());
        }
Esempio n. 4
0
        private void GroundMeas()
        {
            // 접지 검사
            short nRet = 0;

            NMFstate[1]++;
            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)1);

            Delay(500);

            string judge = "OK";

            double[] doub = new double[4];
            NITimer.Enabled = false;

            for (int i = 0; i < 4; i++)
            {
                DAQ1.Digital_Write(i, true);
                DAQ2.Digital_Write(i, true);
                DAQ3.Digital_Write(i, true);
                DAQ4.Digital_Write(i, true);
                doub[0] = DAQ1.Analog_Read();
                doub[1] = DAQ2.Analog_Read();
                doub[2] = DAQ3.Analog_Read();
                doub[3] = DAQ4.Analog_Read();
                //Txt_Test.Text = Convert.ToString(DAQ1.Analog_Read());
                for (int j = 0; j < 4; j++)
                {
                    if (i == j)
                    {
                        if (doub[j] < Convert.ToDouble(info[7]))
                        {
                            judge = "NG";
                        }
                    }
                    else
                    {
                        if (doub[j] > 0.2)
                        {
                            judge = "NG";
                        }
                    }
                }
                DAQ1.Digital_Write(i, false);
                DAQ2.Digital_Write(i, false);
                DAQ3.Digital_Write(i, false);
                DAQ4.Digital_Write(i, false);
            }
            Txt_Test.Text = judge;
            if (ChannelComboBox.Text != "")
            {
                NITimer.Enabled = true;
            }
            Delay(500);

            NMFstate[1]++;
            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)1);
        }
Esempio n. 5
0
 public void Disconnect(short nDevNo)
 {
     NMF.nmf_Disconnect(nDevNo);
     TdWatchDIO.Abort();
     TdWatchDIO.Join();
     while (TdWatchDIO.ThreadState != ThreadState.Stopped)
     {
     }
 }
Esempio n. 6
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (!IsNMFCon)
            {
                IsNMFCon = nmfdio.Connect(DevNum, IP[0], IP[1], IP[2], 200);
            }
            short nRet = 0;

            if (NMFstate[0] % 2 == 1)
            {
                NMFstate[0]++;
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)0);
            }

            if (NMFstate[1] % 2 == 1)
            {
                NMFstate[1]++;
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)1);
            }

            if (NMFstate[2] % 2 == 1)
            {
                NMFstate[2]++;
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)2);
            }

            if (!myledbulb4.On)
            {
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)3);
                Delay(1000);
            }

            if (!myledbulb6.On)
            {
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)4);
                Delay(1000);
            }

            if (!myledbulb8.On)
            {
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)5);
                Delay(2500);
            }

            if (!myledbulb10.On)
            {
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)6);
                Delay(1000);
            }

            if (!myledbulb12.On)
            {
                nRet = NMF.nmf_DOSetTogPin(DevNum, (short)7);
                Delay(1500);
            }
        }
Esempio n. 7
0
        private void BTN_DO1_Click(object sender, EventArgs e)
        {
            if (!IsNMFCon)
            {
                IsNMFCon = nmfdio.Connect(DevNum, IP[0], IP[1], IP[2], 200);
            }

            short nRet = 0;

            Button B     = (Button)sender;
            int    index = Convert.ToInt32(B.Tag);

            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)index);
        }
Esempio n. 8
0
        public bool Connect(short nDevNo, short nIP1, short nIP2, short nIP3, int num)
        {
            bool flag = false;

            TdWatchDIO = new Thread(new ThreadStart(DoWatch));

            short nRet;

            try
            {
                nRet = NMF.nmf_PingCheck(nDevNo, nIP1, nIP2, nIP3, 200);
                if (nRet != 0)
                {
                    return(flag);
                }

                nRet = NMF.nmf_Connect(nDevNo, nIP1, nIP2, nIP3);
                if (nRet == 0)
                {
                    m_bDevConnect = true;
                    switch (TdWatchDIO.ThreadState)
                    {
                    case ThreadState.Stopped:
                        TdWatchDIO = new Thread(new ThreadStart(DoWatch));
                        break;

                    case ThreadState.Unstarted:
                        break;

                    default:
                        TdWatchDIO.Abort();
                        // TdWatchSensor.Join();
                        while (TdWatchDIO.ThreadState != ThreadState.Stopped)
                        {
                        }
                        break;
                    }
                    TdWatchDIO.Start();
                    flag = true;
                }
            }
            catch (Exception ex)
            {
            }
            return(flag);
        }
Esempio n. 9
0
        /*private void IDRead()
         * {
         *  short nRet = 0;
         *
         *  NMFstate[0]++;
         *  nRet = NMF.nmf_DOSetTogPin(DevNum, (short)0);
         *
         *  Delay(1000);
         *  //Idread.ReadErrorCode();
         *  Idread.ReadID();
         *  Txt_Test.Text = Idread.GetID();
         *  //Txt_Test.Text = Idread.GetID();
         *
         *  NMFstate[0]++;
         *  nRet = NMF.nmf_DOSetTogPin(DevNum, (short)0);
         * }*/

        private async void VoutMeas()
        {
            short  nRet = 0;
            double sum  = 0;

            NMFstate[2]++;
            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)2);
            await Task.Delay(500);

            for (int i = 0; i < 100; i++)
            {
                sum += DAQ5.Analog_Read();
            }
            Txt_Test.Text = (sum / 100).ToString();
            Txt_Test.Text = DAQ5.Analog_Read().ToString();
            await Task.Delay(500);

            NMFstate[2]++;
            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)2);
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            const string matrixFileName = @"c:\temp\matrix.txt";
            const int    rows           = 10000;
            const int    columns        = 1000;
            const double density        = 0.01;

            const int featuresCount   = 10;
            const int iterationsCount = 30;

            Console.WriteLine("Generating matrix {0}x{1} with avg. {2} non-zero elements ({3}%)...", rows, columns, (long)((double)rows * columns * density), density * 100.0);
            using (var file = new FileStream(matrixFileName, FileMode.Create))
                using (var mmwriter = new MatrixMarketWriter <double>(file))
                {
                    var vectors = SparseVectorHelper.GenerateSparseVectors(rows, columns, density, () => SparseVectorHelper.RandomInInterval(0.01, 100, 2));
                    mmwriter.Write(vectors);
                }

            Console.WriteLine("Matrix factorization (features=" + featuresCount + ", iterations=" + iterationsCount + ")...");
            using (var file = new FileStream(matrixFileName, FileMode.Open))
                using (var mmreader = new MatrixMarketReader <double>(file))
                    using (var cachedReader = new CachedMatrixMarketReader <double>(mmreader))
                    {
                        var nmf = new NMF(cachedReader);

                        // act
                        var sw = Stopwatch.StartNew();
                        using (var factorization = nmf.Factorize(featuresCount, iterationsCount))
                        {
                            sw.Stop();
                            Console.WriteLine("Factorization time: " + sw.ElapsedMilliseconds + " ms.");

                            Console.WriteLine("Euclidean distance calculation...");
                            sw = Stopwatch.StartNew();
                            var euclideanDistance = nmf.GetEuclideanDistance(factorization);
                            sw.Stop();
                            Console.WriteLine("Euclidean distance calculation time: " + sw.ElapsedMilliseconds + " ms.");
                            Console.WriteLine("Factorization Euclidean distance: " + euclideanDistance);
                        }
                    }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            const string matrixFileName = @"c:\temp\matrix.txt";
            const int rows = 10000;
            const int columns = 1000;
            const double density = 0.01;

            const int featuresCount = 10;
            const int iterationsCount = 30;

            Console.WriteLine("Generating matrix {0}x{1} with avg. {2} non-zero elements ({3}%)...", rows, columns, (long)((double)rows * columns * density), density * 100.0);
            using (var file = new FileStream(matrixFileName, FileMode.Create))
            using (var mmwriter = new MatrixMarketWriter<double>(file))
            {
                var vectors = SparseVectorHelper.GenerateSparseVectors(rows, columns, density, () => SparseVectorHelper.RandomInInterval(0.01, 100, 2));
                mmwriter.Write(vectors);
            }

            Console.WriteLine("Matrix factorization (features=" + featuresCount + ", iterations=" + iterationsCount + ")...");
            using (var file = new FileStream(matrixFileName, FileMode.Open))
            using (var mmreader = new MatrixMarketReader<double>(file))
            using (var cachedReader = new CachedMatrixMarketReader<double>(mmreader))
            {
                var nmf = new NMF(cachedReader);

                // act
                var sw = Stopwatch.StartNew();
                using (var factorization = nmf.Factorize(featuresCount, iterationsCount))
                {
                    sw.Stop();                    
                    Console.WriteLine("Factorization time: " + sw.ElapsedMilliseconds + " ms.");

                    Console.WriteLine("Euclidean distance calculation...");
                    sw = Stopwatch.StartNew();
                    var euclideanDistance = nmf.GetEuclideanDistance(factorization);
                    sw.Stop();
                    Console.WriteLine("Euclidean distance calculation time: " + sw.ElapsedMilliseconds + " ms.");                   
                    Console.WriteLine("Factorization Euclidean distance: " + euclideanDistance);                    
                }
            }
        }
Esempio n. 12
0
        public void FactorizeTestMethod1()
        {
            // arrange
            var reader = new InMemorySparseMatrixReader(
                new SparseVector<double> { { 1, 85.18 }, { 2, 11.52 } },
                new SparseVector<double> { { 1, 37.99 }, { 2, 74.95 } },
                new SparseVector<double> ());
            var nmf = new NMF(reader);
            
            // act
            var sw = Stopwatch.StartNew();
            using (var factorization = nmf.Factorize(2, 1000))
            {
                sw.Stop();

                // assert
                Print("W", factorization.W);
                Print("H", factorization.H);
                Console.WriteLine("Factorization Euclidean distance: " + nmf.GetEuclideanDistance(factorization));
                Console.WriteLine("Time: " + sw.ElapsedMilliseconds + " ms.");
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Rating based Non-negative Matrix Factorization
        /// </summary>
        public string RunNMF(int maxEpoch, double learnRate, double regularization,
                             int factorCount, int topN = 0)
        {
            if (!ReadyForNumerical)
            {
                GetReadyForNumerical();
            }
            StringBuilder log = new StringBuilder();

            log.AppendLine(Utils.PrintHeading("NMF"));

            // Prediction
            Utils.StartTimer();
            DataMatrix R_predicted = NMF.PredictRatings(R_train, R_unknown, maxEpoch,
                                                        learnRate, regularization, factorCount);

            log.AppendLine(Utils.StopTimer());

            // Numerical Evaluation
            log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted).ToString("0.0000")));
            log.AppendLine(Utils.PrintValue("MAE", MAE.Evaluate(R_test, R_predicted).ToString("0.0000")));

            // TopN Evaluation
            if (topN != 0)
            {
                var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN);
                for (int n = 1; n <= topN; n++)
                {
                    log.AppendLine(Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
                }
                for (int n = 1; n <= topN; n++)
                {
                    log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")));
                }
            }

            return(log.ToString());
        }
Esempio n. 14
0
        private void NMFTimer_Tick(object sender, EventArgs e)
        {
            if (!IsNMFCon)
            {
                IsNMFCon = nmfdio.Connect(DevNum, IP[0], IP[1], IP[2], 200);
            }
            short nRet;

            short[] nPinStatus = new short[128];
            bool[]  state      = new bool[15];

            try
            {
                nRet = NMF.nmf_GetBrdAllStatus(DevNum, out nmfdio.tAllStatus);

                nRet = NMF.nmf_DIGet(DevNum, nPinStatus);
                if (nRet != 0)
                {
                    return;
                }
                for (int i = 0; i < 15; i++)
                {
                    //CheckBox chkBox = (Controls.Find("checkBox_In" + i.ToString(), true)[0] as CheckBox);
                    if (i < nmfdio.tAllStatus.nCntDIBrd * NMF.BRD_VS_DIO_PINS)
                    {
                        /*chkBox.Enabled = true;
                         * chkBox.ImageIndex = nPinStatus[i];
                         * if (chkBox.ImageIndex == 0)
                         *  chkBox.CheckState = CheckState.Unchecked;
                         * else
                         *  chkBox.CheckState = CheckState.Checked;*/
                        if (nPinStatus[i] == 1)
                        {
                            state[i] = true;
                        }
                        else
                        {
                            state[i] = false;
                        }
                    }
                    else
                    {
                        state[i] = false;
                    }
                }
                myledbulb1.On  = state[0];
                myledbulb2.On  = state[1];
                myledbulb3.On  = state[2];
                myledbulb4.On  = state[4];
                myledbulb5.On  = state[5];
                myledbulb6.On  = state[6];
                myledbulb7.On  = state[7];
                myledbulb8.On  = state[8];
                myledbulb9.On  = state[9];
                myledbulb10.On = state[10];
                myledbulb11.On = state[11];
                myledbulb12.On = state[12];
                //myledbulb13.On = state[13];
                myledbulb14.On = state[14];
            }
            catch
            {
                NMFTimer.Enabled = false;
                MessageBox.Show("NMF연결 상태를 확인해주세요");
            }
        }
Esempio n. 15
0
        private async void IDRead()
        {
            short nRet = 0;
            bool  isIDOK;

            NMFstate[0]++;
            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)0);

            await Task.Delay(1000);

            /*Idread.ReadID();
             * Txt_ICID.Text = Idread.GetID();*/
            TimeSpan spanTime = TimeSpan.FromMilliseconds(1000);

            isRecv = false;
            DateTime start = DateTime.Now;

            ID = "";

            SSC_.Write("OWT2800372f5a2\r\n");

            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }

            isRecv = false;
            start  = DateTime.Now;
            SSC_.Write("OR_28002\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }
            //recv = recv.Remove(0, 2);
            //ID = recv;
            ReadData = "";

            isRecv = false;
            start  = DateTime.Now;
            SSC_.Write("OW_28003240079\r\n");

            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }

            isRecv = false;
            start  = DateTime.Now;
            SSC_.Write("OR_28003\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }
            if (ReadData.Length > 3)
            {
                ReadData = ReadData.Remove(0, 3);
            }
            ID = ReadData;

            isRecv = false;
            isIDOK = false;
            start  = DateTime.Now;

            SSC_.Write("OW_2800324007A\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }
            isRecv = false;
            start  = DateTime.Now;
            SSC_.Write("OR_28003\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }

            if (ReadData.Length > 3)
            {
                ReadData = ReadData.Remove(0, 3);
            }
            ID += ReadData;

            isRecv = false;
            isIDOK = false;
            start  = DateTime.Now;

            SSC_.Write("OW_2800324007B\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }

            isRecv = false;
            start  = DateTime.Now;
            SSC_.Write("OR_28003\r\n");

            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }

            if (ReadData.Length > 3)
            {
                ReadData = ReadData.Remove(0, 3);
            }
            ID += ReadData;

            isRecv = false;
            isIDOK = false;
            start  = DateTime.Now;

            SSC_.Write("OW_2800324007C\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }

            isRecv = false;
            start  = DateTime.Now;
            SSC_.Write("OR_28003\r\n");
            while (true)
            {
                if (DateTime.Now - start > spanTime)
                {
                    break;
                }
                if (isRecv)
                {
                    break;
                }
            }
            if (ReadData.Length > 3)
            {
                ReadData = ReadData.Remove(0, 3);
            }
            ID += ReadData;
            ID  = ID.Replace(@"
"                                , "");

            Txt_Test.Text = ID;

            NMFstate[0]++;
            nRet = NMF.nmf_DOSetTogPin(DevNum, (short)0);
        }