MINUIT 在 local minimum 面前束手无策

设计一个0-1之间的有很多局域最小值的快速震荡函数
FVAL = cos(100*3.1415926535*xval(1))/(1+scale*(xval(1)-0.55)**2)
scale是一个放大参数,scale越大,最小值与其它极值点差异越大

代码:

program main

	implicit none 
	integer,parameter	:: NER=kind(0.0d0)
	integer		:: NPAR=1,IFLAG
	real(NER)	:: XVAL(1)=0.500000,GRAD(1)=0 
	real(NER)	:: FVAL
	integer		:: errflag,ivarbl
	real		:: argmn(5)=0
	integer		:: nn
	real(NER)	:: step=0.001_NER
	real(NER)	:: limit=0.0_NER,limit1=0.0,limit2=1.0
	character(len=5) 	:: paraname(1)

	external futil
	external fcn
	external MNINIT, MNEXCM, MNPARM, MNSETI, MNPOUT

	call fcn(NPAR, GRAD, FVAL, XVAL, IFLAG, FUTIL)

	paraname(1)= "para1"

	call MNINIT (5, 6, 7)
	call MNSETI ('FCN')
    argmn(1) = -1.0_NER
    call MNEXCM (FCN, 'SET PRINTOUT', argmn, 1, errflag, FUTIL)

	CALL MNPARM(1,paraname(1),xval(1),step,limit1,limit2,errflag)

	argmn(1) = 2.
    call MNEXCM (FCN, 'SET STRATEGY', argmn, 1, errflag, FUTIL)


    argmn(1) = 100000.
    argmn(2) = 0.001
    call MNEXCM(fcn, 'migrad',argmn,2,errflag,futil)



    argmn(1) = 1000000.
    call MNEXCM(fcn, 'improve',argmn,1,errflag,futil)

    
end program

function FUTIL()
	integer futil
	futil = 1
	write(*,*) "hello world"
end function

subroutine fcn(NPAR, GRAD, FVAL, XVAL, IFLAG, FUTIL)

	integer,parameter		:: NER=kind(0.0D0)
	integer, intent(in)     :: NPAR, IFLAG
	real(NER), intent(in)   :: XVAL(1:NPAR)
	real(NER), intent(out)  :: FVAL
	real(NER), intent(out)  :: GRAD(1:NPAR)
	integer					:: scale = 20000

	external :: FUTIL

	if(xval(1)<=1.0 .and. xval(1)>0.0) then 
		FVAL = cos(100*3.1415926535*xval(1))/(1+scale*(xval(1)-0.55)**2)
	else 
		fval =1
	end if

	print*,"XVAL: ",xval(1),"  FVAL: ",fval


end subroutine

运行结果如下,成功找到最小值点0.55

XVAL:   0.50000000000000000        FVAL:    1.9607833970753514E-002
 **********
 **    1 **SET PRINTOUT  0.1587-313
 **********

 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 'para1     '   0.50000      0.10000E-02     0.0000       1.0000    
 **********
 **    2 **SET STRATEGY  0.5305-314
 **********
 **********
 **    3 **MIGRAD  0.7703E-26   0.000    
 **********
 XVAL:   0.50000000000000000        FVAL:    1.9607833970753514E-002
 XVAL:   0.50001000000696405        FVAL:    1.9615428588068692E-002
 XVAL:   0.49998999999303595        FVAL:    1.9600050324587299E-002
 XVAL:   0.31247240058968401        FVAL:   -6.3150158445647933E-004
 XVAL:   0.38955528565784270        FVAL:   -1.9196591888349791E-003
 XVAL:   0.35752386430665556        FVAL:    9.6016976640166780E-004
 XVAL:   0.99586100702573488        FVAL:    6.7192160995500531E-005
 XVAL:   0.78999910445346644        FVAL:   -8.6730920852707812E-004
 XVAL:   0.56478195138379472        FVAL:    1.2745559692366321E-002
 XVAL:   0.47680903093615801        FVAL:    4.9771636683686926E-003
 XVAL:   0.43292163144366919        FVAL:   -2.2079883619285450E-003
 XVAL:   0.46721284268659191        FVAL:   -4.6390212924105247E-003
 XVAL:   0.46731262818000863        FVAL:   -4.8226003476060314E-003
 XVAL:   0.46711305850466328        FVAL:   -4.4517603069725272E-003
 XVAL:   0.46724883567244496        FVAL:   -4.7056741382685008E-003
 XVAL:   0.46717684987137420        FVAL:   -4.5718893726272736E-003
 XVAL:   0.49038385354493741        FVAL:   -1.3772385763266325E-002
 XVAL:   0.48191657713136388        FVAL:    8.7948583902536168E-003
 XVAL:   0.60555290644860427        FVAL:   -2.7555036501283078E-003
 XVAL:   0.53481623094724329        FVAL:   -1.0283036798199207E-002
 XVAL:   0.50699213850467006        FVAL:   -1.5418088047477192E-002
 XVAL:   0.50702839753009599        FVAL:   -1.5685815259360688E-002
 XVAL:   0.50695587944246623        FVAL:   -1.5149219603553915E-002
 XVAL:   0.50701575615666994        FVAL:   -1.5592606588012009E-002
 XVAL:   0.50696852083706656        FVAL:   -1.5243085317500382E-002
 MINUIT WARNING IN MIGRAD  
 ============== Negative diagonal element  1 in Error Matrix
 MINUIT WARNING IN MIGRAD  
 ==============     0.11E+01 added to diagonal of error matrix
 XVAL:   0.97870485833656828        FVAL:    2.4977501126080326E-004
 XVAL:   0.90281237195628938        FVAL:    2.5473291298494033E-004
 XVAL:   0.72789003164292043        FVAL:   -1.2434735250361514E-003
 XVAL:   0.61968485287339115        FVAL:    1.0141743611477631E-002
 XVAL:   0.56208823619760873        FVAL:   0.20201709331547443     
 XVAL:   0.52501791104384554        FVAL:   -4.1758890352707454E-004
 XVAL:   0.51509679970504163        FVAL:    1.1988662909358631E-003
 XVAL:   0.51016569801979084        FVAL:   -3.0506537093079350E-002
 XVAL:   0.51018948789178897        FVAL:   -3.0529145878207011E-002
 XVAL:   0.51014190812476978        FVAL:   -3.0482257461208851E-002
 XVAL:   0.51017859934475052        FVAL:   -3.0519005598981710E-002
 XVAL:   0.51015279668806024        FVAL:   -3.0493577202016911E-002
 XVAL:   0.51065320721730667        FVAL:   -3.0629305407304800E-002
 XVAL:   0.51049312011095083        FVAL:   -3.0668830946729807E-002
 XVAL:   0.51050602225930919        FVAL:   -3.0668571609290703E-002
 XVAL:   0.51048021795560250        FVAL:   -3.0668580067472317E-002

 MIGRAD MINIMIZATION HAS CONVERGED.

 FCN= -0.3066883E-01 FROM MIGRAD    STATUS=CONVERGED     40 CALLS       41 TOTAL
                     EDM=  0.52E-08    STRATEGY= 0      NO ERROR MATRIX       

  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME        VALUE          ERROR          SIZE      DERIVATIVE 
   1    para1      0.51049       0.10000E-02   0.65498E-03   0.16385E-03
 **********
 **    4 **IMPROVE  0.7703E-26
 **********
 XVAL:   0.51049312011095083        FVAL:   -3.0668830946729807E-002
 XVAL:   0.51050602225930919        FVAL:   -3.0668571609290703E-002
 XVAL:   0.51048021795560250        FVAL:   -3.0668580067472317E-002

 START ATTEMPT NO. 1 TO FIND NEW MINIMUM
 XVAL:   0.49670703728104204        FVAL:    8.8394581313809475E-003
 XVAL:   0.50483535344822172        FVAL:    1.2369141868642239E-003
 XVAL:   0.48857959137384666        FVAL:   -1.1799703623788189E-002
 XVAL:   0.48045516363766605        FVAL:    1.0127864865538478E-002
 XVAL:   0.46422394976684306        FVAL:    1.6293725107443173E-003
 XVAL:   0.44803055279286996        FVAL:   -3.8985493829021595E-003
 XVAL:   0.41582562006824370        FVAL:    7.1036207309076453E-004
 XVAL:   0.38397649773883230        FVAL:    5.7222903598666596E-004
 XVAL:   0.32188212411925721        FVAL:    7.9694622910272278E-004
 XVAL:   0.26279622971041716        FVAL:    3.8670089826211949E-004
 XVAL:   0.15757414147132193        FVAL:    2.3479172797228102E-004
 XVAL:    7.5389180375027676E-002   FVAL:    2.7065629817364869E-005
 XVAL:    3.2519474849773689E-004   FVAL:    1.6459478936350073E-004
 XVAL:    5.7464619599499833E-002   FVAL:    1.4408192179233005E-004
 XVAL:   0.47690379671620392        FVAL:    5.2203402377533027E-003
 XVAL:   0.91916926580119762        FVAL:    3.5432265691915965E-004
 XVAL:   0.53322051510158308        FVAL:   -7.9983248842938004E-002
 XVAL:    4.8391212093156999E-002   FVAL:   -1.7383943161937128E-004
 XVAL:   0.55342398818300764        FVAL:  -0.38488774603948750     
 XVAL:   0.89570239925297757        FVAL:    9.1537159974782785E-005
 XVAL:   0.59353418304050742        FVAL:   -1.1422666986878421E-002
 XVAL:    1.9657690306870756E-002   FVAL:    1.7671121849950990E-004
 XVAL:   0.67164909955098062        FVAL:   -2.9254458381214217E-003
 XVAL:   0.77760069827685485        FVAL:    7.0307884988022706E-004
 XVAL:   0.81241932008156303        FVAL:   -5.2587147791436294E-004
 XVAL:    3.3414235885206311E-002   FVAL:   -8.9511073503593448E-005
 XVAL:   0.98484049562592713        FVAL:    1.3239932830549670E-005
 XVAL:   0.17431477179515725        FVAL:   -7.5646958754691686E-005
 XVAL:   0.74829961782830723        FVAL:   -1.0929938864204991E-003
 XVAL:   0.99210470936737183        FVAL:   -2.0184585111435944E-004
 XVAL:    6.2537632077579342E-002   FVAL:    1.4698927296743087E-004
 XVAL:   0.68630447494081448        FVAL:   -1.0694170825791163E-003
 XVAL:    8.3335147765358331E-002   FVAL:    1.1465702510367926E-004
 XVAL:   0.14497635213104126        FVAL:    2.2634187451940946E-006
 XVAL:   0.96553083382499683        FVAL:   -4.8055819585107544E-005
 XVAL:    2.9344432382344410E-004   FVAL:    1.6473588537365625E-004
 XVAL:   0.16991646946296318        FVAL:   -3.4586932457298817E-004
 XVAL:   0.54715576626680396        FVAL:  -0.53938783279209379     
 AN IMPROVEMENT ON THE PREVIOUS MINIMUM HAS BEEN FOUND
 XVAL:   0.54715576626680396        FVAL:  -0.53938783279209379     
 START SIMPLEX MINIMIZATION.    CONVERGENCE WHEN EDM .LT.  0.10E+00
 XVAL:   0.52157141090689907        FVAL:    5.1305740783478090E-002
 XVAL:   0.55735853415746761        FVAL:   0.32405639078885340     
 XVAL:   0.54306885969492669        FVAL:   0.29076907538128022     
 XVAL:   0.55123949644654313        FVAL:  -0.89755730531967570     
 XVAL:   0.55531977515583542        FVAL:    6.4045234013332911E-002
 XVAL:   0.55531977515583542        FVAL:    6.4045234013332911E-002
 XVAL:   0.54919804560331698        FVAL:  -0.95613236012436187     
 XVAL:   0.54715576626861484        FVAL:  -0.53938783326935613     
 XVAL:   0.55021887673599956        FVAL:  -0.99668173493936607     
 SIMPLEX MINIMIZATION HAS CONVERGED.
 XVAL:   0.55021887673509462        FVAL:  -0.99668173496676893     

 FCN= -0.9966817     FROM SIMPLEX   STATUS=PROGRESS      10 CALLS       93 TOTAL
                     EDM=  0.27E-10    STRATEGY= 0      ERROR MATRIX ACCURATE 

  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME        VALUE          ERROR          SIZE      DERIVATIVE 
   1    para1      0.55022       0.25534E-01   0.18190E-11   0.32640E+07

 IMPROVE HAS FOUND A TRULY NEW MINIMUM
 *************************************

 XVAL:   0.54819664405508539        FVAL:  -0.79223514088950409     
 XVAL:   0.55224027991553515        FVAL:  -0.69283908038410313     
 XVAL:   0.54227038041339182        FVAL:   0.34453274458608346     
 XVAL:   0.54971424097312460        FVAL:  -0.99434925246263506     
 MIGRAD FAILS WITH STRATEGY=0.   WILL TRY WITH STRATEGY=1.
 XVAL:   0.55021887673509462        FVAL:  -0.99668173496676893     
 XVAL:   0.54819664405508539        FVAL:  -0.79223514088950409     
 XVAL:   0.55224027991553515        FVAL:  -0.69283908038410313     
 XVAL:   0.54920786199518812        FVAL:  -0.95718257528502082     
 XVAL:   0.55122968409989481        FVAL:  -0.89911112206792232     
 XVAL:   0.55001669024027922        FVAL:  -0.99998067325751328     
 XVAL:   0.55042105493490279        FVAL:  -0.98776139504853400     
 XVAL:   0.54997584747860118        FVAL:  -0.99995955985551355     
 XVAL:   0.54795351745711929        FVAL:  -0.73849148324188363     
 XVAL:   0.55199735201488953        FVAL:  -0.74968815036674952     
 XVAL:   0.54999624773645450        FVAL:  -0.99999902563190934     
 XVAL:   0.54797392586790306        FVAL:  -0.74316297267908471     
 XVAL:   0.55201774378283996        FVAL:  -0.74505962542737447     

 MIGRAD MINIMIZATION HAS CONVERGED.

 FCN= -0.9999990     FROM MIGRAD    STATUS=CONVERGED     17 CALLS      110 TOTAL
                     EDM=  0.87E-06  STRATEGY=1  ERROR MATRIX UNCERTAINTY=  5.4%

  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME        VALUE          ERROR          SIZE      DERIVATIVE 
   1    para1      0.55000       0.42119E-02  -0.41006E-04   0.23334

找最小值有一定随机性,例如scale改为10,结果如下:

XVAL:   0.50000000000000000        FVAL:   0.97560974474682971     
 **********
 **    1 **SET PRINTOUT  0.1587-313
 **********

 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 'para1     '   0.50000      0.10000E-02     0.0000       1.0000    
 **********
 **    2 **SET STRATEGY  0.5305-314
 **********
 **********
 **    3 **MIGRAD  0.7703E-26   0.000    
 **********
 XVAL:   0.50000000000000000        FVAL:   0.97560974474682971     
 XVAL:   0.50001000000696405        FVAL:   0.97561443853647745     
 XVAL:   0.49998999999303595        FVAL:   0.97559542035102087     
 XVAL:   0.50000312758495857        FVAL:   0.97561224778513467     
 XVAL:   0.49999687241504148        FVAL:   0.97560629966369106     
 XVAL:   0.27112785285767249        FVAL:  -0.52758163940408165     
 XVAL:   0.15398026789026997        FVAL:  -0.12261175677271749     
 XVAL:    1.1989409613079260E-002   FVAL:  -0.20823130360913300     
 XVAL:   0.97285414107188140        FVAL:  -0.22389160020031401     
 XVAL:   0.64351971292093135        FVAL:   0.41239283833208851     
 XVAL:   0.45398478997104891        FVAL:  -0.28708891011905729     
 XVAL:    1.4714439555999104E-002   FVAL:   -2.3178262258938803E-002
 XVAL:   0.10786339105596815        FVAL:  -0.26500661254163782     
 XVAL:   0.27115566015938020        FVAL:  -0.52590238908331055     
 XVAL:   0.27110004645147623        FVAL:  -0.52922028070892424     
 XVAL:   0.27113157702186796        FVAL:  -0.52735909099236555     
 XVAL:   0.27112412870953984        FVAL:  -0.52780345940350271     
 MINUIT WARNING IN MIGRAD  
 ============== Negative diagonal element  1 in Error Matrix
 MINUIT WARNING IN MIGRAD  
 ==============     0.10E+01 added to diagonal of error matrix
 XVAL:    3.9654383933650061E-002   FVAL:   0.27579520981445249     
 XVAL:   0.13870528071376459        FVAL:   0.34121009693205140     
 XVAL:   0.79262987213766822        FVAL:  -0.42655992445560387     
 XVAL:    7.3326929957164055E-002   FVAL:  -0.15333568605379091     
 XVAL:    1.9534839593589814E-002   FVAL:   0.25940187431116379     
 XVAL:   0.11829220711988486        FVAL:   0.30013223382948379     
 XVAL:   0.19440481488797012        FVAL:   -8.2091329835333349E-002
 XVAL:   0.23490668896173283        FVAL:   -1.4707140072779509E-002
 XVAL:   0.25622121357561078        FVAL:   0.20091366107442823     
 XVAL:   0.26698731430451272        FVAL:  -0.32458081316210441     
 XVAL:   0.26998820979165705        FVAL:  -0.56051350889646478     
 XVAL:   0.26999196888929766        FVAL:  -0.56052218289917344     
 XVAL:   0.26998445071050720        FVAL:  -0.56050405333887499     
 XVAL:   0.27003239936882417        FVAL:  -0.56056605747890120     
 XVAL:   0.27003606756336546        FVAL:  -0.56056556321380246     
 XVAL:   0.27002873118998105        FVAL:  -0.56056580721666105     
 XVAL:   0.27003179813580114        FVAL:  -0.56056606747403148     
 XVAL:   0.27003546536332806        FVAL:  -0.56056569543803192     
 XVAL:   0.27002813092396422        FVAL:  -0.56056569537850387     

 MIGRAD MINIMIZATION HAS CONVERGED.

 FCN= -0.5605661     FROM MIGRAD    STATUS=CONVERGED     35 CALLS       36 TOTAL
                     EDM=  0.12E-11    STRATEGY= 0      NO ERROR MATRIX       

  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME        VALUE          ERROR          SIZE      DERIVATIVE 
   1    para1      0.27003       0.10000E-02  -0.13542E-05  -0.36034E-05
 **********
 **    4 **IMPROVE  0.7703E-26
 **********
 XVAL:   0.27003179813580114        FVAL:  -0.56056606747403148     
 XVAL:   0.27003546536332806        FVAL:  -0.56056569543803192     
 XVAL:   0.27002813092396422        FVAL:  -0.56056569537850387     

 START ATTEMPT NO. 1 TO FIND NEW MINIMUM
 XVAL:   0.26715423783747744        FVAL:  -0.34793129531242889     
 XVAL:   0.26884959940495695        FVAL:  -0.52243692970045741     
 XVAL:   0.26546228777173131        FVAL:   -7.9974662997188098E-002
 XVAL:   0.26377377399704915        FVAL:   0.20655223051064761     
 XVAL:   0.26040715422610572        FVAL:   0.53943685564288935     
 XVAL:   0.25705457582584773        FVAL:   0.32374341260153544     
 XVAL:   0.25039232821619484        FVAL:  -0.52297048735803675     
 XVAL:   0.24378859291284788        FVAL:   0.19170232312021507     
 XVAL:   0.23076283725979169        FVAL:  -0.48110980191819763     
 XVAL:   0.21798952199030558        FVAL:   0.38390087771213177     
 XVAL:   0.19324787040979696        FVAL:  -0.23015053025528320     
 XVAL:   0.16965640884112038        FVAL:  -0.40634957961240870     
 XVAL:   0.12627324312217220        FVAL:  -0.13930527561428729     
 XVAL:   0.14730359523075209        FVAL:  -0.25257407722609548     
 XVAL:   0.16965640884112038        FVAL:  -0.40634957961240870     
 XVAL:   0.15477467242351778        FVAL:   -2.7606512305086924E-002
 XVAL:   0.18507665929375566        FVAL:   -1.0328123415746682E-002
 XVAL:   0.16214668910348046        FVAL:   0.31189518473251643     
 XVAL:   0.16965640884112038        FVAL:  -0.40634957961240870     
 XVAL:   0.16008159187939547        FVAL:   0.39663777711190262     
 XVAL:   0.17945121037716505        FVAL:   0.41514883406712205     
 XVAL:   0.16484110029697024        FVAL:    2.0092063616358898E-002
 XVAL:   0.17452671581800311        FVAL:   -6.1473245568654404E-002
 XVAL:   0.16724182944290883        FVAL:  -0.26267130112242415     
 XVAL:   0.17208473799059004        FVAL:  -0.32661401778953603     
 XVAL:   0.17086886100066601        FVAL:  -0.39508286880155324     
 XVAL:   0.16844739412833143        FVAL:  -0.35970929631772686     
 XVAL:   0.17026220602773540        FVAL:  -0.40811047932895633     
 XVAL:   0.16905147101675339        FVAL:  -0.38998127720378378     
 XVAL:   0.16995920011259030        FVAL:  -0.40907977770651133     
 XVAL:   0.16995920011259030        FVAL:  -0.40907977770651133     
 START SIMPLEX MINIMIZATION.    CONVERGENCE WHEN EDM .LT.  0.10E+00
 XVAL:   0.17601314954341457        FVAL:   0.13046616297364058     
 XVAL:   0.16756120971072003        FVAL:  -0.29260737413489657     
 XVAL:   0.17092218996537145        FVAL:  -0.39323972163338716     
 XVAL:   0.16899837492565162        FVAL:  -0.38786515745248218     
 XVAL:   0.17044042485017086        FVAL:  -0.40581031250609451     
 XVAL:   0.16947851654169227        FVAL:  -0.40303262782579863     
 XVAL:   0.17019974488484091        FVAL:  -0.40861366423810264     
 SIMPLEX MINIMIZATION HAS CONVERGED.
 XVAL:   0.16995920011259036        FVAL:  -0.40907977770651149     

 FCN= -0.4090798     FROM SIMPLEX   STATUS=PROGRESS       8 CALLS       78 TOTAL
                     EDM=  0.17E-15    STRATEGY= 0      ERROR MATRIX ACCURATE 

  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME        VALUE          ERROR          SIZE      DERIVATIVE 
   1    para1      0.16996       0.60119E-02   0.11102E-15    1325.2    

 FCN= -0.5605661     FROM SIMPLEX   STATUS=RESET          8 CALLS       78 TOTAL
                     EDM=  0.12E-14  STRATEGY=0  ERROR MATRIX UNCERTAINTY= 50.0%

  EXT PARAMETER                APPROXIMATE     INTERNAL      INTERNAL  
  NO.   NAME        VALUE          ERROR       STEP SIZE       VALUE   
   1    para1      0.27003       0.60119E-02   0.60119E-04  -0.47792 

结论minuit不适合给单个可调参数找最小值,很容易找到极小值,最好将初始值设置在最小值同一个山谷内

你可能感兴趣的:(Fortran)