Daniel@0: /***********************************************************************/ Daniel@0: /* */ Daniel@0: /* svm_hideo.c */ Daniel@0: /* */ Daniel@0: /* The Hildreth and D'Espo solver specialized for SVMs. */ Daniel@0: /* */ Daniel@0: /* Author: Thorsten Joachims */ Daniel@0: /* Date: 02.07.02 */ Daniel@0: /* */ Daniel@0: /* Copyright (c) 2002 Thorsten Joachims - All rights reserved */ Daniel@0: /* */ Daniel@0: /* This software is available for non-commercial use only. It must */ Daniel@0: /* not be modified and distributed without prior permission of the */ Daniel@0: /* author. The author is not responsible for implications from the */ Daniel@0: /* use of this software. */ Daniel@0: /* */ Daniel@0: /***********************************************************************/ Daniel@0: Daniel@0: # include Daniel@0: # include "svm_common.h" Daniel@0: Daniel@0: /* Daniel@0: solve the quadratic programming problem Daniel@0: Daniel@0: minimize g0 * x + 1/2 x' * G * x Daniel@0: subject to ce*x = ce0 Daniel@0: l <= x <= u Daniel@0: Daniel@0: The linear constraint vector ce can only have -1/+1 as entries Daniel@0: */ Daniel@0: Daniel@0: /* Common Block Declarations */ Daniel@0: Daniel@0: long verbosity; Daniel@0: Daniel@0: # define PRIMAL_OPTIMAL 1 Daniel@0: # define DUAL_OPTIMAL 2 Daniel@0: # define MAXITER_EXCEEDED 3 Daniel@0: # define NAN_SOLUTION 4 Daniel@0: # define ONLY_ONE_VARIABLE 5 Daniel@0: Daniel@0: # define LARGEROUND 0 Daniel@0: # define SMALLROUND 1 Daniel@0: Daniel@0: /* /////////////////////////////////////////////////////////////// */ Daniel@0: Daniel@0: # define DEF_PRECISION 1E-5 Daniel@0: # define DEF_MAX_ITERATIONS 200 Daniel@0: # define DEF_LINDEP_SENSITIVITY 1E-8 Daniel@0: # define EPSILON_HIDEO 1E-20 Daniel@0: # define EPSILON_EQ 1E-5 Daniel@0: Daniel@0: double *optimize_qp(QP *, double *, long, double *, LEARN_PARM *); Daniel@0: double *primal=0,*dual=0; Daniel@0: long precision_violations=0; Daniel@0: double opt_precision=DEF_PRECISION; Daniel@0: long maxiter=DEF_MAX_ITERATIONS; Daniel@0: double lindep_sensitivity=DEF_LINDEP_SENSITIVITY; Daniel@0: double *buffer; Daniel@0: long *nonoptimal; Daniel@0: Daniel@0: long smallroundcount=0; Daniel@0: long roundnumber=0; Daniel@0: Daniel@0: /* /////////////////////////////////////////////////////////////// */ Daniel@0: Daniel@0: void *my_malloc(); Daniel@0: Daniel@0: int optimize_hildreth_despo(long,long,double,double,double,long,long,long,double,double *, Daniel@0: double *,double *,double *,double *,double *, Daniel@0: double *,double *,double *,long *,double *,double *); Daniel@0: int solve_dual(long,long,double,double,long,double *,double *,double *, Daniel@0: double *,double *,double *,double *,double *,double *, Daniel@0: double *,double *,double *,double *,long); Daniel@0: Daniel@0: void linvert_matrix(double *, long, double *, double, long *); Daniel@0: void lprint_matrix(double *, long); Daniel@0: void ladd_matrix(double *, long, double); Daniel@0: void lcopy_matrix(double *, long, double *); Daniel@0: void lswitch_rows_matrix(double *, long, long, long); Daniel@0: void lswitchrk_matrix(double *, long, long, long); Daniel@0: Daniel@0: double calculate_qp_objective(long, double *, double *, double *); Daniel@0: Daniel@0: Daniel@0: Daniel@0: double *optimize_qp(qp,epsilon_crit,nx,threshold,learn_parm) Daniel@0: QP *qp; Daniel@0: double *epsilon_crit; Daniel@0: long nx; /* Maximum number of variables in QP */ Daniel@0: double *threshold; Daniel@0: LEARN_PARM *learn_parm; Daniel@0: /* start the optimizer and return the optimal values */ Daniel@0: /* The HIDEO optimizer does not necessarily fully solve the problem. */ Daniel@0: /* Since it requires a strictly positive definite hessian, the solution */ Daniel@0: /* is restricted to a linear independent subset in case the matrix is */ Daniel@0: /* only semi-definite. */ Daniel@0: { Daniel@0: long i,j; Daniel@0: int result; Daniel@0: double eq,progress; Daniel@0: Daniel@0: roundnumber++; Daniel@0: Daniel@0: if(!primal) { /* allocate memory at first call */ Daniel@0: primal=(double *)my_malloc(sizeof(double)*nx); Daniel@0: dual=(double *)my_malloc(sizeof(double)*((nx+1)*2)); Daniel@0: nonoptimal=(long *)my_malloc(sizeof(long)*(nx)); Daniel@0: buffer=(double *)my_malloc(sizeof(double)*((nx+1)*2*(nx+1)*2+ Daniel@0: nx*nx+2*(nx+1)*2+2*nx+1+2*nx+ Daniel@0: nx+nx+nx*nx)); Daniel@0: (*threshold)=0; Daniel@0: for(i=0;i=4) { /* really verbose */ Daniel@0: printf("\n\n"); Daniel@0: eq=qp->opt_ce0[0]; Daniel@0: for(i=0;iopt_n;i++) { Daniel@0: eq+=qp->opt_xinit[i]*qp->opt_ce[i]; Daniel@0: printf("%f: ",qp->opt_g0[i]); Daniel@0: for(j=0;jopt_n;j++) { Daniel@0: printf("%f ",qp->opt_g[i*qp->opt_n+j]); Daniel@0: } Daniel@0: printf(": a=%.10f < %f",qp->opt_xinit[i],qp->opt_up[i]); Daniel@0: printf(": y=%f\n",qp->opt_ce[i]); Daniel@0: } Daniel@0: if(qp->opt_m) { Daniel@0: printf("EQ: %f*x0",qp->opt_ce[0]); Daniel@0: for(i=1;iopt_n;i++) { Daniel@0: printf(" + %f*x%ld",qp->opt_ce[i],i); Daniel@0: } Daniel@0: printf(" = %f\n\n",-qp->opt_ce0[0]); Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: result=optimize_hildreth_despo(qp->opt_n,qp->opt_m, Daniel@0: opt_precision,(*epsilon_crit), Daniel@0: learn_parm->epsilon_a,maxiter, Daniel@0: /* (long)PRIMAL_OPTIMAL, */ Daniel@0: (long)0, (long)0, Daniel@0: lindep_sensitivity, Daniel@0: qp->opt_g,qp->opt_g0,qp->opt_ce,qp->opt_ce0, Daniel@0: qp->opt_low,qp->opt_up,primal,qp->opt_xinit, Daniel@0: dual,nonoptimal,buffer,&progress); Daniel@0: if(verbosity>=3) { Daniel@0: printf("return(%d)...",result); Daniel@0: } Daniel@0: Daniel@0: if(learn_parm->totwords < learn_parm->svm_maxqpsize) { Daniel@0: /* larger working sets will be linear dependent anyway */ Daniel@0: learn_parm->svm_maxqpsize=maxl(learn_parm->totwords,(long)2); Daniel@0: } Daniel@0: Daniel@0: if(result == NAN_SOLUTION) { Daniel@0: lindep_sensitivity*=2; /* throw out linear dependent examples more */ Daniel@0: /* generously */ Daniel@0: if(learn_parm->svm_maxqpsize>2) { Daniel@0: learn_parm->svm_maxqpsize--; /* decrease size of qp-subproblems */ Daniel@0: } Daniel@0: precision_violations++; Daniel@0: } Daniel@0: Daniel@0: /* take one round of only two variable to get unstuck */ Daniel@0: if((result != PRIMAL_OPTIMAL) || (!(roundnumber % 31)) || (progress <= 0)) { Daniel@0: Daniel@0: smallroundcount++; Daniel@0: Daniel@0: result=optimize_hildreth_despo(qp->opt_n,qp->opt_m, Daniel@0: opt_precision,(*epsilon_crit), Daniel@0: learn_parm->epsilon_a,(long)maxiter, Daniel@0: (long)PRIMAL_OPTIMAL,(long)SMALLROUND, Daniel@0: lindep_sensitivity, Daniel@0: qp->opt_g,qp->opt_g0,qp->opt_ce,qp->opt_ce0, Daniel@0: qp->opt_low,qp->opt_up,primal,qp->opt_xinit, Daniel@0: dual,nonoptimal,buffer,&progress); Daniel@0: if(verbosity>=3) { Daniel@0: printf("return_srd(%d)...",result); Daniel@0: } Daniel@0: Daniel@0: if(result != PRIMAL_OPTIMAL) { Daniel@0: if(result != ONLY_ONE_VARIABLE) Daniel@0: precision_violations++; Daniel@0: if(result == MAXITER_EXCEEDED) Daniel@0: maxiter+=100; Daniel@0: if(result == NAN_SOLUTION) { Daniel@0: lindep_sensitivity*=2; /* throw out linear dependent examples more */ Daniel@0: /* generously */ Daniel@0: /* results not valid, so return inital values */ Daniel@0: for(i=0;iopt_n;i++) { Daniel@0: primal[i]=qp->opt_xinit[i]; Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: Daniel@0: if(precision_violations > 50) { Daniel@0: precision_violations=0; Daniel@0: (*epsilon_crit)*=10.0; Daniel@0: if(verbosity>=1) { Daniel@0: printf("\nWARNING: Relaxing epsilon on KT-Conditions (%f).\n", Daniel@0: (*epsilon_crit)); Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: if((qp->opt_m>0) && (result != NAN_SOLUTION) && (!isnan(dual[1]-dual[0]))) Daniel@0: (*threshold)=dual[1]-dual[0]; Daniel@0: else Daniel@0: (*threshold)=0; Daniel@0: Daniel@0: if(verbosity>=4) { /* really verbose */ Daniel@0: printf("\n\n"); Daniel@0: eq=qp->opt_ce0[0]; Daniel@0: for(i=0;iopt_n;i++) { Daniel@0: eq+=primal[i]*qp->opt_ce[i]; Daniel@0: printf("%f: ",qp->opt_g0[i]); Daniel@0: for(j=0;jopt_n;j++) { Daniel@0: printf("%f ",qp->opt_g[i*qp->opt_n+j]); Daniel@0: } Daniel@0: printf(": a=%.30f",primal[i]); Daniel@0: printf(": nonopti=%ld",nonoptimal[i]); Daniel@0: printf(": y=%f\n",qp->opt_ce[i]); Daniel@0: } Daniel@0: printf("eq-constraint=%.30f\n",eq); Daniel@0: printf("b=%f\n",(*threshold)); Daniel@0: printf(" smallroundcount=%ld ",smallroundcount); Daniel@0: } Daniel@0: Daniel@0: return(primal); Daniel@0: } Daniel@0: Daniel@0: Daniel@0: Daniel@0: int optimize_hildreth_despo(n,m,precision,epsilon_crit,epsilon_a,maxiter,goal, Daniel@0: smallround,lindep_sensitivity,g,g0,ce,ce0,low,up, Daniel@0: primal,init,dual,lin_dependent,buffer,progress) Daniel@0: long n; /* number of variables */ Daniel@0: long m; /* number of linear equality constraints [0,1] */ Daniel@0: double precision; /* solve at least to this dual precision */ Daniel@0: double epsilon_crit; /* stop, if KT-Conditions approx fulfilled */ Daniel@0: double epsilon_a; /* precision of alphas at bounds */ Daniel@0: long maxiter; /* stop after this many iterations */ Daniel@0: long goal; /* keep going until goal fulfilled */ Daniel@0: long smallround; /* use only two variables of steepest descent */ Daniel@0: double lindep_sensitivity; /* epsilon for detecting linear dependent ex */ Daniel@0: double *g; /* hessian of objective */ Daniel@0: double *g0; /* linear part of objective */ Daniel@0: double *ce,*ce0; /* linear equality constraints */ Daniel@0: double *low,*up; /* box constraints */ Daniel@0: double *primal; /* primal variables */ Daniel@0: double *init; /* initial values of primal */ Daniel@0: double *dual; /* dual variables */ Daniel@0: long *lin_dependent; Daniel@0: double *buffer; Daniel@0: double *progress; /* delta in the objective function between Daniel@0: before and after */ Daniel@0: { Daniel@0: long i,j,k,from,to,n_indep,changed; Daniel@0: double sum,bmin=0,bmax=0; Daniel@0: double *d,*d0,*ig,*dual_old,*temp,*start; Daniel@0: double *g0_new,*g_new,*ce_new,*ce0_new,*low_new,*up_new; Daniel@0: double add,t; Daniel@0: int result; Daniel@0: double obj_before,obj_after; Daniel@0: long b1,b2; Daniel@0: double g0_b1,g0_b2,ce0_b; Daniel@0: Daniel@0: g0_new=&(buffer[0]); /* claim regions of buffer */ Daniel@0: d=&(buffer[n]); Daniel@0: d0=&(buffer[n+(n+m)*2*(n+m)*2]); Daniel@0: ce_new=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2]); Daniel@0: ce0_new=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n]); Daniel@0: ig=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m]); Daniel@0: dual_old=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m+n*n]); Daniel@0: low_new=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m+n*n+(n+m)*2]); Daniel@0: up_new=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m+n*n+(n+m)*2+n]); Daniel@0: start=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m+n*n+(n+m)*2+n+n]); Daniel@0: g_new=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m+n*n+(n+m)*2+n+n+n]); Daniel@0: temp=&(buffer[n+(n+m)*2*(n+m)*2+(n+m)*2+n+m+n*n+(n+m)*2+n+n+n+n*n]); Daniel@0: Daniel@0: b1=-1; Daniel@0: b2=-1; Daniel@0: for(i=0;i=( up[i]-epsilon_a)) && (ce[i]>0.0))) Daniel@0: ) { Daniel@0: bmin=sum; Daniel@0: b1=i; Daniel@0: } Daniel@0: if(((b2==-1) || (sum>=bmax)) Daniel@0: && (!((init[i]<=(low[i]+epsilon_a)) && (ce[i]>0.0))) Daniel@0: && (!((init[i]>=( up[i]-epsilon_a)) && (ce[i]<0.0))) Daniel@0: ) { Daniel@0: bmax=sum; Daniel@0: b2=i; Daniel@0: } Daniel@0: } Daniel@0: /* in case of unbiased hyperplane, the previous projection on */ Daniel@0: /* equality constraint can lead to b1 or b2 being -1. */ Daniel@0: if((b1 == -1) || (b2 == -1)) { Daniel@0: b1=maxl(b1,b2); Daniel@0: b2=maxl(b1,b2); Daniel@0: } Daniel@0: Daniel@0: for(i=0;i g0_b2) { /* set b2 to upper bound */ Daniel@0: /* printf("case +=>\n"); */ Daniel@0: changed=1; Daniel@0: t=up[b2]-init[b2]; Daniel@0: if((init[b1]-low[b1]) < t) { Daniel@0: t=init[b1]-low[b1]; Daniel@0: } Daniel@0: start[b1]=init[b1]-t; Daniel@0: start[b2]=init[b2]+t; Daniel@0: } Daniel@0: } Daniel@0: else if(((g[b1*n+b1]>0) || (g[b2*n+b2]>0))) { /* (ce[b1] != ce[b2]) */ Daniel@0: /* printf("case +!\n"); */ Daniel@0: t=((ce[b2]/ce[b1])*g0[b1]-g0[b2]+ce0[0]*(g[b1*n+b1]*ce[b2]/ce[b1]-g[b1*n+b2]/ce[b1]))/((ce[b2]*ce[b2]/(ce[b1]*ce[b1]))*g[b1*n+b1]+g[b2*n+b2]-2*(g[b1*n+b2]*ce[b2]/ce[b1]))-init[b2]; Daniel@0: changed=1; Daniel@0: if((up[b2]-init[b2]) < t) { Daniel@0: t=up[b2]-init[b2]; Daniel@0: } Daniel@0: if((init[b2]-low[b2]) < -t) { Daniel@0: t=-(init[b2]-low[b2]); Daniel@0: } Daniel@0: if((up[b1]-init[b1]) < t) { Daniel@0: t=(up[b1]-init[b1]); Daniel@0: } Daniel@0: if((init[b1]-low[b1]) < -t) { Daniel@0: t=-(init[b1]-low[b1]); Daniel@0: } Daniel@0: start[b1]=init[b1]+t; Daniel@0: start[b2]=init[b2]+t; Daniel@0: } Daniel@0: } Daniel@0: if((-g[b1*n+b2] == g[b1*n+b1]) && (-g[b1*n+b2] == g[b2*n+b2])) { Daniel@0: /* printf("diffeuqal\n"); */ Daniel@0: if(ce[b1] != ce[b2]) { Daniel@0: if((g0_b1+g0_b2) < 0) { /* set b1 and b2 to upper bound */ Daniel@0: /* printf("case -!<\n"); */ Daniel@0: changed=1; Daniel@0: t=up[b1]-init[b1]; Daniel@0: if((up[b2]-init[b2]) < t) { Daniel@0: t=up[b2]-init[b2]; Daniel@0: } Daniel@0: start[b1]=init[b1]+t; Daniel@0: start[b2]=init[b2]+t; Daniel@0: } Daniel@0: else if((g0_b1+g0_b2) >= 0) { /* set b1 and b2 to lower bound */ Daniel@0: /* printf("case -!>\n"); */ Daniel@0: changed=1; Daniel@0: t=init[b1]-low[b1]; Daniel@0: if((init[b2]-low[b2]) < t) { Daniel@0: t=init[b2]-low[b2]; Daniel@0: } Daniel@0: start[b1]=init[b1]-t; Daniel@0: start[b2]=init[b2]-t; Daniel@0: } Daniel@0: } Daniel@0: else if(((g[b1*n+b1]>0) || (g[b2*n+b2]>0))) { /* (ce[b1]==ce[b2]) */ Daniel@0: /* printf("case -=\n"); */ Daniel@0: t=((ce[b2]/ce[b1])*g0[b1]-g0[b2]+ce0[0]*(g[b1*n+b1]*ce[b2]/ce[b1]-g[b1*n+b2]/ce[b1]))/((ce[b2]*ce[b2]/(ce[b1]*ce[b1]))*g[b1*n+b1]+g[b2*n+b2]-2*(g[b1*n+b2]*ce[b2]/ce[b1]))-init[b2]; Daniel@0: changed=1; Daniel@0: if((up[b2]-init[b2]) < t) { Daniel@0: t=up[b2]-init[b2]; Daniel@0: } Daniel@0: if((init[b2]-low[b2]) < -t) { Daniel@0: t=-(init[b2]-low[b2]); Daniel@0: } Daniel@0: if((up[b1]-init[b1]) < -t) { Daniel@0: t=-(up[b1]-init[b1]); Daniel@0: } Daniel@0: if((init[b1]-low[b1]) < t) { Daniel@0: t=init[b1]-low[b1]; Daniel@0: } Daniel@0: start[b1]=init[b1]-t; Daniel@0: start[b2]=init[b2]+t; Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: /* if we have a biased hyperplane, then adding a constant to the */ Daniel@0: /* hessian does not change the solution. So that is done for examples */ Daniel@0: /* with zero diagonal entry, since HIDEO cannot handle them. */ Daniel@0: if((m>0) Daniel@0: && ((fabs(g[b1*n+b1]) < lindep_sensitivity) Daniel@0: || (fabs(g[b2*n+b2]) < lindep_sensitivity))) { Daniel@0: /* printf("Case 0\n"); */ Daniel@0: add+=0.093274; Daniel@0: } Daniel@0: /* in case both examples are linear dependent */ Daniel@0: else if((m>0) Daniel@0: && (g[b1*n+b2] != 0 && g[b2*n+b2] != 0) Daniel@0: && (fabs(g[b1*n+b1]/g[b1*n+b2] - g[b1*n+b2]/g[b2*n+b2]) Daniel@0: < lindep_sensitivity)) { Daniel@0: /* printf("Case lindep\n"); */ Daniel@0: add+=0.078274; Daniel@0: } Daniel@0: Daniel@0: /* special case for zero diagonal entry on unbiased hyperplane */ Daniel@0: if((m==0) && (b1>=0)) { Daniel@0: if(fabs(g[b1*n+b1]) < lindep_sensitivity) { Daniel@0: /* printf("Case 0b1\n"); */ Daniel@0: for(i=0;i=0) Daniel@0: start[b1]=low[b1]; Daniel@0: } Daniel@0: } Daniel@0: if((m==0) && (b2>=0)) { Daniel@0: if(fabs(g[b2*n+b2]) < lindep_sensitivity) { Daniel@0: /* printf("Case 0b2\n"); */ Daniel@0: for(i=0;i=0) Daniel@0: start[b2]=low[b2]; Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: /* printf("b1=%ld,b2=%ld\n",b1,b2); */ Daniel@0: Daniel@0: lcopy_matrix(g,n,d); Daniel@0: if((m==1) && (add>0.0)) { Daniel@0: for(j=0;j2) { /* switch, so that variables are better mixed */ Daniel@0: lswitchrk_matrix(d,n,b1,(long)0); Daniel@0: if(b2 == 0) Daniel@0: lswitchrk_matrix(d,n,b1,(long)1); Daniel@0: else Daniel@0: lswitchrk_matrix(d,n,b2,(long)1); Daniel@0: } Daniel@0: if(smallround == SMALLROUND) { Daniel@0: for(i=2;i0) { /* for biased hyperplane, pick two variables */ Daniel@0: lin_dependent[0]=0; Daniel@0: lin_dependent[1]=0; Daniel@0: } Daniel@0: else { /* for unbiased hyperplane, pick only one variable */ Daniel@0: lin_dependent[0]=smallroundcount % 2; Daniel@0: lin_dependent[1]=(smallroundcount+1) % 2; Daniel@0: } Daniel@0: } Daniel@0: else { Daniel@0: for(i=0;i2) { /* now switch back */ Daniel@0: if(b2 == 0) { Daniel@0: lswitchrk_matrix(ig,n,b1,(long)1); Daniel@0: i=lin_dependent[1]; Daniel@0: lin_dependent[1]=lin_dependent[b1]; Daniel@0: lin_dependent[b1]=i; Daniel@0: } Daniel@0: else { Daniel@0: lswitchrk_matrix(ig,n,b2,(long)1); Daniel@0: i=lin_dependent[1]; Daniel@0: lin_dependent[1]=lin_dependent[b2]; Daniel@0: lin_dependent[b2]=i; Daniel@0: } Daniel@0: lswitchrk_matrix(ig,n,b1,(long)0); Daniel@0: i=lin_dependent[0]; Daniel@0: lin_dependent[0]=lin_dependent[b1]; Daniel@0: lin_dependent[b1]=i; Daniel@0: } Daniel@0: /* lprint_matrix(d,n); */ Daniel@0: /* lprint_matrix(ig,n); */ Daniel@0: Daniel@0: lcopy_matrix(g,n,g_new); /* restore g_new matrix */ Daniel@0: if(add>0) Daniel@0: for(j=0;j0) ce0_new[0]=-ce0[0]; Daniel@0: for(i=0;i0) ce0_new[0]-=(start[i]*ce[i]); Daniel@0: } Daniel@0: } Daniel@0: from=0; /* remove linear dependent vectors */ Daniel@0: to=0; Daniel@0: n_indep=0; Daniel@0: for(i=0;i=3) { Daniel@0: printf("real_qp_size(%ld)...",n_indep); Daniel@0: } Daniel@0: Daniel@0: /* cannot optimize with only one variable */ Daniel@0: if((n_indep<=1) && (m>0) && (!changed)) { Daniel@0: for(i=n-1;i>=0;i--) { Daniel@0: primal[i]=init[i]; Daniel@0: } Daniel@0: return((int)ONLY_ONE_VARIABLE); Daniel@0: } Daniel@0: Daniel@0: if((!changed) || (n_indep>1)) { Daniel@0: result=solve_dual(n_indep,m,precision,epsilon_crit,maxiter,g_new,g0_new, Daniel@0: ce_new,ce0_new,low_new,up_new,primal,d,d0,ig, Daniel@0: dual,dual_old,temp,goal); Daniel@0: } Daniel@0: else { Daniel@0: result=PRIMAL_OPTIMAL; Daniel@0: } Daniel@0: Daniel@0: j=n_indep; Daniel@0: for(i=n-1;i>=0;i--) { Daniel@0: if(!lin_dependent[i]) { Daniel@0: j--; Daniel@0: primal[i]=primal[j]; Daniel@0: } Daniel@0: else { Daniel@0: primal[i]=start[i]; /* leave as is */ Daniel@0: } Daniel@0: temp[i]=primal[i]; Daniel@0: } Daniel@0: Daniel@0: obj_before=calculate_qp_objective(n,g,g0,init); Daniel@0: obj_after=calculate_qp_objective(n,g,g0,primal); Daniel@0: (*progress)=obj_before-obj_after; Daniel@0: if(verbosity>=3) { Daniel@0: printf("before(%.30f)...after(%.30f)...result_sd(%d)...", Daniel@0: obj_before,obj_after,result); Daniel@0: } Daniel@0: Daniel@0: return((int)result); Daniel@0: } Daniel@0: Daniel@0: Daniel@0: int solve_dual(n,m,precision,epsilon_crit,maxiter,g,g0,ce,ce0,low,up,primal, Daniel@0: d,d0,ig,dual,dual_old,temp,goal) Daniel@0: /* Solves the dual using the method of Hildreth and D'Espo. */ Daniel@0: /* Can only handle problems with zero or exactly one */ Daniel@0: /* equality constraints. */ Daniel@0: Daniel@0: long n; /* number of variables */ Daniel@0: long m; /* number of linear equality constraints */ Daniel@0: double precision; /* solve at least to this dual precision */ Daniel@0: double epsilon_crit; /* stop, if KT-Conditions approx fulfilled */ Daniel@0: long maxiter; /* stop after that many iterations */ Daniel@0: double *g; Daniel@0: double *g0; /* linear part of objective */ Daniel@0: double *ce,*ce0; /* linear equality constraints */ Daniel@0: double *low,*up; /* box constraints */ Daniel@0: double *primal; /* variables (with initial values) */ Daniel@0: double *d,*d0,*ig,*dual,*dual_old,*temp; /* buffer */ Daniel@0: long goal; Daniel@0: { Daniel@0: long i,j,k,iter; Daniel@0: double sum,w,maxviol,viol,temp1,temp2,isnantest; Daniel@0: double model_b,dist; Daniel@0: long retrain,maxfaktor,primal_optimal=0,at_bound,scalemaxiter; Daniel@0: double epsilon_a=1E-15,epsilon_hideo; Daniel@0: double eq; Daniel@0: Daniel@0: if((m<0) || (m>1)) Daniel@0: perror("SOLVE DUAL: inappropriate number of eq-constrains!"); Daniel@0: Daniel@0: /* Daniel@0: printf("\n"); Daniel@0: for(i=0;i0) { Daniel@0: sum=0; /* dual hessian for eq constraints */ Daniel@0: for(j=0;j0) { Daniel@0: sum=0; /* dual linear component for eq constraints */ Daniel@0: for(j=0;j 0) && (iter < (scalemaxiter*maxfaktor))) { Daniel@0: iter++; Daniel@0: Daniel@0: while((maxviol > precision) && (iter < (scalemaxiter*maxfaktor))) { Daniel@0: iter++; Daniel@0: maxviol=0; Daniel@0: for(i=0;i<2*(n+m);i++) { Daniel@0: sum=d0[i]; Daniel@0: for(j=0;j<2*(n+m);j++) { Daniel@0: sum+=d[i*2*(n+m)+j]*dual_old[j]; Daniel@0: } Daniel@0: sum-=d[i*2*(n+m)+i]*dual_old[i]; Daniel@0: dual[i]=-sum/d[i*2*(n+m)+i]; Daniel@0: if(dual[i]<0) dual[i]=0; Daniel@0: Daniel@0: viol=fabs(dual[i]-dual_old[i]); Daniel@0: if(viol>maxviol) Daniel@0: maxviol=viol; Daniel@0: dual_old[i]=dual[i]; Daniel@0: } Daniel@0: /* Daniel@0: printf("%d) maxviol=%20f precision=%f\n",iter,maxviol,precision); Daniel@0: */ Daniel@0: } Daniel@0: Daniel@0: if(m>0) { Daniel@0: for(i=0;i=(up[i])) { Daniel@0: primal[i]=up[i]; Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: if(m>0) Daniel@0: model_b=dual[n+n+1]-dual[n+n]; Daniel@0: else Daniel@0: model_b=0; Daniel@0: Daniel@0: epsilon_hideo=EPSILON_HIDEO; Daniel@0: for(i=0;i(low[i]+epsilon_hideo)) &&(dist>(1.0+epsilon_crit))) { Daniel@0: epsilon_hideo=(primal[i]-low[i])*2.0; Daniel@0: } Daniel@0: } Daniel@0: /* printf("\nEPSILON_HIDEO=%.30f\n",epsilon_hideo); */ Daniel@0: Daniel@0: for(i=0;i=(up[i]-epsilon_hideo)) { Daniel@0: primal[i]=up[i]; Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: retrain=0; Daniel@0: primal_optimal=1; Daniel@0: at_bound=0; Daniel@0: for(i=0;(i(low[i]+epsilon_a)) && (dist > (1.0+epsilon_crit))) { Daniel@0: retrain=1; Daniel@0: primal_optimal=0; Daniel@0: } Daniel@0: if((primal[i]<=(low[i]+epsilon_a)) || (primal[i]>=(up[i]-epsilon_a))) { Daniel@0: at_bound++; Daniel@0: } Daniel@0: /* printf("HIDEOtemp: a[%ld]=%.30f, dist=%.6f, b=%f, at_bound=%ld\n",i,primal[i],dist,model_b,at_bound); */ Daniel@0: } Daniel@0: if(m>0) { Daniel@0: eq=-ce0[0]; /* check precision of eq-constraint */ Daniel@0: for(i=0;i=(up[i]-epsilon_a)) { Daniel@0: primal[i]=up[i]; Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: isnantest=0; Daniel@0: for(i=0;i0) { Daniel@0: temp1=dual[n+n+1]; /* copy the dual variables for the eq */ Daniel@0: temp2=dual[n+n]; /* constraints to a handier location */ Daniel@0: for(i=n+n+1;i>=2;i--) { Daniel@0: dual[i]=dual[i-2]; Daniel@0: } Daniel@0: dual[0]=temp2; Daniel@0: dual[1]=temp1; Daniel@0: isnantest+=temp1+temp2; Daniel@0: } Daniel@0: Daniel@0: if(isnan(isnantest)) { Daniel@0: return((int)NAN_SOLUTION); Daniel@0: } Daniel@0: else if(primal_optimal) { Daniel@0: return((int)PRIMAL_OPTIMAL); Daniel@0: } Daniel@0: else if(maxviol == 0.0) { Daniel@0: return((int)DUAL_OPTIMAL); Daniel@0: } Daniel@0: else { Daniel@0: return((int)MAXITER_EXCEEDED); Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: Daniel@0: void linvert_matrix(matrix,depth,inverse,lindep_sensitivity,lin_dependent) Daniel@0: double *matrix; Daniel@0: long depth; Daniel@0: double *inverse,lindep_sensitivity; Daniel@0: long *lin_dependent; /* indicates the active parts of matrix on Daniel@0: input and output*/ Daniel@0: { Daniel@0: long i,j,k; Daniel@0: double factor; Daniel@0: Daniel@0: for(i=0;i=0;i--) { Daniel@0: if(!lin_dependent[i]) { Daniel@0: factor=1/matrix[i*depth+i]; Daniel@0: for(k=0;k=0;j--) { Daniel@0: factor=matrix[j*depth+i]; Daniel@0: matrix[j*depth+i]=0; Daniel@0: for(k=0;k