Methods for Optimizing program C + +

Source: Internet
Author: User

The problem solved

The general form of the optimization problem
mINF (PaRas) s.T.PaRas∈[PaRas_LoWeR,PaRas_uPPeR]
This is the general form of mathematics, when the maximum value of the function is only added a minus sign can be
The problems we face in the process are generally discrete, i.e. F(paRas) In general, it is about X i of a discrete variable. At this point, the problem is translated into

       errorfunc(valueType* paras,valueType* X)//误差函数输入要求的参数和具体的X的值因为多个以数组的形式传值。       {               sum(f(paras,X_i))//求得函数关于每个X_i的函数和       }

We adjust the required parameter paras to minimize the value returned by the Errorfunc.

  exp=min_value//收敛的最小值  while(errorfunc( paras, X)>exp)//当误差函数的变化大于最小值时候  {        adjust(paras);//调整参数,继续判断其收敛与否  }
The solution is when the problem is linear.

Ax=b
Using SVD decomposition can be obtained the final solution, this method is relatively simple, there are a lot of code on the Internet, the next opportunity to fill in.

When the problem is nonlinear

If we don't adopt the method of function library, we can only analyze the concrete problem concretely.
Here we use the GNU GSL function library
[ ]
[Specific configuration Method]
Here's our function code.

voidMY_DF (ConstGsl_vector *params,void*xandcval)//paras is a value that needs to be passed in, can be x and some fixed parameters, or it can be used as a global variable, para is the last parameter we need to adjust.{Doublex, y;Double*xandcval = (Double*) Xandcval;//x = Gsl_vector_get (params,0);//will be obtained paras read outy = Gsl_vector_get (params,1);returnErrorfunc (...);}voidOptim1 () {size_t NP = -;size of the//paras    Doublexandcval[2] = {1,2};//Here assume only parameters, when there are many X times need to construct    ConstGsl_multimin_fminimizer_type *t_min = Gsl_multimin_fminimizer_nmsimplex;    Gsl_multimin_fminimizer *s = NULL;    Gsl_vector *ss, *x;    Gsl_multimin_function Minex_func; size_t iter =0IintStatusDoubleSize/ * Initial vertex size vector * /SS = Gsl_vector_alloc (NP);/ * Set All step sizes to 1 * /Gsl_vector_set_all (SS,0.00001);//The 0.00001 here refers to exp    / * Starting point * /Paras = Gsl_vector_alloc (NP);//Give initial value to the desired parameter 123Gsl_vector_set (Paras,0, A);//Parameter table, ordinal, valueGsl_vector_set (Paras,1,123);/ * Initialize method and Iterate * /MINEX_FUNC.F = &my_f1;//The name of the error functionMINEX_FUNC.N = NP; Minex_func.params= (void*) &par;Charszbuffer[ +];    s = Gsl_multimin_fminimizer_alloc (T_min, NP); Gsl_multimin_fminimizer_set (S, &minex_func, X, SS); Do{iter++; Status = Gsl_multimin_fminimizer_iterate (s);if(status) Break;        Size = Gsl_multimin_fminimizer_size (s); Status = Gsl_multimin_test_size (size,1e-12);if(Status = = Gsl_success)//Convergence{printf ("f () =%12.11f size =%.3f\n", s->fval, size);        ; }//printf ("%5d", ITER);         for(i =0; I < NP; i++) {;//printf ("%10.3e", Gsl_vector_get (S->x, i));} optim1<<"f () ="<<s->fval<<"Size="<<size<<"\ n";//sprintf (Szbuffer, "f () =%12.11f size =%.3f\n", s->fval, size);        //messagebox (NULL, (tchar*) szbuffer,text ("Length optimization"), MB_OK);        //printf ("f () =%12.11f size =%.3f\n", s->fval, size);fcf<<"fc[0]="<<fc[0]<<"; \ t"<<"fc[1]="<<fc[1]<<"; \ t"<<"cc[0]="<<cc[0]<<"; \ t"<<"cc[1]="<<cc[1]<<"; \ n"; } while(Status = = Gsl_continue && iter <15622);//Set the maximum number of iterations here, 15622    ///x = Gsl_vector_get (S->paras,0);//Pass the function value backy = Gsl_vector_get (S->paras,1);//Gsl_vector_free (x);    Gsl_vector_free (ss); Gsl_multimin_fminimizer_free (s);}

There may be some details in the above error, a hasty excuse to see their official documents ....

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Methods for Optimizing program C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.