annotate toolboxes/SVM-light/src/svm_loqo.c @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 /***********************************************************************/
Daniel@0 2 /* */
Daniel@0 3 /* svm_loqo.c */
Daniel@0 4 /* */
Daniel@0 5 /* Interface to the PR_LOQO optimization package for SVM. */
Daniel@0 6 /* */
Daniel@0 7 /* Author: Thorsten Joachims */
Daniel@0 8 /* Date: 19.07.99 */
Daniel@0 9 /* */
Daniel@0 10 /* Copyright (c) 1999 Universitaet Dortmund - All rights reserved */
Daniel@0 11 /* */
Daniel@0 12 /* This software is available for non-commercial use only. It must */
Daniel@0 13 /* not be modified and distributed without prior permission of the */
Daniel@0 14 /* author. The author is not responsible for implications from the */
Daniel@0 15 /* use of this software. */
Daniel@0 16 /* */
Daniel@0 17 /***********************************************************************/
Daniel@0 18
Daniel@0 19 # include <math.h>
Daniel@0 20 # include "pr_loqo/pr_loqo.h"
Daniel@0 21 # include "svm_common.h"
Daniel@0 22
Daniel@0 23 /* Common Block Declarations */
Daniel@0 24
Daniel@0 25 long verbosity;
Daniel@0 26
Daniel@0 27 /* /////////////////////////////////////////////////////////////// */
Daniel@0 28
Daniel@0 29 # define DEF_PRECISION_LINEAR 1E-8
Daniel@0 30 # define DEF_PRECISION_NONLINEAR 1E-14
Daniel@0 31
Daniel@0 32 double *optimize_qp();
Daniel@0 33 double *primal=0,*dual=0;
Daniel@0 34 double init_margin=0.15;
Daniel@0 35 long init_iter=500,precision_violations=0;
Daniel@0 36 double model_b;
Daniel@0 37 double opt_precision=DEF_PRECISION_LINEAR;
Daniel@0 38
Daniel@0 39 /* /////////////////////////////////////////////////////////////// */
Daniel@0 40
Daniel@0 41 void *my_malloc();
Daniel@0 42
Daniel@0 43 double *optimize_qp(qp,epsilon_crit,nx,threshold,learn_parm)
Daniel@0 44 QP *qp;
Daniel@0 45 double *epsilon_crit;
Daniel@0 46 long nx; /* Maximum number of variables in QP */
Daniel@0 47 double *threshold;
Daniel@0 48 LEARN_PARM *learn_parm;
Daniel@0 49 /* start the optimizer and return the optimal values */
Daniel@0 50 {
Daniel@0 51 register long i,j,result;
Daniel@0 52 double margin,obj_before,obj_after;
Daniel@0 53 double sigdig,dist,epsilon_loqo;
Daniel@0 54 int iter;
Daniel@0 55
Daniel@0 56 if(!primal) { /* allocate memory at first call */
Daniel@0 57 primal=(double *)my_malloc(sizeof(double)*nx*3);
Daniel@0 58 dual=(double *)my_malloc(sizeof(double)*(nx*2+1));
Daniel@0 59 }
Daniel@0 60
Daniel@0 61 if(verbosity>=4) { /* really verbose */
Daniel@0 62 printf("\n\n");
Daniel@0 63 for(i=0;i<qp->opt_n;i++) {
Daniel@0 64 printf("%f: ",qp->opt_g0[i]);
Daniel@0 65 for(j=0;j<qp->opt_n;j++) {
Daniel@0 66 printf("%f ",qp->opt_g[i*qp->opt_n+j]);
Daniel@0 67 }
Daniel@0 68 printf(": a%ld=%.10f < %f",i,qp->opt_xinit[i],qp->opt_up[i]);
Daniel@0 69 printf(": y=%f\n",qp->opt_ce[i]);
Daniel@0 70 }
Daniel@0 71 for(j=0;j<qp->opt_m;j++) {
Daniel@0 72 printf("EQ-%ld: %f*a0",j,qp->opt_ce[j]);
Daniel@0 73 for(i=1;i<qp->opt_n;i++) {
Daniel@0 74 printf(" + %f*a%ld",qp->opt_ce[i],i);
Daniel@0 75 }
Daniel@0 76 printf(" = %f\n\n",-qp->opt_ce0[0]);
Daniel@0 77 }
Daniel@0 78 }
Daniel@0 79
Daniel@0 80 obj_before=0; /* calculate objective before optimization */
Daniel@0 81 for(i=0;i<qp->opt_n;i++) {
Daniel@0 82 obj_before+=(qp->opt_g0[i]*qp->opt_xinit[i]);
Daniel@0 83 obj_before+=(0.5*qp->opt_xinit[i]*qp->opt_xinit[i]*qp->opt_g[i*qp->opt_n+i]);
Daniel@0 84 for(j=0;j<i;j++) {
Daniel@0 85 obj_before+=(qp->opt_xinit[j]*qp->opt_xinit[i]*qp->opt_g[j*qp->opt_n+i]);
Daniel@0 86 }
Daniel@0 87 }
Daniel@0 88
Daniel@0 89 result=STILL_RUNNING;
Daniel@0 90 qp->opt_ce0[0]*=(-1.0);
Daniel@0 91 /* Run pr_loqo. If a run fails, try again with parameters which lead */
Daniel@0 92 /* to a slower, but more robust setting. */
Daniel@0 93 for(margin=init_margin,iter=init_iter;
Daniel@0 94 (margin<=0.9999999) && (result!=OPTIMAL_SOLUTION);) {
Daniel@0 95 sigdig=-log10(opt_precision);
Daniel@0 96
Daniel@0 97 result=pr_loqo((int)qp->opt_n,(int)qp->opt_m,
Daniel@0 98 (double *)qp->opt_g0,(double *)qp->opt_g,
Daniel@0 99 (double *)qp->opt_ce,(double *)qp->opt_ce0,
Daniel@0 100 (double *)qp->opt_low,(double *)qp->opt_up,
Daniel@0 101 (double *)primal,(double *)dual,
Daniel@0 102 (int)(verbosity-2),
Daniel@0 103 (double)sigdig,(int)iter,
Daniel@0 104 (double)margin,(double)(qp->opt_up[0])/4.0,(int)0);
Daniel@0 105
Daniel@0 106 if(isnan(dual[0])) { /* check for choldc problem */
Daniel@0 107 if(verbosity>=2) {
Daniel@0 108 printf("NOTICE: Restarting PR_LOQO with more conservative parameters.\n");
Daniel@0 109 }
Daniel@0 110 if(init_margin<0.80) { /* become more conservative in general */
Daniel@0 111 init_margin=(4.0*margin+1.0)/5.0;
Daniel@0 112 }
Daniel@0 113 margin=(margin+1.0)/2.0;
Daniel@0 114 (opt_precision)*=10.0; /* reduce precision */
Daniel@0 115 if(verbosity>=2) {
Daniel@0 116 printf("NOTICE: Reducing precision of PR_LOQO.\n");
Daniel@0 117 }
Daniel@0 118 }
Daniel@0 119 else if(result!=OPTIMAL_SOLUTION) {
Daniel@0 120 iter+=2000;
Daniel@0 121 init_iter+=10;
Daniel@0 122 (opt_precision)*=10.0; /* reduce precision */
Daniel@0 123 if(verbosity>=2) {
Daniel@0 124 printf("NOTICE: Reducing precision of PR_LOQO due to (%ld).\n",result);
Daniel@0 125 }
Daniel@0 126 }
Daniel@0 127 }
Daniel@0 128
Daniel@0 129 if(qp->opt_m) /* Thanks to Alex Smola for this hint */
Daniel@0 130 model_b=dual[0];
Daniel@0 131 else
Daniel@0 132 model_b=0;
Daniel@0 133
Daniel@0 134 /* Check the precision of the alphas. If results of current optimization */
Daniel@0 135 /* violate KT-Conditions, relax the epsilon on the bounds on alphas. */
Daniel@0 136 epsilon_loqo=1E-10;
Daniel@0 137 for(i=0;i<qp->opt_n;i++) {
Daniel@0 138 dist=-model_b*qp->opt_ce[i];
Daniel@0 139 dist+=(qp->opt_g0[i]+1.0);
Daniel@0 140 for(j=0;j<i;j++) {
Daniel@0 141 dist+=(primal[j]*qp->opt_g[j*qp->opt_n+i]);
Daniel@0 142 }
Daniel@0 143 for(j=i;j<qp->opt_n;j++) {
Daniel@0 144 dist+=(primal[j]*qp->opt_g[i*qp->opt_n+j]);
Daniel@0 145 }
Daniel@0 146 /* printf("LOQO: a[%d]=%f, dist=%f, b=%f\n",i,primal[i],dist,dual[0]); */
Daniel@0 147 if((primal[i]<(qp->opt_up[i]-epsilon_loqo)) && (dist < (1.0-(*epsilon_crit)))) {
Daniel@0 148 epsilon_loqo=(qp->opt_up[i]-primal[i])*2.0;
Daniel@0 149 }
Daniel@0 150 else if((primal[i]>(0+epsilon_loqo)) && (dist > (1.0+(*epsilon_crit)))) {
Daniel@0 151 epsilon_loqo=primal[i]*2.0;
Daniel@0 152 }
Daniel@0 153 }
Daniel@0 154
Daniel@0 155 for(i=0;i<qp->opt_n;i++) { /* clip alphas to bounds */
Daniel@0 156 if(primal[i]<=(0+epsilon_loqo)) {
Daniel@0 157 primal[i]=0;
Daniel@0 158 }
Daniel@0 159 else if(primal[i]>=(qp->opt_up[i]-epsilon_loqo)) {
Daniel@0 160 primal[i]=qp->opt_up[i];
Daniel@0 161 }
Daniel@0 162 }
Daniel@0 163
Daniel@0 164 obj_after=0; /* calculate objective after optimization */
Daniel@0 165 for(i=0;i<qp->opt_n;i++) {
Daniel@0 166 obj_after+=(qp->opt_g0[i]*primal[i]);
Daniel@0 167 obj_after+=(0.5*primal[i]*primal[i]*qp->opt_g[i*qp->opt_n+i]);
Daniel@0 168 for(j=0;j<i;j++) {
Daniel@0 169 obj_after+=(primal[j]*primal[i]*qp->opt_g[j*qp->opt_n+i]);
Daniel@0 170 }
Daniel@0 171 }
Daniel@0 172
Daniel@0 173 /* if optimizer returned NAN values, reset and retry with smaller */
Daniel@0 174 /* working set. */
Daniel@0 175 if(isnan(obj_after) || isnan(model_b)) {
Daniel@0 176 for(i=0;i<qp->opt_n;i++) {
Daniel@0 177 primal[i]=qp->opt_xinit[i];
Daniel@0 178 }
Daniel@0 179 model_b=0;
Daniel@0 180 if(learn_parm->svm_maxqpsize>2) {
Daniel@0 181 learn_parm->svm_maxqpsize--; /* decrease size of qp-subproblems */
Daniel@0 182 }
Daniel@0 183 }
Daniel@0 184
Daniel@0 185 if(obj_after >= obj_before) { /* check whether there was progress */
Daniel@0 186 (opt_precision)/=100.0;
Daniel@0 187 precision_violations++;
Daniel@0 188 if(verbosity>=2) {
Daniel@0 189 printf("NOTICE: Increasing Precision of PR_LOQO.\n");
Daniel@0 190 }
Daniel@0 191 }
Daniel@0 192
Daniel@0 193 if(precision_violations > 500) {
Daniel@0 194 (*epsilon_crit)*=10.0;
Daniel@0 195 precision_violations=0;
Daniel@0 196 if(verbosity>=1) {
Daniel@0 197 printf("\nWARNING: Relaxing epsilon on KT-Conditions.\n");
Daniel@0 198 }
Daniel@0 199 }
Daniel@0 200
Daniel@0 201 (*threshold)=model_b;
Daniel@0 202
Daniel@0 203 if(result!=OPTIMAL_SOLUTION) {
Daniel@0 204 printf("\nERROR: PR_LOQO did not converge. \n");
Daniel@0 205 return(qp->opt_xinit);
Daniel@0 206 }
Daniel@0 207 else {
Daniel@0 208 return(primal);
Daniel@0 209 }
Daniel@0 210 }
Daniel@0 211