/[ascend]/trunk/solvers/ipopt/asc_ipopt.c
ViewVC logotype

Diff of /trunk/solvers/ipopt/asc_ipopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1736 by jpye, Sun Jan 6 03:42:21 2008 UTC revision 1737 by jpye, Thu Feb 7 23:58:06 2008 UTC
# Line 115  struct IpoptSystemStruct{ Line 115  struct IpoptSystemStruct{
115      Index n;                          /* number of variables */      Index n;                          /* number of variables */
116      Index m;                          /* number of constraints */      Index m;                          /* number of constraints */
117    
118      int nnzJ; /* number of non zeros in the jacobian of the constraints */      Index nnzJ; /* number of non zeros in the jacobian of the constraints */
119      int nnzH; /* number of non-zeros in the hessian of the objective */      Index nnzH; /* number of non-zeros in the hessian of the objective */
120    
121      Number* x_L;                  /* lower bounds on x */      Number* x_L;                  /* lower bounds on x */
122      Number* x_U;                  /* upper bounds on x */      Number* x_U;                  /* upper bounds on x */
# Line 231  static int ipopt_get_status(slv_system_t Line 231  static int ipopt_get_status(slv_system_t
231      return 0;      return 0;
232  }  }
233    
234    /**
235        Update the solver status. FIXME can't we get rid of this silly function
236        somehot?
237     */
238    static void update_status(IpoptSystem *sys){
239       boolean unsuccessful;
240    
241       sys->s.time_limit_exceeded = FALSE; /* can't do this one with IPOPT */
242       sys->s.iteration_limit_exceeded = FALSE; /* IPOPT handles this one internally */
243    
244       unsuccessful = sys->s.diverged || sys->s.inconsistent ||
245          sys->s.iteration_limit_exceeded || sys->s.time_limit_exceeded;
246    
247       sys->s.ready_to_solve = !unsuccessful && !sys->s.converged;
248       sys->s.ok = !unsuccessful && sys->s.calc_ok && !sys->s.struct_singular;
249    }
250    
251  static int32 ipopt_eligible_solver(slv_system_t server){  static int32 ipopt_eligible_solver(slv_system_t server){
252      UNUSED_PARAMETER(server);      UNUSED_PARAMETER(server);
253            
# Line 289  int32 ipopt_get_default_parameters(slv_s Line 306  int32 ipopt_get_default_parameters(slv_s
306              " 'acceptable_tol' as a second termination criterion. Note, some other"              " 'acceptable_tol' as a second termination criterion. Note, some other"
307              " algorithmic features also use this quantity to determine thresholds"              " algorithmic features also use this quantity to determine thresholds"
308              " etc."              " etc."
309          }, 1e-8, 0, 1e20}          }, 1.e-8, 0, 1.e20}
310      );      );
311    
312      slv_param_char(parameters,IPOPT_PARAM_MU_STRATEGY      slv_param_char(parameters,IPOPT_PARAM_MU_STRATEGY
# Line 373  static void ipopt_set_parameters(slv_sys Line 390  static void ipopt_set_parameters(slv_sys
390      @return 0 on success.      @return 0 on success.
391  */  */
392  int ipopt_update_model(IpoptSystem *sys, const double *x){  int ipopt_update_model(IpoptSystem *sys, const double *x){
393      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Not implemented");      unsigned j;
394      return 1; /* error */  
395        asc_assert(sys);
396        asc_assert(sys->vlist);
397    
398        /* FIXME do we need to update any other stuff? */
399        for(j = 0; j < sys->n; ++j){
400            var_set_value(sys->vlist[j], x[j]);
401        }
402    
403        return 0;
404  }  }
405    
406  /** Function to evaluate the objective function f(x).  /** Function to evaluate the objective function f(x).
# Line 390  Bool ipopt_eval_f(Index n, Number *x, Bo Line 416  Bool ipopt_eval_f(Index n, Number *x, Bo
416      sys = SYS(user_data);      sys = SYS(user_data);
417      int res;      int res;
418    
419        CONSOLE_DEBUG("ipopt_eval_f");
420    
421      asc_assert(n==sys->n);      asc_assert(n==sys->n);
422      asc_assert(sys->obj!=NULL);      asc_assert(sys->obj!=NULL);
423    
# Line 420  Bool ipopt_eval_grad_f(Index n, Number* Line 448  Bool ipopt_eval_grad_f(Index n, Number*
448          ,VAR_ACTIVE | VAR_INCIDENT | VAR_SVAR | VAR_FIXED          ,VAR_ACTIVE | VAR_INCIDENT | VAR_SVAR | VAR_FIXED
449      };      };
450    
451        CONSOLE_DEBUG("ipopt_eval_grad_f");
452    
453      asc_assert(n==sys->n);      asc_assert(n==sys->n);
454      asc_assert(sys->obj);      asc_assert(sys->obj);
455    
# Line 458  Bool ipopt_eval_g(Index n, Number* x, Bo Line 488  Bool ipopt_eval_g(Index n, Number* x, Bo
488      sys = SYS(user_data);      sys = SYS(user_data);
489      int i, res;      int i, res;
490    
491        CONSOLE_DEBUG("ipopt_eval_g");
492    
493      asc_assert(n==sys->n);      asc_assert(n==sys->n);
494      asc_assert(m==sys->m);      asc_assert(m==sys->m);
495    
# Line 481  Bool ipopt_eval_jac_g(Index n, Number* x Line 513  Bool ipopt_eval_jac_g(Index n, Number* x
513      sys = SYS(user_data);      sys = SYS(user_data);
514      int i,res;      int i,res;
515    
516        asc_assert(sys!=NULL);
517      asc_assert(n==sys->n);      asc_assert(n==sys->n);
518      asc_assert(nele_jac==sys->nnzJ);      asc_assert(nele_jac==sys->nnzJ);
519      asc_assert(m==sys->m);      asc_assert(m==sys->m);
# Line 505  Bool ipopt_eval_h(Index n, Number* x, Bo Line 538  Bool ipopt_eval_h(Index n, Number* x, Bo
538          , Index* jCol, Number* values          , Index* jCol, Number* values
539          , void *user_data          , void *user_data
540  ){  ){
541    
542        CONSOLE_DEBUG("ipopt_eval_h");
543    
544      if(iRow != NULL){      if(iRow != NULL){
545          asc_assert(jCol !=NULL);          asc_assert(jCol !=NULL);
546          asc_assert(x==NULL); asc_assert(lambda==NULL); asc_assert(values==NULL);          asc_assert(x==NULL); asc_assert(lambda==NULL); asc_assert(values==NULL);
# Line 556  static int ipopt_presolve(slv_system_t s Line 592  static int ipopt_presolve(slv_system_t s
592    
593      /** @TODO slv_sort_rels_and_vars(server,&(sys->m),&(sys->n)); */      /** @TODO slv_sort_rels_and_vars(server,&(sys->m),&(sys->n)); */
594    
595        /* TODO are there cases where these should be different? */
596        sys->n = sys->rtot;
597        sys->m = sys->vtot;
598    
599      /* set all relations as being 'unsatisfied' to start with... */      /* set all relations as being 'unsatisfied' to start with... */
600      for(i=0; i < sys->rtot; ++i){      for(i=0; i < sys->rtot; ++i){
601          rel_set_satisfied(sys->rlist[i],FALSE);          rel_set_satisfied(sys->rlist[i],FALSE);
# Line 571  static int ipopt_presolve(slv_system_t s Line 611  static int ipopt_presolve(slv_system_t s
611    
612      /* calculate nnz for hessian matrix @TODO FIXME */      /* calculate nnz for hessian matrix @TODO FIXME */
613    
614      sys->nnzH = relman_hessian_count(sys->rlist, sys->rtot, &(sys->vfilt), &(sys->rfilt), &max);      if(strcmp(SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_HESS_APPROX),"exact")==0){
615            sys->nnzH = relman_hessian_count(sys->rlist, sys->rtot, &(sys->vfilt), &(sys->rfilt), &max);
616        }else{
617            CONSOLE_DEBUG("Skipping relman_hessian_count as hessian method is not exact.");
618            sys->nnzH = sys->n * sys->m;
619        }
620    
621      /* need to provide sparsity structure for hessian matrix? */      /* need to provide sparsity structure for hessian matrix? */
622    
# Line 701  static int ipopt_presolve(slv_system_t s Line 746  static int ipopt_presolve(slv_system_t s
746      sys->s.block.iteration = 0;      sys->s.block.iteration = 0;
747      sys->obj_val =  MAXDOUBLE/2000.0;      sys->obj_val =  MAXDOUBLE/2000.0;
748    
749        update_status(sys);
750    
751      ipopt_iteration_ends(sys);      ipopt_iteration_ends(sys);
752    
753      CONSOLE_DEBUG("Reset status");      CONSOLE_DEBUG("Reset status");
754    
755      /* sys->s.cost[sys->s.block.number_of].time=sys->s.cpu_elapsed; */      /* sys->s.cost[sys->s.block.number_of].time=sys->s.cpu_elapsed; */
756    
757      ERROR_REPORTER_HERE(ASC_PROG_ERR,"presolve completed");      ERROR_REPORTER_HERE(ASC_USER_SUCCESS,"presolve completed");
758      return 0;      return 0;
759  }  }
760    
# Line 723  static int ipopt_solve(slv_system_t serv Line 770  static int ipopt_solve(slv_system_t serv
770    
771      double *x, *x_L, *x_U, *g_L, *g_U, *mult_x_L, *mult_x_U;      double *x, *x_L, *x_U, *g_L, *g_U, *mult_x_L, *mult_x_U;
772    
773      CONSOLE_DEBUG("SOLVING...");      CONSOLE_DEBUG("SOLVING, sys->n = %d...",sys->n);
774        asc_assert(sys->n!=-1);
775    
776      /* set the number of variables and allocate space for the bounds */      /* set the number of variables and allocate space for the bounds */
777      x_L = ASC_NEW_ARRAY(Number,sys->n);      x_L = ASC_NEW_ARRAY(Number,sys->n);
778      x_U = ASC_NEW_ARRAY(Number,sys->n);      x_U = ASC_NEW_ARRAY(Number,sys->n);
779    
780        CONSOLE_DEBUG("SETTING BOUNDS...");
781    
782      /* @TODO set the values for the variable bounds */      /* @TODO set the values for the variable bounds */
783      int jj = 0;      int jj = 0;
784      for(j = 0; j < sys->vtot; j++){      for(j = 0; j < sys->vtot; j++){
785            CONSOLE_DEBUG("j = %d, vtot = %d, vlist = %p",j,sys->vtot,sys->vlist);
786          var = sys->vlist[j];          var = sys->vlist[j];
787          if(var_apply_filter(var,&(sys->vfilt))){          if(var_apply_filter(var,&(sys->vfilt))){
788                CONSOLE_DEBUG("setting x_L[%d]",jj);
789              x_L[jj] = var_lower_bound(var);              x_L[jj] = var_lower_bound(var);
790              x_U[jj] = var_upper_bound(var);              x_U[jj] = var_upper_bound(var);
791              jj++;              jj++;
# Line 744  static int ipopt_solve(slv_system_t serv Line 796  static int ipopt_solve(slv_system_t serv
796      /* need to identify equations that share the same non-constant parts? */      /* need to identify equations that share the same non-constant parts? */
797      /* then find the constant parts and make then g_L or g_U accordingly */      /* then find the constant parts and make then g_L or g_U accordingly */
798      /* what to do about other bounds? */      /* what to do about other bounds? */
799        /* set the number of variables and allocate space for the bounds */
800        g_L = ASC_NEW_ARRAY(Number,sys->m);
801        g_U = ASC_NEW_ARRAY(Number,sys->m);
802        for(j = 0; j < sys->m; j++){
803            g_L[j] = 0;
804            g_U[j] = 0;
805        }
806    
807        CONSOLE_DEBUG("CREATING PROBLEM...");
808    
809      /* create the IpoptProblem */      /* create the IpoptProblem */
810        CONSOLE_DEBUG("n = %d, m = %d, nnzJ = %d, nnzH = %d",sys->n, sys->m, sys->nnzJ, sys->nnzH);
811      sys->nlp = CreateIpoptProblem(sys->n, x_L, x_U, sys->m, g_L, g_U, sys->nnzJ, sys->nnzH, 0/*index style=C*/,      sys->nlp = CreateIpoptProblem(sys->n, x_L, x_U, sys->m, g_L, g_U, sys->nnzJ, sys->nnzH, 0/*index style=C*/,
812          &ipopt_eval_f, &ipopt_eval_g, &ipopt_eval_grad_f,          &ipopt_eval_f, &ipopt_eval_g, &ipopt_eval_grad_f,
813          &ipopt_eval_jac_g, &ipopt_eval_h          &ipopt_eval_jac_g, &ipopt_eval_h
814      );      );
815    
816        CONSOLE_DEBUG("FREEING INTERNAL STUFF");
817        
818      /* We can free the memory now - the values for the bounds have been      /* We can free the memory now - the values for the bounds have been
819      copied internally in CreateIpoptProblem */      copied internally in CreateIpoptProblem */
820    #if 0
821        /* freeing this stuff seems to cause a crash...?!?!? */
822      ASC_FREE(x_L);      ASC_FREE(x_L);
823      ASC_FREE(x_U);      ASC_FREE(x_U);
824      ASC_FREE(g_L);      ASC_FREE(g_L);
825      ASC_FREE(g_U);      ASC_FREE(g_U);
826    #endif
827    
828        CONSOLE_DEBUG("SETTING OPTIONS...");
829      /* set some options */      /* set some options */
830      AddIpoptNumOption(sys->nlp, "tol", SLV_PARAM_BOOL(&(sys->p),IPOPT_PARAM_TOL));      AddIpoptNumOption(sys->nlp, "tol", SLV_PARAM_REAL(&(sys->p),IPOPT_PARAM_TOL));
831      AddIpoptStrOption(sys->nlp, "mu_strategy", SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_MU_STRATEGY));      AddIpoptStrOption(sys->nlp, "mu_strategy", SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_MU_STRATEGY));
832      AddIpoptStrOption(sys->nlp, "derivative_test", SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_DERIVATIVE_TEST));      AddIpoptStrOption(sys->nlp, "derivative_test", SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_DERIVATIVE_TEST));
833      AddIpoptStrOption(sys->nlp, "hessian_approximation", SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_HESS_APPROX));      AddIpoptStrOption(sys->nlp, "hessian_approximation", SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_HESS_APPROX));
834    
835        CONSOLE_DEBUG("Hessian method: %s",SLV_PARAM_CHAR(&(sys->p),IPOPT_PARAM_HESS_APPROX));
836    
837      /* initial values */      /* initial values */
838      x = ASC_NEW_ARRAY(Number, sys->n);      x = ASC_NEW_ARRAY(Number, sys->n);
839    
# Line 773  static int ipopt_solve(slv_system_t serv Line 843  static int ipopt_solve(slv_system_t serv
843      mult_x_L = ASC_NEW_ARRAY(Number, sys->n);      mult_x_L = ASC_NEW_ARRAY(Number, sys->n);
844      mult_x_U = ASC_NEW_ARRAY(Number, sys->n);      mult_x_U = ASC_NEW_ARRAY(Number, sys->n);
845    
846        CONSOLE_DEBUG("Calling IpoptSolve...");
847    
848      /* solve the problem */      /* solve the problem */
849      status = IpoptSolve(sys->nlp, x, NULL, &sys->obj_val, NULL, mult_x_L, mult_x_U, NULL);      status = IpoptSolve(sys->nlp, x, NULL, &sys->obj_val, NULL, mult_x_L, mult_x_U, (void*)sys);
850    
851        CONSOLE_DEBUG("Done IpoptSolve...");
852    
853      /** @TODO update the sys->s.xxxxx flags based on value of 'status' */      /** @TODO update the sys->s.xxxxx flags based on value of 'status' */
854    
# Line 836  static void ipopt_iteration_ends(IpoptSy Line 910  static void ipopt_iteration_ends(IpoptSy
910    
911    
912  static int ipopt_iterate(slv_system_t server, SlvClientToken asys){  static int ipopt_iterate(slv_system_t server, SlvClientToken asys){
913      UNUSED_PARAMETER(server);      CONSOLE_DEBUG("ipopt_iterate about to call ipopt_solve...");
914      CONSOLE_DEBUG("ITERATING...");      return ipopt_solve(server,asys);
     ERROR_REPORTER_HERE(ASC_PROG_ERR,"Not implemented");  
     return 1;  
915  }  }
916    
917  static int ipopt_resolve(slv_system_t server, SlvClientToken asys){  static int ipopt_resolve(slv_system_t server, SlvClientToken asys){

Legend:
Removed from v.1736  
changed lines
  Added in v.1737

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22