• SMO实现


    #include "stdio.h"
    #include <vector>
    using namespace std;

    float function(float alfa[5],float H[5][5],float sign[5])
    {

            float ret = alfa[0]+alfa[1]+alfa[2]+alfa[3]+alfa[4];
            for(int j=0;j<5;++j)
            {
                    float t=0.0;
                    for(int i=0;i<5;++i)
                    {
                            t+=sign[i]*alfa[i]*H[j][i];
                    }
                    ret += -1*(t*alfa[j]*sign[j])/2;
            }
            return ret;
    }
    int main(void)
    {
            float matrix[5][4]={
                    {1,5,1},
                    {1,2,1},
                    {2,2,-1},
                    {2,1,-1},
                    {1,1,-1}};
            float H[5][5];
            vector<float> c1;
            vector<float> c2;
            for(int i=0;i<5;++i)
            {
                    c1.push_back(matrix[i][0]);
                    c2.push_back(matrix[i][1]);
            }
            for(int i=0;i<5;++i)
            {
                    for(int j=0;j<5;++j)
                    {
                            H[i][j]=c1[i]*c1[j]+c2[i]*c2[j];
                            printf("%f ",H[i][j]);
                    }
                    printf(" ");
            }
            float alfa[5]={3,3,2,2,2};
            float sign[5];
            for(int i=0;i<5;++i)
                    sign[i]=matrix[i][2];
            float last_r = function(alfa,H,sign);
            float new_r;
            float con_r;
            for(int i=0;i<5;++i)
            {
                    for(int j=0;j<5;j++)
                    {
                            printf("%f,alfa={%f,%f,%f,%f,%f} ",last_r,alfa[0],alfa[1],alfa[2],alfa[3],alfa[4]);
                            if(i==j) continue;
                            else if((alfa[i]<0.01&&alfa[i]>-0.01)&&(alfa[j]<0.01&&alfa[j]>-0.01)) continue;
                            else if((alfa[j]>0.01)&&(alfa[i]<0.01&&alfa[i]>-0.01))
                            {
                                    while(alfa[j]>0.01){
                                            alfa[i]+=0.1;
                                            new_r = function(alfa,H,sign);
                                            if( new_r > last_r )
                                            {
                                                    alfa[j] -= 0.1*sign[i]*sign[j];
                                                    last_r = function(alfa,H,sign);
                                            }
                                            else
                                            {
                                                    alfa[i]-=0.1;
                                                    break;
                                            }
                                    };
                            }
                            else if((alfa[i]>0.01)&&(alfa[j]<0.01&&alfa[j]>-0.01))
                            {
                                    while(alfa[i]>0.01){
                                            alfa[j]+=0.1;
                                            new_r = function(alfa,H,sign);
                                            if( new_r > last_r )
                                            {
                                                    alfa[i] -= 0.1*sign[i]*sign[j];
                                                    last_r = function(alfa,H,sign);
                                            }
                                            else
                                            {
                                                    alfa[j]-=0.1;
                                                    break;
                                            }
                                    };
                            }
                            else
                            {

                                    alfa[j]+=0.1;
                                    new_r = function(alfa,H,sign);
                                    alfa[j]-=0.2;
                                    con_r = function(alfa,H,sign);
                                    alfa[j]+=0.1;

                                    if(new_r>con_r&&new_r>last_r)
                                    {
                                            while(alfa[i]>0.01&&alfa[j]>0.01)
                                            {
                                                    alfa[j] += 0.1;
                                                    alfa[i] -= 0.1*sign[i]*sign[j];
                                                    new_r = function(alfa,H,sign);
                                                    if(new_r > last_r)
                                                    {
                                                            last_r = new_r;
                                                    }
                                                    else
                                                    {
                                                    alfa[j] -= 0.1;
                                                    alfa[i] += 0.1*sign[i]*sign[j];
                                                    break;
                                                    }
                                            };

                                    }
                                    else if(con_r>new_r&&con_r>last_r)
                                    {
                                            while(alfa[i]>0.01&&alfa[j]>0.01)
                                            {
                                                    alfa[j] -= 0.1;
                                                    alfa[i] += 0.1*sign[i]*sign[j];
                                                    con_r = function(alfa,H,sign);
                                                    if(con_r > last_r)
                                                    {
                                                            last_r = con_r;
                                                    }
                                                    else
                                                    {
                                                    alfa[j] += 0.1;
                                                    alfa[i] -= 0.1*sign[i]*sign[j];
                                                    break;
                                                    }
                                            }
                                    }
                                    else
                                    {}
                            }

                    }
            }
            printf("%f,alfa={%f,%f,%f,%f,%f} ",last_r,alfa[0],alfa[1],alfa[2],alfa[3],alfa[4]);
            return 0;
    }
  • 相关阅读:
    Android_EditText
    JAVA_Gson_example
    JAVA_Gson
    JAVA_eclipse 保留Java文件时自动格式化代码和优化Import
    JAVA_JSON_example
    JAVA_JSON
    JAVA_HttpClientUtils
    Android_Gallery
    JAVA_JDBC
    day05 Pyhton学习
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205282.html
Copyright © 2020-2023  润新知