• 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;
    }
  • 相关阅读:
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 2.安装配置OpenStack基础服务
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 1.操作系统环境配置
    ssh免密登录
    PHP实现识别带emoji表情的字符串
    算法-PHP实现八大算法
    项目总结-第一次外包项目2-项目相关
    PHP.52-TP框架商城应用实例-前台4-商品详情页-面包屑导航、AJAX浏览历史
    PHP.51-TP框架商城应用实例-前台3-楼层推荐、TP框架雪崩问题
    PHP.50-TP框架商城应用实例-前台2-商品推荐
    PHP.49-TP框架商城应用实例-前台1-公共布局、制作首页
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205282.html
Copyright © 2020-2023  润新知