123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281type[@warning"-37"]nlopt_algorithm=|NLOPT_GN_DIRECT|NLOPT_GN_DIRECT_L|NLOPT_GN_DIRECT_L_RAND|NLOPT_GN_DIRECT_NOSCAL|NLOPT_GN_DIRECT_L_NOSCAL|NLOPT_GN_DIRECT_L_RAND_NOSCAL|NLOPT_GN_ORIG_DIRECT|NLOPT_GN_ORIG_DIRECT_L|NLOPT_GD_STOGO|NLOPT_GD_STOGO_RAND|NLOPT_LD_LBFGS_NOCEDAL|NLOPT_LD_LBFGS|NLOPT_LN_PRAXIS|NLOPT_LD_VAR1|NLOPT_LD_VAR2|NLOPT_LD_TNEWTON|NLOPT_LD_TNEWTON_RESTART|NLOPT_LD_TNEWTON_PRECOND|NLOPT_LD_TNEWTON_PRECOND_RESTART|NLOPT_GN_CRS2_LM|NLOPT_GN_MLSL|NLOPT_GD_MLSL|NLOPT_GN_MLSL_LDS|NLOPT_GD_MLSL_LDS|NLOPT_LD_MMA|NLOPT_LN_COBYLA|NLOPT_LN_NEWUOA|NLOPT_LN_NEWUOA_BOUND|NLOPT_LN_NELDERMEAD|NLOPT_LN_SBPLX|NLOPT_LN_AUGLAG|NLOPT_LD_AUGLAG|NLOPT_LN_AUGLAG_EQ|NLOPT_LD_AUGLAG_EQ|NLOPT_LN_BOBYQA|NLOPT_GN_ISRES|NLOPT_AUGLAG|NLOPT_AUGLAG_EQ|NLOPT_G_MLSL|NLOPT_G_MLSL_LDS|NLOPT_LD_SLSQPtype'aalgorithm=nlopt_algorithmletdirect=NLOPT_GN_DIRECTletdirect_l=NLOPT_GN_DIRECT_Lletdirect_l_rand=NLOPT_GN_DIRECT_L_RANDletdirect_noscal=NLOPT_GN_DIRECT_NOSCALletdirect_l_noscal=NLOPT_GN_DIRECT_L_NOSCALletdirect_l_rand_noscal=NLOPT_GN_DIRECT_L_RAND_NOSCALletorig_direct=NLOPT_GN_ORIG_DIRECTletorig_direct_l=NLOPT_GN_ORIG_DIRECT_Lletstogo=NLOPT_GD_STOGOletstogo_rand=NLOPT_GD_STOGO_RANDletlbfgs_nocedal=NLOPT_LD_LBFGS_NOCEDALletlbfgs=NLOPT_LD_LBFGSletpraxis=NLOPT_LN_PRAXISletvar1=NLOPT_LD_VAR1letvar2=NLOPT_LD_VAR2lettnewton=NLOPT_LD_TNEWTONlettnewton_restart=NLOPT_LD_TNEWTON_RESTARTlettnewton_precond=NLOPT_LD_TNEWTON_PRECONDlettnewton_precond_restart=NLOPT_LD_TNEWTON_PRECOND_RESTARTletcrs2_lm=NLOPT_GN_CRS2_LMletmma=NLOPT_LD_MMAletcobyla=NLOPT_LN_COBYLAletnewuoa=NLOPT_LN_NEWUOAletnewuoa_bound=NLOPT_LN_NEWUOA_BOUNDletneldermead=NLOPT_LN_NELDERMEADletsbplx=NLOPT_LN_SBPLXletbobyqa=NLOPT_LN_BOBYQAletisres=NLOPT_GN_ISRESletauglag=NLOPT_AUGLAGletauglag_eq=NLOPT_AUGLAG_EQletmlsl=NLOPT_G_MLSLletmlsl_lds=NLOPT_G_MLSL_LDSletslsqp=NLOPT_LD_SLSQPtype'attype[@warning"-37"]result=|Failure_res|Invalid_args_res|Out_of_memory_res|Roundoff_limited_res|Forced_stop_res|Success|Stopval_reached|Ftol_reached|Xtol_reached|Maxeval_reached|Maxtime_reachedexceptionRoundoff_limitedexceptionForced_stopletmap_result=function|Failure_res->raise(Failure"NLOPT_FAILURE")|Invalid_args_res->raise(Invalid_argument"NLOPT_INVALID_ARGS")|Out_of_memory_res->raiseOut_of_memory|Roundoff_limited_res->raiseRoundoff_limited|Forced_stop_res->raiseForced_stop|Success->`Success|Stopval_reached->`Stopval_reached|Ftol_reached->`Ftol_reached|Xtol_reached->`Xtol_reached|Maxeval_reached->`Maxeval_reached|Maxtime_reached->`Maxtime_reached;;letstring_of_result=function|`Success->"NLOPT_SUCCESS"|`Stopval_reached->"NLOPT_STOPVAL_REACHED"|`Ftol_reached->"NLOPT_FTOL_REACHED"|`Xtol_reached->"NLOPT_XTOL_REACHED"|`Maxeval_reached->"NLOPT_MAXEVAL_REACHED"|`Maxtime_reached->"NLOPT_MAXTIME_REACHED";;externalcreate:'aalgorithm->int->'at="ml_nlopt_create"externalget_dimension:'at->int="ml_nlopt_get_dimension"externalml_set_min_objective:'at->(floatarray->(floatarray)option->float)->result="ml_nlopt_set_min_objective"letset_min_objectiveoptf=let_=map_result(ml_set_min_objectiveoptf)in();;externalml_set_max_objective:'at->(floatarray->(floatarray)option->float)->result="ml_nlopt_set_max_objective"letset_max_objectiveoptf=let_=map_result(ml_set_max_objectiveoptf)in();;externalml_optimize:'at->floatarray->(result*floatarray*float)="ml_nlopt_optimize"letoptimizeoptx=ifget_dimensionopt<>Array.lengthxthenraise(Invalid_argument"Nlopt.optimize: dimension of initial guess different from algorithm dimension")elselet(result,xopt,fopt)=ml_optimizeoptxin(map_resultresult,xopt,fopt);;(* Constraints *)externalml_set_lower_bounds:'at->floatarray->result="ml_nlopt_set_lower_bounds"letset_lower_boundsoptlb=ifget_dimensionopt<>Array.lengthlbthenraise(Invalid_argument"Nlopt.set_lower_bounds: dimension of bounds different from algorithm dimension")elselet_=map_result(ml_set_lower_boundsoptlb)in()externalml_get_lower_bounds:'at->floatarray->result="ml_nlopt_get_lower_bounds"letget_lower_boundsopt=letlb=Array.make(get_dimensionopt)naninlet_=map_result(ml_get_lower_boundsoptlb)inlb;;externalml_set_upper_bounds:'at->floatarray->result="ml_nlopt_set_upper_bounds"letset_upper_boundsoptub=ifget_dimensionopt<>Array.lengthubthenraise(Invalid_argument"Nlopt.set_upper_bounds: dimension of bounds different from algorithm dimension")elselet_=map_result(ml_set_upper_boundsoptub)in()externalml_get_upper_bounds:'at->floatarray->result="ml_nlopt_get_upper_bounds"letget_upper_boundsopt=letub=Array.make(get_dimensionopt)naninlet_=map_result(ml_get_upper_boundsoptub)inub;;externalml_add_inequality_constraint:'at->(floatarray->(floatarray)option->float)->float->result="ml_nlopt_add_inequality_constraint"letadd_inequality_constraintoptfconstrtol=let_=map_result(ml_add_inequality_constraintoptfconstrtol)in();;externalml_add_equality_constraint:'at->(floatarray->(floatarray)option->float)->float->result="ml_nlopt_add_equality_constraint"letadd_equality_constraintoptfconstrtol=let_=map_result(ml_add_equality_constraintoptfconstrtol)in();;(* Stopping criteria *)externalml_set_stopval:'at->float->result="ml_nlopt_set_stopval"letset_stopvaloptx=let_=map_result(ml_set_stopvaloptx)in();;externalget_stopval:'at->float="ml_nlopt_get_stopval"externalml_set_ftol_rel:'at->float->result="ml_nlopt_set_ftol_rel"letset_ftol_relopttol=let_=map_result(ml_set_ftol_relopttol)in();;externalget_ftol_rel:'at->float="ml_nlopt_get_ftol_rel"externalml_set_ftol_abs:'at->float->result="ml_nlopt_set_ftol_abs"letset_ftol_absopttol=let_=map_result(ml_set_ftol_absopttol)in();;externalget_ftol_abs:'at->float="ml_nlopt_get_ftol_abs"externalml_set_xtol_rel:'at->float->result="ml_nlopt_set_xtol_rel"letset_xtol_relopttol=let_=map_result(ml_set_xtol_relopttol)in();;externalget_xtol_rel:'at->float="ml_nlopt_get_xtol_rel"externalml_set_xtol_abs:'at->floatarray->result="ml_nlopt_set_xtol_abs"letset_xtol_absopttol=let_=map_result(ml_set_xtol_absopttol)in();;externalml_get_xtol_abs:'at->floatarray->result="ml_nlopt_get_xtol_abs"letget_xtol_absopt=lettol=Array.make(get_dimensionopt)naninlet_=map_result(ml_get_xtol_absopttol)intol;;externalml_set_maxeval:'at->int->result="ml_nlopt_set_maxeval"letset_maxevaloptn=let_=map_result(ml_set_maxevaloptn)in();;externalget_maxeval:'at->int="ml_nlopt_get_maxeval"externalml_set_maxtime:'at->float->result="ml_nlopt_set_maxtime"letset_maxtimeoptt=let_=map_result(ml_set_maxtimeoptt)in();;externalget_maxtime:'at->float="ml_nlopt_get_maxtime"externalml_force_stop:'at->result="ml_nlopt_force_stop"letforce_stopopt=let_=map_result(ml_force_stopopt)in();;(* Local/subsidiary optimization algorithm *)externalml_set_local_optimizer:[>`Subsidiary]t->'at->result="ml_nlopt_set_local_optimizer"letset_local_optimizeroptlocal_opt=let_=map_result(ml_set_local_optimizeroptlocal_opt)in();;(* Initial step size *)externalml_set_initial_step:'at->floatarray->result="ml_nlopt_set_initial_step"letset_initial_stepoptdx=ifget_dimensionopt<>Array.lengthdxthenraise(Invalid_argument"Nlopt.set_initial_step: dimension of initial step different from algorithm dimension")elselet_=map_result(ml_set_initial_stepoptdx)in();;externalml_get_initial_step:'at->floatarray->floatarray->result="ml_nlopt_get_initial_step"letget_initial_stepoptx=ifget_dimensionopt<>Array.lengthxthenraise(Invalid_argument"Nlopt.get_initial_step: dimension of initial step different from algorithm dimension")elseletdx=Array.make(get_dimensionopt)naninlet_=map_result(ml_get_initial_stepoptxdx)indx;;(* Stochastic population *)externalml_set_population:'at->int->result="ml_nlopt_set_population"letset_populationoptpop=ifpop<0thenraise(Invalid_argument"Nlopt.set_population: population negative")elselet_=map_result(ml_set_populationoptpop)in();;(* Vector storage for limited-memory quasi-Newton algorithms *)externalml_set_vector_storage:'at->int->result="ml_nlopt_set_vector_storage"letset_vector_storageoptm=ifm<0thenraise(Invalid_argument"Nlopt.set_vector_storage: number of stored vectors negative")elselet_=map_result(ml_set_vector_storageoptm)in();;externalget_vector_storage:'at->int="ml_nlopt_get_vector_storage"(* Version *)externalversion:unit->int*int*int="ml_nlopt_version"