zoukankan      html  css  js  c++  java
  • (OK) 隐马尔可夫模型(Hidden Markov Model,HMM)server




    vim tcp_server.c

    /*
    [root@bupt src]# cd markov-main/mim-hmm/src
    
    [root@bupt src]# make
    [root@bupt src]# ./hxx_baumwelch-1   0.5 0.5 0.5 0.5   0.7 0.2 0.1 0.1 0.2 0.7   0.9 0.1   100
    */
    
    //ztg add
    //-----------------------------------
    extern "C"
    {
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    }
    //-----------------------------------
    
    
    
    #include "hxx.hpp"
    
    #include <algorithm>
    #include <iostream>
    #include <vector>
    using std::cout;
    using std::random_shuffle;
    using std::vector;
    
    char buf2[256];
    void output_biglambda (const hxx_matrices&);
    void hmm (char* argv[]);
    
    //ztg add
    //-----------------------------------
    int main (int argc, char* argv[])
    {
      int iii, BUF_SIZE = 256;
      char buf[BUF_SIZE];
      char *token, *hmm_str[13];
    
      /* delete the socket file */
      unlink("/data/hmm_socket");
    
      /* create a socket */
      int server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    
      struct sockaddr_un server_addr;
      server_addr.sun_family = AF_UNIX;
      strcpy(server_addr.sun_path, "/data/hmm_socket");
    
      /* bind with the local file */
      bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    
      /* listen */
      listen(server_sockfd, 5);
    
      int client_sockfd;
      struct sockaddr_un client_addr;
      socklen_t len = sizeof(client_addr);
      while(1)
      {
        printf("server waiting:
    ");
    
        /* accept a connection */
        client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &len);
    
        /* exchange data */
        //read(client_sockfd, &ch, 1);
        bzero(buf, BUF_SIZE);
        recv(client_sockfd, buf, BUF_SIZE, MSG_NOSIGNAL);
        printf("get char from client: %s
    ", buf);
    
        iii = 0;
        token = strtok(buf, "-");
        while (token != NULL)
        {
          hmm_str[iii++] = token;
          //printf("%s
    ", token);
          token = strtok(NULL, "-");
        }
    
        for (int i=0; i<13; ++i) printf("%s
    ", hmm_str[i]);
    
        bzero(buf2, BUF_SIZE);
        hmm(hmm_str);
        send(client_sockfd, buf2, strlen(buf2), MSG_NOSIGNAL);
    
        //bzero(buf2, BUF_SIZE);
        //sprintf(buf2, "%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s", hmm_str[0], hmm_str[1], hmm_str[2], hmm_str[3], hmm_str[4], hmm_str[5], hmm_str[6], hmm_str[7], hmm_str[8], hmm_str[9], hmm_str[10], hmm_str[11], hmm_str[12]);
        //send(client_sockfd, buf2, strlen(buf2), MSG_NOSIGNAL);
    
        /* close the socket */
        close(client_sockfd);
      }
    
      return 0;
    }
    //-----------------------------------
    
    
    //ztg alter
    //int main (int argc, char* argv[])
    void hmm (char* argv[])
    {
    //ztg add
    //-----------------------------------
    /*
        double a1 = atof(argv[1]), a2 = atof(argv[2]), a3 = atof(argv[3]), a4 = atof(argv[4]);
        double b1 = atof(argv[5]), b2 = atof(argv[6]), b3 = atof(argv[7]), b4 = atof(argv[8]), b5 = atof(argv[9]), b6 = atof(argv[10]);
        double p1 = atof(argv[11]), p2 = atof(argv[12]);
        int times = atoi(argv[13]);
    //*/
        double a1 = atof(argv[0]), a2 = atof(argv[1]), a3 = atof(argv[2]), a4 = atof(argv[3]);
        double b1 = atof(argv[4]), b2 = atof(argv[5]), b3 = atof(argv[6]), b4 = atof(argv[7]), b5 = atof(argv[8]), b6 = atof(argv[9]);
        double p1 = atof(argv[10]), p2 = atof(argv[11]);
        int times = atoi(argv[12]);
    //-----------------------------------
    
    /*
    λ = (H, O, A, B, π )
    H = {connect, disconnect}
    O = {good, moderate, bad}
    A = {a11, a12,
         a21, a22};
    B = {b11, b12, b13,
         b21, b22, b23};
    Pi = {p1, p2};
    
    0.5 0.5 0.5 0.5   0.7 0.2 0.1 0.1 0.2 0.7   0.9 0.1
    */
    
    //ztg alter
    //-----------------------------------
    /*
        auto A = {.5, .5,
                  .5, .5};
    
        auto B = {.7, .2, .1,
                  .1, .2, .7};
    
        auto Pi = {.9, .1};
    //*/
        auto A = {a1, a2,
                  a3, a4};
    
        auto B = {b1, b2, b3,
                  b4, b5, b6};
    
        auto Pi = {p1, p2};
    //-----------------------------------
    
    
        hxx_matrices biglambda (A, B, Pi);
    
        hxx_gen hg (A, B, Pi);
    
        pair<int, int> tmp;
    
    //ztg alter
        //int T = 100000;
        int T = times;
    
        vector<int> Q (T);
        vector<int> O (T);
    
        for (int t = 0; t < T; ++t) {
            tmp = hg ();
            Q[t] = tmp.first;
            O[t] = tmp.second;
        }
    
        vector<double> Xi, Gamma;
    
        //random_shuffle (O.begin (), O.end ());
    
        hxx_forwardbackward (O, biglambda, Xi, Gamma);
    
        hxx_matrices newbiglambda (A, B, Pi);
    
        hxx_baumwelch (O, Xi, Gamma, newbiglambda);
    
        for (int t = 4; t < T; ++t) {
            double likelihood = hxx_forward (O.cbegin () + t - 4, O.cbegin () + t + 1, A, B, Pi);
            cout << Q[t] << " " << O[t] << " " << likelihood << "
    ";
        }
    
    //ztg alter
    //-----------------------------------
    //*
        cout << "Original biglambda
    ";
        output_biglambda (biglambda);
        cout << "New biglambda
    ";
        output_biglambda (newbiglambda);
    //*/
        int ii = 0;
        double *hmm_str[12];
        int N = newbiglambda.N ();
        int M = newbiglambda.M ();
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                hmm_str[ii++] = & newbiglambda.a (i, j);
            }
        }
        for (int i = 0; i < N; ++i) {
            for (int k = 0; k < M; ++k) {
                hmm_str[ii++] = & newbiglambda.b (i, k);
            }
        }
        for (int i = 0; i < N; ++i) {
            hmm_str[ii++] = & newbiglambda.p (i);
        }
    
        sprintf(buf2, "%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%d", *hmm_str[0], *hmm_str[1], *hmm_str[2], *hmm_str[3], *hmm_str[4], *hmm_str[5], *hmm_str[6], *hmm_str[7], *hmm_str[8], *hmm_str[9], *hmm_str[10], *hmm_str[11], 100);
        printf("%s
    ", buf2);
    //-----------------------------------
    }
    
    void output_biglambda (const hxx_matrices& biglambda) {
        int N = biglambda.N ();
        int M = biglambda.M ();
    
        cout << "A = {";
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                cout << " " << biglambda.a (i, j);
            }
        }
        cout << " }
    ";
    
        cout << "B = {";
        for (int i = 0; i < N; ++i) {
            for (int k = 0; k < M; ++k) {
                cout << " " << biglambda.b (i, k);
            }
        }
        cout << " }
    ";
    
        cout << "Pi = {";
        for (int i = 0; i < N; ++i) {
            cout << " " << biglambda.p (i);
        }
        cout << " }
    ";
    }


    vim tcp_client.c

    /*
    gcc -g -Wall -o tcp_client tcp_client.c
    */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
      int iii, BUF_SIZE = 256;
      char buf[BUF_SIZE];
      double hmm[12] = {0.5, 0.5, 0.5, 0.5, 0.7, 0.2, 0.1, 0.1, 0.2, 0.7, 0.9, 0.1};
      int times = 100;
      char *token, *hmm_str[13];
    
      /* create a socket */
      int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    
      struct sockaddr_un address;
      address.sun_family = AF_UNIX;
      strcpy(address.sun_path, "/data/hmm_socket");
    
      /* connect to the server */
      int result = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
      if(result == -1)
      {
        perror("connect failed: ");
        exit(1);
      }
    
      /* exchange data */
      //char ch = 'A';
      //write(sockfd, &ch, 1);
      //read(sockfd, &ch, 1);
    
      //printf输出float和double都可以用%f,double还可以用%lf
      //scanf输入float用%f,double输入用%lf,不能混用
      //printf("%10.4f
    ",123.45678), 10.4总宽10 (包括小数点), 小数部分保留4位(四舍五入), 结果为:  123.4568(前面两个空格)
      bzero(buf, BUF_SIZE);
      sprintf(buf, "%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%.10f-%d", hmm[0], hmm[1], hmm[2], hmm[3], hmm[4], hmm[5], hmm[6], hmm[7], hmm[8], hmm[9], hmm[10], hmm[11], times);
      //linux下当连接断开,还发数据的时候,不仅send()的返回值会有反映,而且还会向系统发送一个异常消息,如果不作处理,系统会出BrokePipe,
      //程序会退出,这对于服务器提供稳定的服务将造成巨大的灾难。为此,send()函数的最后一个参数可以设MSG_NOSIGNAL,禁止send()函数向系统发送异常消息
      send(sockfd, buf, strlen(buf), MSG_NOSIGNAL);
      bzero(buf, BUF_SIZE);
      recv(sockfd, buf, BUF_SIZE, MSG_NOSIGNAL);
      printf("get char from server: %s
    ", buf);
    
      iii = 0;
      token = strtok(buf, "-");
      while (token != NULL)
      {
        hmm_str[iii++] = token;
        //printf("%s
    ", token);
        token = strtok(NULL, "-");
      }
    
      for (int i=0; i<13; ++i) printf("%s
    ", hmm_str[i]);
    
      /* close the socket */
      close(sockfd);
    
      return 0;
    }





  • 相关阅读:
    mysql聚合函数
    轮播图与定时器
    3.23 参数 DOM 鼠标点击跟移入事件
    循环+数组
    for循环
    JS讲解跟遇到的问题
    CSS标签和属性回顾,JS初步了解
    2018.03.14理工大网站问题及解决办法
    2018.3.13 浮动 定位
    2018.3.12CSS样式
  • 原文地址:https://www.cnblogs.com/ztguang/p/12644451.html
Copyright © 2011-2022 走看看