├── README.md ├── imgs └── opt_matlab.jpg ├── 第10章 ├── 例10.1 │ ├── Busacker.m │ └── 例10.1.txt ├── 例10.10 │ ├── Postman.m │ └── 例10.10.txt ├── 例10.11 │ ├── graphTSP.m │ └── 例10.11.txt ├── 例10.12 │ └── 例10.12.txt ├── 例10.13 │ ├── colorcodf.asv │ ├── colorcodf.m │ ├── graphpts.m │ └── 例10.13.txt ├── 例10.14 │ └── 例10.14.txt ├── 例10.2 │ ├── Ford.m │ └── 例10.2.txt ├── 例10.3 │ └── 例10.3.txt ├── 例10.4 │ ├── Maxpath.m │ ├── Mindist.m │ ├── pointdist.m │ └── 例10.4.txt ├── 例10.5 │ └── 例10.5.txt ├── 例10.6 │ └── 例10.6.txt ├── 例10.7 │ └── 例10.7.txt ├── 例10.8 │ └── 例10.8.txt └── 例10.9 │ ├── mintree.m │ └── 例10.9.txt ├── 第11章 ├── 例11.1 │ ├── myga.m │ ├── optifun14.m │ ├── optifun16.m │ └── 例11.1.txt ├── 例11.10 │ ├── optifun24.asv │ ├── optifun24.m │ └── 例11.10.txt ├── 例11.11 │ ├── boundtest.m │ ├── gaDE.m │ ├── optifun25.m │ └── 例11.11.txt ├── 例11.12 │ ├── gaDE1.m │ ├── optifun26.m │ └── 例11.12.txt ├── 例11.13 │ ├── memetic.m │ ├── optifun27.m │ └── 例11.13.txt ├── 例11.14 │ ├── city2d.m │ ├── memeticTSP.m │ ├── value.m │ └── 例11.14.txt ├── 例11.15 │ └── 例11.14.txt ├── 例11.2 │ ├── myga1.m │ ├── optifun15.m │ └── 例11.2.txt ├── 例11.3 │ ├── gaexhause.m │ ├── optifun20.m │ └── 例11.3.txt ├── 例11.4 │ ├── optifun18.m │ └── 例11.4.txt ├── 例11.5 │ ├── optifun19.m │ ├── press.m │ └── 例11.5.txt ├── 例11.6 │ ├── GAJSP.m │ └── 例11.6.txt ├── 例11.7 │ ├── gaEP.m │ ├── optifun21.m │ └── 例11.7.txt ├── 例11.8 │ ├── gaEP1.m │ ├── optifun22.m │ └── 例11.8.txt └── 例11.9 │ ├── gaES.m │ ├── optifun23.m │ └── 例11.9.txt ├── 第12章 ├── 例12.1 │ ├── MainAnealTSP.m │ ├── TSPplot.m │ ├── exchange.m │ ├── matrixinsert.m │ └── 例12.1.txt ├── 例12.2 │ ├── MainAneal.m │ ├── optifun28.m │ └── 例12.2.txt ├── 例12.3 │ ├── optifun29.m │ └── 例12.3.txt └── 例12.4 │ ├── cluster_dis.m │ ├── fy.m │ ├── guiyi_range.m │ ├── myclass.m │ ├── optifun33.m │ └── 例12.4.txt ├── 第13章 ├── 例13.1 │ ├── TSOATSP.m │ └── 例13.1.txt ├── 例13.2 │ ├── TSOA.m │ ├── optifun13.m │ ├── selectCandidate.m │ └── 例13.2.txt └── 例13.3 │ ├── TSOA1.m │ ├── cluster_center.m │ ├── cluster_dis.m │ ├── correct.m │ ├── crossvalindtest.m │ ├── guiyi.m │ ├── optifun40.m │ └── 例13.3.txt ├── 第14章 ├── 例14.1 │ ├── antTSP.m │ └── 例14.1.txt ├── 例14.2 │ ├── antmin.m │ ├── optifun31.m │ ├── optifun32.m │ └── 例14.2.txt └── 例14.3 │ ├── antcluster.m │ ├── center_dis.m │ ├── cluster_center.m │ ├── cluster_dis.m │ ├── guiyi.m │ └── 例14.3.txt ├── 第15章 ├── 例15.1 │ ├── mypso.m │ ├── optifun35.m │ └── 例15.1.txt ├── 例15.2 │ ├── mypsoTSP.m │ └── 例15.2.txt ├── 例15.3 │ ├── optifun37.m │ └── 例15.3.txt ├── 例15.4 │ ├── COApso.m │ ├── createCOA.m │ ├── optifun38.m │ └── 例15.4.txt └── 例15.5 │ └── 例15.5.txt ├── 第16章 ├── 例16.1 │ ├── fish.m │ ├── optifun41.m │ └── 例16.1.txt ├── 例16.2 │ ├── fishdstc.m │ ├── fishevaluate.m │ ├── fishfollow.m │ ├── fishmove.m │ ├── fishprey.m │ ├── fishswarm.m │ ├── newfish.m │ ├── optifun42.m │ └── 例16.2.txt ├── 例16.3 │ ├── fishTSP.m │ └── 例16.3.txt ├── 例16.4 │ └── 例16.4.txt ├── 例16.5 │ ├── Dec.m │ ├── binfish.m │ ├── fishevaluate3_1.m │ ├── fishfollow3_1.m │ ├── fishmove3_1.m │ ├── fishprey3_1.m │ ├── fishswarm3_1.m │ ├── optifun15_1.m │ └── 例16.5.txt └── 例16.6 │ ├── optifun45.m │ └── 例16.6.txt ├── 第17章 ├── 例17.1 │ ├── SFLA.m │ ├── optifun46.m │ └── 例17.1.txt ├── 例17.2 │ ├── SFLATSP.m │ └── 例17.2.txt ├── 例17.3 │ ├── SFLAcluster.m │ ├── optifun33.m │ └── 例17.3.txt └── 例17.4 │ ├── bitSFLA.m │ ├── optifun47.m │ └── 例17.4.txt ├── 第18章 ├── 例18.1 │ ├── Dec1.m │ ├── qGA.m │ └── 例18.1.txt ├── 例18.2 │ ├── niching.m │ ├── optifun49.m │ ├── qGA1.m │ ├── rotationq.m │ ├── trimatrix.m │ └── 例18.2.txt ├── 例18.3 │ ├── isin_TSP.m │ ├── qGATSP.m │ └── 例18.3.txt └── 例18.4 │ ├── optifun50.m │ ├── qGA3.m │ └── 例18.4.txt ├── 第19章 ├── 例19.1 │ ├── ABC.m │ ├── optifun51.m │ ├── sum1.m │ └── 例19.1.txt ├── 例19.2 │ ├── newABC.m │ ├── optifun52.m │ └── 例19.2.txt ├── 例19.3 │ ├── optifun53.m │ └── 例19.3.txt ├── 例19.4 │ ├── optifun54.m │ └── 例19.4.txt ├── 例19.5 │ ├── optifun55.m │ ├── realbit.m │ └── 例19.5.txt └── 例19.6 │ ├── TSP_opt.m │ ├── TSPop.m │ ├── bitABC.m │ ├── greed.m │ ├── init_ABC.m │ ├── isin_TSP.m │ ├── matrixinsert.m │ └── 例19.6.txt ├── 第20章 ├── 例20.1 │ ├── chaos.m │ ├── optifun56.m │ ├── optifun57.m │ └── 例20.1.txt ├── 例20.2 │ ├── chaosTSP.m │ └── 例20.2.txt ├── 例20.3 │ ├── delsample.m │ ├── newchoas.m │ ├── optifun58.m │ └── 例20.3.txt └── 例20.4 │ ├── chaos_serch.m │ ├── gachaos.m │ ├── optifun59.m │ └── 例20.4.txt ├── 第21章 ├── 例21.1 │ ├── IA.m │ ├── optifun64.m │ └── 例21.1.txt ├── 例21.2 │ ├── dyIA.m │ ├── optifun70.m │ └── 例21.2.txt ├── 例21.3 │ ├── IAGA.m │ ├── ICA_bacterin.m │ ├── ICA_caldensity.m │ ├── ICA_calpd.m │ ├── ICA_calpf.m │ ├── ICA_densitysort.m │ ├── ICA_fitsort.m │ ├── ICA_operator.m │ ├── ICA_select.m │ ├── gacrossover.m │ ├── gamutation.m │ ├── optifun71.m │ └── 例21.3.txt ├── 例21.4 │ ├── ICA_TSP.m │ ├── ICA_operator_TSP.m │ ├── gacrossover_TSP.m │ ├── gamutation_TSP.m │ ├── my_delrow.m │ └── 例21.4.txt ├── 例21.5 │ ├── IAEP.m │ ├── gaussmution.m │ ├── optifun72.m │ └── 例21.5.txt ├── 例21.6 │ ├── IAES.m │ ├── IAES_mutation.m │ ├── IAES_recombination1.m │ ├── IAES_select1.m │ ├── optifun73.m │ └── 例21.6.txt ├── 例21.7 │ ├── IAClone.m │ ├── clone.m │ ├── clonemutation.m │ ├── cloneselection.m │ ├── optifun74.m │ └── 例21.7.txt ├── 例21.8 │ ├── IAClone_bit.m │ ├── ICA_operator_bit.m │ ├── clonemutation_bit.m │ ├── optifun75.m │ └── 例21.8.txt └── 例21.9 │ ├── IA_cluster.m │ ├── cluster_center.m │ ├── dis2.m │ ├── dyICA_bacterin.m │ ├── dyICA_operator.m │ ├── gacrossover_dy.m │ ├── gamutation_dy.m │ ├── optifun76.m │ └── 例21.9.txt ├── 第22章 ├── 例22.1 │ ├── BFO.m │ ├── optifun63.m │ └── 例22.1.txt ├── 例22.2 │ ├── BFO1.m │ ├── mutation_DE.m │ ├── my_delrow.m │ ├── optifun65.m │ └── 例22.2.txt ├── 例22.3 │ ├── optifun66.m │ ├── psoBFO.m │ └── 例22.3.txt ├── 例22.4 │ ├── optifun67.m │ └── 例22.4.txt ├── 例22.5 │ ├── BFOTSP.m │ ├── greedycross.m │ └── 例22.5.txt └── 例22.6 │ ├── BFO1cluster.m │ ├── lack_iter.m │ ├── mutation_DE1.m │ ├── mymykmeans.m │ ├── optifun68.m │ └── 例22.6.txt ├── 第23章 ├── 例23.1 │ ├── CAT.m │ ├── optifun60.m │ └── 例23.1.txt ├── 例23.2 │ ├── cat1.m │ ├── optifun61.m │ └── 例23.2.txt ├── 例23.3 │ ├── optifun62.m │ └── 例23.3.txt └── 例23.4 │ ├── catTSP.m │ ├── isin.m │ └── 例23.4.txt ├── 第24章 ├── 例24.1 │ ├── GABP.m │ └── 例24.1.txt ├── 例24.2 │ └── 例24.2.txt └── 例24.3 │ ├── hopfieldTSP.m │ └── 例24.3.txt ├── 第25章 ├── 例25.1 │ ├── FA.m │ ├── optifun77.m │ └── 例25.1.txt ├── 例25.10 │ ├── APOA.m │ ├── optifun87.m │ └── 例25.10.txt ├── 例25.11 │ ├── ECA.m │ ├── optifun88.m │ └── 例25.11.txt ├── 例25.12 │ ├── CA.m │ ├── optifun89.m │ └── 例25.12.txt ├── 例25.13 │ ├── CRO.m │ ├── findpos2.m │ └── 例25.13.txt ├── 例25.14 │ ├── FWA.m │ ├── optifun96.m │ └── 例25.14.txt ├── 例25.2 │ ├── BA.m │ ├── optifun78.m │ └── 例25.2.txt ├── 例25.3 │ ├── FOA.m │ ├── optifun79.m │ ├── optifun79_1.m │ └── 例25.3.txt ├── 例25.4 │ ├── BBO.m │ ├── optifun84.m │ └── 例25.4.txt ├── 例25.5 │ ├── IWO.m │ ├── optifun82.m │ └── 例25.5.txt ├── 例25.6 │ ├── GSA.m │ ├── optifun80.m │ └── 例25.6.txt ├── 例25.7 │ ├── HS.m │ ├── optifun83.m │ └── 例25.7.txt ├── 例25.8 │ ├── GWO.m │ ├── optifun85.m │ └── 例25.8.txt └── 例25.9 │ ├── CS.m │ ├── levy.m │ ├── optifun86.m │ └── 例25.9.txt ├── 第26章 ├── 例26.1 │ ├── SSO.m │ ├── optifun90.m │ └── 例26.1.txt ├── 例26.10 │ ├── gradnewton.m │ └── 例26.10.txt ├── 例26.2 │ ├── GAPSO_TSP.m │ └── 例26.2.txt ├── 例26.3 │ ├── SAPSO.m │ ├── optifun91.m │ └── 例26.3.txt ├── 例26.4 │ ├── FCMCluster.m │ ├── GASAA.m │ └── 例26.4.txt ├── 例26.5 │ ├── GAACOA_TSP.m │ └── 例26.5.txt ├── 例26.6 │ ├── SMSA.m │ ├── optifun92.m │ └── 例26.6.txt ├── 例26.7 │ ├── CAFSA.m │ ├── optifun93.m │ └── 例26.7.txt ├── 例26.8 │ ├── HS1.m │ ├── optifun94.m │ └── 例26.8.txt └── 例26.9 │ ├── MA.m │ ├── optifun95.m │ └── 例26.9.txt ├── 第2章 ├── 例2.1 │ ├── interval1.m │ ├── opfun1.m │ ├── opfun1_1.m │ └── 例2.1.txt ├── 例2.10 │ ├── hooke1.m │ ├── mycomplex1.m │ ├── opfun9.m │ ├── powell1.m │ ├── redu.m │ └── 例2.10.txt ├── 例2.11 │ ├── optifun5.m │ └── 例2.11.txt ├── 例2.2 │ ├── myfzeros.m │ ├── myjacobian1.m │ ├── mymultifun1.m │ └── 例2.2.txt ├── 例2.3 │ ├── goldcut1.m │ ├── interpolation1.m │ ├── myDF1.m │ ├── mydiff1.m │ ├── mynewton11.m │ ├── myparabola1.m │ ├── mysucfail1.m │ ├── opfun2.m │ └── 例2.3.txt ├── 例2.4 │ ├── mymin.m │ ├── mysearch1.m │ ├── opfun3.m │ └── 例2.4.txt ├── 例2.5 │ ├── opfun4.m │ └── 例2.5.txt ├── 例2.6 │ ├── mygrad1.m │ ├── mynewton21.m │ ├── opfun5.m │ └── 例2.6.txt ├── 例2.7 │ ├── myconju1.m │ ├── opfun6.m │ └── 例2.7.txt ├── 例2.8 │ ├── coord1.m │ ├── opfun7.m │ └── 例2.8.txt └── 例2.9 │ ├── BFGS1.m │ ├── DFP1.m │ ├── mybroyden1.m │ ├── mynnewtown1.m │ ├── opfun8.m │ └── 例2.9.txt ├── 第3章 ├── 例3.1 │ ├── sumt1.m │ └── 例3.1.txt ├── 例3.2 │ ├── PHR1.m │ ├── dlag1.m │ ├── lag1.m │ ├── mybfgs1.m │ ├── opfun11.m │ ├── opgfun1.m │ ├── ophfun1.m │ └── 例3.2.txt ├── 例3.3 │ ├── findzeros.m │ ├── myJT1.m │ ├── mygoldcut1.m │ ├── zoutendijk1.m │ └── 例3.3.txt ├── 例3.4 │ ├── rosen1.m │ └── 例3.4.txt ├── 例3.5 │ ├── mycompare1.m │ ├── wolfe1.m │ └── 例3.5.txt ├── 例3.6 │ ├── dlg11.m │ ├── lghess1.m │ ├── lgmatrix1.m │ ├── myhessian1.m │ ├── newlag1.m │ └── 例3.6.txt ├── 例3.7 │ ├── BB.m │ ├── sqp1.m │ └── 例3.7.txt ├── 例3.8 │ ├── newquad.m │ ├── newsqp1.m │ └── 例3.8.txt └── 例3.9 │ ├── mycon1.m │ ├── optifun7.m │ └── 例3.9.txt ├── 第4章 ├── 例4.1 │ ├── myls.m │ └── 例4.1.txt ├── 例4.2 │ ├── mylm.m │ └── 例4.2.txt ├── 例4.3 │ ├── mylm1.m │ └── 例4.3.txt ├── 例4.4 │ ├── newls1.m │ └── 例4.4.txt ├── 例4.5 │ ├── orthfun.m │ ├── simplify.m │ └── 例4.5.txt └── 例4.6 │ ├── optifun8.m │ ├── optifun9.m │ └── 例4.6.txt ├── 第5章 ├── 例5.1 │ ├── dualsimplex.m │ ├── findeye.m │ ├── findzeros.m │ ├── mycompare.m │ ├── mycompare1.m │ ├── mysimplex.m │ └── 例5.1.txt ├── 例5.2 │ ├── simplex1.m │ └── 例5.2.txt ├── 例5.3 │ └── 例5.3.txt ├── 例5.4 │ └── 例5.4.txt ├── 例5.5 │ └── 例5.5.txt ├── 例5.6 │ ├── progturn.m │ └── 例5.6.txt └── 例5.7 │ └── 例5.7.txt ├── 第6章 ├── 例6.1 │ ├── decisfun1.m │ ├── dyprog.m │ ├── exhaus.m │ ├── findletter.m │ ├── road.m │ ├── simplex.m │ ├── simplexphaseone.m │ ├── simplexphasetwo.m │ ├── simplexpiecewise.m │ ├── simplexpostsolve.m │ ├── simplexpresolve.m │ ├── subfun1.m │ ├── trafun1.m │ └── 例6.1.txt ├── 例6.2 │ ├── decisfun2.m │ ├── intprog.m │ ├── myinteger.m │ ├── subfun2.m │ ├── trafun2.m │ └── 例6.2.txt ├── 例6.3 │ ├── decisfun3.m │ ├── decisfun4.m │ ├── subfun3.m │ ├── trafun3.m │ └── 例6.3.txt ├── 例6.4 │ ├── decisfun6.m │ ├── dyprog2.m │ ├── subfun6.m │ ├── trafun6.m │ └── 例6.4.txt ├── 例6.5 │ ├── decisfun5.m │ ├── subfun5.m │ ├── trafun5.m │ └── 例6.5.txt ├── 例6.6 │ ├── decisfun7.m │ ├── subfun7.m │ ├── trafun7.m │ └── 例6.6.txt ├── 例6.7 │ ├── decisfun8.m │ ├── objfun8.m │ ├── subfun8.m │ ├── trafun8.m │ └── 例6.7.txt ├── 例6.8 │ ├── decisfun9.m │ ├── optifun10.m │ ├── optifun11.m │ ├── subfun9.m │ ├── trafun9.m │ └── 例6.8.txt └── 例6.9 │ ├── dyprogTSP.m │ ├── findpos1.m │ └── 例6.9.txt ├── 第7章 ├── 例7.1 │ ├── greedy.m │ └── 例7.1.txt ├── 例7.2 │ ├── findeye.m │ ├── gomory.m │ ├── mydec.m │ └── 例7.2.txt ├── 例7.3 │ └── 例7.3.txt ├── 例7.4 │ └── 例7.4.txt ├── 例7.5 │ └── 例7.5.txt ├── 例7.6 │ ├── mybintprog.m │ ├── mydec2bin.m │ └── 例7.6.txt ├── 例7.7 │ ├── Hungarian.m │ ├── findcount.m │ ├── lattice.m │ └── 例7.7.txt ├── 例7.8 │ └── 例7.8.txt └── 例7.9 │ └── 例7.9.txt ├── 第8章 ├── 例8.1 │ ├── myquad.asv │ ├── myquad.m │ └── 例8.1.txt ├── 例8.2 │ ├── myquad1.m │ └── 例8.2.txt ├── 例8.3 │ ├── newquad.m │ └── 例8.3.txt └── 例8.4 │ └── 例8.4.txt └── 第9章 ├── 例9.1 └── 例9.1.txt ├── 例9.2 └── 例9.2.txt ├── 例9.3 └── 例9.3.txt ├── 例9.4 └── 例9.4.txt ├── 例9.5 └── 例9.5.txt ├── 例9.6 ├── findeye.m ├── multgoal.m ├── progturn.m └── 例9.6.txt └── 例9.7 └── 例9.7.txt /README.md: -------------------------------------------------------------------------------- 1 | # 最优化方法及其MATLAB实现 2 | 3 | 5124-2716-7 《最优化方法及其MATLAB实现》 程序包新 习题 4 | 5 | 论坛:https://www.ilovematlab.cn/forum-268-1.html 6 | 7 |

8 | 9 |

10 | 11 | --- 12 | 13 | ## 程序使用说明 14 | 15 | 1.本习题夹所列的例题程序在2019版本中全部运行通过,经过测试,部分在2014与2016版本中也能运行通过。 16 | 17 | 2.请按照例题给出的函数输入格式输入参数。 18 | 19 | 3.运行程序前请将所有的例题程序所在的文件列入matlab的搜索路径中。 20 | 21 | 4.由于没有测试全部的程序是否能在2014与2016版本中通过,所以当用这两个版本运行程序时有可能会出现问题。 22 | 23 | 5.运行时如果出现程序错误,或者出现函数缺失(无定义)等问题,请联系作者。作者邮箱xuggsx@sins.com,微信13572198239。 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /imgs/opt_matlab.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/imgs/opt_matlab.jpg -------------------------------------------------------------------------------- /第10章/例10.1/Busacker.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.1/Busacker.m -------------------------------------------------------------------------------- /第10章/例10.1/例10.1.txt: -------------------------------------------------------------------------------- 1 | f=[4 1 2 3 2 4 4 6 3 4 3 1 2 3]; 2 | Aeq=[ones(1,3) zeros(1,11);zeros(1,3) ones(1,3) zeros(1,8); 3 | 1 zeros(1,2) 1 zeros(1,2) -1 zeros(1,7); 4 | zeros(1,1) 1 zeros(1,2) 1 zeros(1,2) -1*ones(1,2) zeros(1,5); 5 | zeros(1,2) 1 zeros(1,2) 1 zeros(1,3) -1 zeros(1,4); 6 | zeros(1,6) 1 1 zeros(1,2) -1 -1 zeros(1,2); 7 | zeros(1,8) 1 1 zeros(1,2) -1 -1; 8 | zeros(1,10) -1 0 -1 0;zeros(1,11) -1 0 -1]; 9 | Beq=[45 75 0 0 0 0 0 -65 -55];LB=zeros(1,14)'; 10 | UB=[30 20 15 40 15 30 50 45 50 60 60 40 45 30]'; 11 | [x,val]=intprog(f,[],[],1:14,Aeq,Beq,LB,UB); 12 | 13 | 14 | clear 15 | c=[0 45 75 zeros(1,8);zeros(1,3) 30 20 15 zeros(1,5); 16 | zeros(1,3) 40 15 30 zeros(1,5); zeros(1,6) 50 zeros(1,4); 17 | zeros(1,6) 45 50 zeros(1,3); zeros(1,7) 60 zeros(1,3); 18 | zeros(1,8) 60 40 0; zeros(1,8) 45 30 0;zeros(1,10) 65;zeros(1,10) 55;zeros(1,11)]; 19 | b=[0 0 0 zeros(1,8); zeros(1,3) 4 1 2 zeros(1,5); zeros(1,3) 3 2 4 zeros(1,5); 20 | zeros(1,6) 4 zeros(1,4);zeros(1,6) 6 3 zeros(1,3); zeros(1,7) 4 zeros(1,3); 21 | zeros(1,8) 3 1 0; zeros(1,8) 2 3 0;zeros(1,10) 0;zeros(1,10) 0;zeros(1,11)]; 22 | [f,wf,zwf]=Busacker(c,b); 23 | 24 | 25 | -------------------------------------------------------------------------------- /第10章/例10.10/Postman.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.10/Postman.m -------------------------------------------------------------------------------- /第10章/例10.10/例10.10.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M=[zeros(1,1) 2 zeros(1,1) 3 zeros(1,8); 2 zeros(1,1) 1 5 2 zeros(1,7); 3 | zeros(1,1) 1 zeros(1,2) 1 zeros(1,7); 3 5 zeros(1,2) 8 5 6 zeros(1,1) 12 zeros(1,3); 4 | zeros(1,1) 2 1 8 zeros(1,2) 5 4 zeros(1,2) 8 zeros(1,1); 5 | zeros(1,3) 5 zeros(1,4) 9 zeros(1,3); zeros(1,3) 6 5 zeros(1,3) 4 zeros(1,1) 4 zeros(1,1); 6 | zeros(1,4) 4 zeros(1,5) 3 zeros(1,1); zeros(1,3) 12 zeros(1,1) 9 4 zeros(1,2) 3 6 4; 7 | zeros(1,8) 3 zeros(1,1) 3 zeros(1,1); zeros(1,4) 8 zeros(1,1) 4 3 6 3 zeros(1,1) 4; 8 | zeros(1,8) 4 zeros(1,1) 4 zeros(1,1)]; 9 | [Edge,Sum] =Postman(M); -------------------------------------------------------------------------------- /第10章/例10.11/graphTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.11/graphTSP.m -------------------------------------------------------------------------------- /第10章/例10.11/例10.11.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M=[0 15 15 5 25;15 0 10 15 20;15 10 0 35 30;5 15 35 0 15;25 20 30 15 0]; 3 | [road1,f0]=dyprogTSP(M); 4 | [Road2,Distance1]=graphTSP(M,1); 5 | [Road3,Distance2]=graphTSP(M,2); 6 | 7 | -------------------------------------------------------------------------------- /第10章/例10.12/例10.12.txt: -------------------------------------------------------------------------------- 1 | clear 2 | F=-[100 400 200 200 100 0 200 800 0 0 100 100 100 100 600 267 153 99 451 30 100 33 33 34 800]; 3 | A=[]; B=[]; 4 | Aeq=[ones(1,5) zeros(1,20); zeros(1,5) ones(1,5) zeros(1,15); zeros(1,10) ones(1,5) zeros(1,10); 5 | zeros(1,15) ones(1,5) zeros(1,5); zeros(1,20) ones(1,5); 6 | zeros(1,0) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4); 7 | zeros(1,1) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,3); 8 | zeros(1,2) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,2); 9 | zeros(1,3) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,1); 10 | zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,0)]; 11 | Beq = ones(1,10); 12 | [x1,min_fval1]=bintprog(F,A,B,Aeq,Beq); 13 | 14 | 15 | clear 16 | F=-[100 400 200 200 100 0 200 800 0 0 100 100 100 100 600 267 153 99 451 30 zeros(1,5)]; 17 | A=[zeros(1,0) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4); 18 | zeros(1,1) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,3); 19 | zeros(1,2) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,2); 20 | zeros(1,3) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,1); 21 | zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,0)]; 22 | B=ones(1,5); 23 | Aeq=[ones(1,5) zeros(1,20); zeros(1,5) ones(1,5) zeros(1,15); 24 | zeros(1,10) ones(1,5) zeros(1,10); zeros(1,15) ones(1,5) zeros(1,5); zeros(1,20) ones(1,5)]; 25 | Beq=[ones(1,4) 0]; 26 | [x2,min_fval2]=bintprog(F,A,B,Aeq,Beq); 27 | 28 | clear 29 | F=-[100 400 200 200 100 0 200 800 0 0 100 100 100 100 600 267 153 99 451 30 zeros(1,5)]; 30 | A=[zeros(1,5) ones(1,5) zeros(1,15); zeros(1,15) ones(1,5) zeros(1,5)]; 31 | B=2*ones(1,2); 32 | Aeq=[ones(1,5) zeros(1,20); zeros(1,10) ones(1,5) zeros(1,10); zeros(1,20) ones(1,5); 33 | zeros(1,0) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4); 34 | zeros(1,1) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,3); 35 | zeros(1,2) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,2); 36 | zeros(1,3) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,1); 37 | zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,0)]; 38 | Beq=[ones(1,2) 0 ones(1,5)]; 39 | [x3,min_fval3]=bintprog(F,A,B,Aeq,Beq); 40 | 41 | 42 | clear 43 | F=-[100 700 200 0 0 0 200 800 0 0 100 100 100 100 600 871 0 99 0 30 0 33 33 34 900]; 44 | A=[]; B=[]; 45 | Aeq = [ones(1,3) zeros(1,22); zeros(1,5) ones(1,5) zeros(1,15); 46 | zeros(1,10) ones(1,5) zeros(1,10); zeros(1,15) 1 0 1 0 1 zeros(1,5); 47 | zeros(1,21) ones(1,4); zeros(1,0) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,9); 48 | zeros(1,1) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,9) 1 zeros(1,3); 49 | zeros(1,2) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,2); 50 | zeros(1,8) 1 zeros(1,4) 1 zeros(1,9) 1 zeros(1,1); 51 | zeros(1,9) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,0)]; 52 | Beq=ones(1,10); 53 | [x4,min_fval4]=bintprog(F,A,B,Aeq,Beq); 54 | 55 | 56 | clear 57 | F=-[100 700 200 0 0 0 200 800 0 0 100 100 100 100 600 871 0 99 0 30... 58 | 0 33 33 34 900 250 250 0 250 250 111 1 0 333 555]; 59 | A=[]; B=[]; 60 | Aeq=[ones(1,3) zeros(1,32); zeros(1,5) ones(1,5) zeros(1,25); 61 | zeros(1,10) 1 0 ones(1,3) zeros(1,20); zeros(1,15) 1 0 1 0 1 zeros(1,15); 62 | zeros(1,21) ones(1,4) zeros(1,10); zeros(1,25) ones(1,2) 0 ones(1,2) zeros(1,5); 63 | zeros(1,30) ones(1,2) 0 ones(1,2); 64 | zeros(1,0) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,9) 1 zeros(1,4) 1 zeros(1,4); 65 | zeros(1,1) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,9) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,3); 66 | zeros(1,2) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,12); 67 | zeros(1,8) 1 zeros(1,4) 1 zeros(1,9) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,1); 68 | zeros(1,9) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,0)]; 69 | Beq=[ones(1,10) 2*ones(1,2)]; 70 | [x5,min_fval5]=bintprog(F,A,B,Aeq,Beq); 71 | 72 | clear 73 | F=-[100 400 200 200 100 0 200 800 0 0 100 100 100 100 600 267 153 99 451 30 100 33 33 34 800]; 74 | A=[ones(1,5) zeros(1,20); zeros(1,5) ones(1,5) zeros(1,15); 75 | zeros(1,10) ones(1,5) zeros(1,10); zeros(1,15) ones(1,5) zeros(1,5); 76 | zeros(1,20) ones(1,5);... 77 | zeros(1,0) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4); 78 | zeros(1,1) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,3); 79 | zeros(1,2) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,2); 80 | zeros(1,3) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,1); 81 | zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,4) 1 zeros(1,0)]; 82 | B=ones(1,10) ; 83 | Aeq=ones(1,25); 84 | Beq=3; 85 | [x6,min_fval6]=bintprog(F,A,B,Aeq,Beq); 86 | 87 | -------------------------------------------------------------------------------- /第10章/例10.13/colorcodf.asv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.13/colorcodf.asv -------------------------------------------------------------------------------- /第10章/例10.13/colorcodf.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.13/colorcodf.m -------------------------------------------------------------------------------- /第10章/例10.13/graphpts.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.13/graphpts.m -------------------------------------------------------------------------------- /第10章/例10.13/例10.13.txt: -------------------------------------------------------------------------------- 1 | clear 2 | W=[0 1 0 1 0 0 0;1 0 1 0 1 0 1;0 1 0 1 1 1 0;1 0 1 0 1 0 1;0 1 1 1 0 1 0;0 0 1 0 1 0 1;0 1 0 1 0 1 0]; 3 | [k,C]=colorcodf(W); -------------------------------------------------------------------------------- /第10章/例10.14/例10.14.txt: -------------------------------------------------------------------------------- 1 | W=[ 0 0 0 0 0 1 0 1 0 1 0 2 | 0 0 0 0 0 0 1 0 1 0 1 3 | 0 0 0 0 0 0 0 1 0 0 1 4 | 0 0 0 0 0 1 0 0 1 1 0 5 | 0 0 0 0 0 0 1 0 1 0 1 6 | 1 0 0 1 0 0 0 0 0 0 0 7 | 0 1 0 0 1 0 0 0 0 0 0 8 | 1 0 1 0 0 0 0 0 0 0 0 9 | 0 1 0 1 1 0 0 0 0 0 0 10 | 1 0 0 1 0 0 0 0 0 0 0 11 | 0 1 1 0 1 0 0 0 0 0 0]; 12 | [k,C]=colorcodf(W,4); -------------------------------------------------------------------------------- /第10章/例10.2/Ford.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.2/Ford.m -------------------------------------------------------------------------------- /第10章/例10.2/例10.2.txt: -------------------------------------------------------------------------------- 1 | clear 2 | C=[zeros(1,1) 60 90 zeros(1,9);zeros(1,3) 10 20 zeros(1,1) 30 zeros(1,5); 3 | zeros(1,3) 30 10 zeros(1,2) 50 zeros(1,4);zeros(1,4) 20 20 zeros(1,2) 15 zeros(1,3); 4 | zeros(1,5) 25 zeros(1,4) 10 zeros(1,1);zeros(1,6) 30 zeros(1,5); 5 | zeros(1,3) 18 zeros(1,4) 30 20 zeros(1,2);zeros(1,4) 25 20 30 zeros(1,2) 20 30 zeros(1,1); 6 | zeros(1,11) 45;zeros(1,11) 40;zeros(1,11) 40;zeros(1,12)]; 7 | [f,nf,wf,nwf,No]=Ford(C); 8 | -------------------------------------------------------------------------------- /第10章/例10.3/例10.3.txt: -------------------------------------------------------------------------------- 1 | clear 2 | c=[zeros(1,1) 6 zeros(1,1) 6 zeros(1,3); zeros(1,2) 2 zeros(1,1) 3 zeros(1,2); 3 | zeros(1,4) 2 2 zeros(1,1); zeros(1,2) 3 zeros(1,2) 1 2; zeros(1,6) 5;zeros(1,6) 4;zeros(1,7)]; 4 | b=[zeros(1,1) 6 zeros(1,1) 3 zeros(1,3); zeros(1,2) 5 zeros(1,1) 4 zeros(1,2); 5 | zeros(1,4) 4 3 zeros(1,1); zeros(1,2) 2 zeros(1,2) 3 8; zeros(1,6) 7;zeros(1,6) 4;zeros(1,7)]; 6 | [f,wf,zwf]=Busacker(c,b); 7 | -------------------------------------------------------------------------------- /第10章/例10.4/Maxpath.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.4/Maxpath.m -------------------------------------------------------------------------------- /第10章/例10.4/Mindist.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.4/Mindist.m -------------------------------------------------------------------------------- /第10章/例10.4/pointdist.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.4/pointdist.m -------------------------------------------------------------------------------- /第10章/例10.4/例10.4.txt: -------------------------------------------------------------------------------- 1 | M=[0 1 3 4;1 0 2 inf;3 2 0 5;4 inf 5 0]; 2 | [path,dist]=Mindist(M); 3 | 4 | 5 | clear; 6 | M=[0 inf*ones(1,2) 6 inf*ones(1,2); inf*ones(1,1) 0 10 inf*ones(1,3); inf 10 0 4 15 14; 7 | 6 inf 4 0 inf*ones(1,2); inf*ones(1,2) 15 inf 0 inf; inf*ones(1,2) 14 inf*ones(1,2) 0]; 8 | for i=1:6 9 | [path,dist(i)]=Mindist(M,3,i,1); 10 | end 11 | 12 | clear 13 | M=[0 1 inf 2 inf 6; 2 0 4 inf*ones(1,3); inf 3 0 inf*ones(1,2) 2; 14 | inf 1 inf 0 3 inf; inf*ones(1,3) 2 0 1; inf*ones(1,2) 3 inf 1 0]; 15 | [path,dist]=Mindist(M,3,4,1,6); 16 | 17 | 18 | clear 19 | M=[0 1 inf 2 inf 6; 2 0 4 inf*ones(1,3); inf 3 0 inf*ones(1,2) 2; 20 | inf 1 inf 0 3 inf; inf*ones(1,3) 2 0 1; inf*ones(1,2) 3 inf 1 0]; 21 | [path,dist]=Mindist(M,1,6,2); 22 | 23 | 24 | clear 25 | M=[1 0.8 zeros(1,2) 0.2; 0.4 1 0.7 zeros(1,2); 0 0.8 1 0.5 0; 0.6 zeros(1,2) 1 0.4; 0.3 0 0.7 0.5 1]; 26 | [path1,probability1,flag]=Maxpath(M,1,3); 27 | [path2,probability2,flag]=Maxpath(M,4,3); 28 | 29 | 30 | clear 31 | M=[1 0.8 zeros(1,2) 0.2; 0.4 1 0.7 zeros(1,2); 0 0.8 1 0.5 0; 0.6 zeros(1,2) 1 0.4; 0.3 0 0.7 0.5 1]; 32 | C=[inf 2 zeros(1,2) 10; 3 inf 4 zeros(1,2); zeros(1,1) 6 inf 3 zeros(1,1); 8 zeros(1,2) inf 5; 2 inf 8 5 inf]; 33 | [path,probability,flag]=Maxpath(M,C,1,3); 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /第10章/例10.5/例10.5.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M=[0 3 4 inf inf inf;3 0 1 6 1 inf;4 1 0 8 3 inf;inf 6 8 0 4 2;inf inf 3 4 0 7;inf inf inf 2 7 0]; 3 | dist=zeros(1,6); 4 | for i=1:6;for j=1:6;[path,a]=Mindist(M,i,j,1);dist(i)=dist(i)+a;end;end 5 | -------------------------------------------------------------------------------- /第10章/例10.6/例10.6.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M=[0 2 9 inf*ones(1,4); inf*ones(1,1) 0 6 8 inf*ones(1,3); 3 | inf*ones(1,2) 0 1 3 inf*ones(1,2); inf*ones(1,3) 0 4 3.5 inf*ones(1,1); 4 | inf*ones(1,4) 0 inf 2.5; inf*ones(1,5) 0 5; inf*ones(1,6) 0]; 5 | [path,dist]=Mindist(M,1,7,1); 6 | 7 | M=[zeros(1,1) 1 zeros(1,6); zeros(1,2) 1 1 zeros(1,4); zeros(1,3) 1 1 zeros(1,3);... 8 | zeros(1,4) 1 1 zeros(1,2); zeros(1,5) 1 1 zeros(1,1); zeros(1,7) 1; zeros(1,7) 1; zeros(1,8)]; 9 | N=[zeros(1,1) 0 zeros(1,6); zeros(1,2) 2 9 zeros(1,4); zeros(1,3) 6 8 zeros(1,3);... 10 | zeros(1,4) 1 3 zeros(1,2); zeros(1,5) 4 3.5 zeros(1,1); zeros(1,7) 2.5; zeros(1,7) 5; zeros(1,8)]; 11 | [f,wf,zwf]=Busacker(M,N); 12 | -------------------------------------------------------------------------------- /第10章/例10.7/例10.7.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M1=[0 16 22 30 41 59; inf*ones(1,1) 0 16 22 30 41; inf*ones(1,2) 0 17 23 31; 3 | inf*ones(1,3) 0 17 23; inf*ones(1,4) 0 18; inf*ones(1,5) 0]; 4 | [path,dist]=Mindist(M1,1,6,1); 5 | -------------------------------------------------------------------------------- /第10章/例10.8/例10.8.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M=[zeros(1,1) 5 4 2 inf*ones(1,13); inf zeros(1,1) inf*ones(1,2) 3 2 inf*ones(1,11); 3 | inf*ones(1,2) zeros(1,1) inf*ones(1,2) 3 2 inf*ones(1,10); 4 | inf*ones(1,3) zeros(1,1) inf*ones(1,2) 3 2 inf*ones(1,9); 5 | inf*ones(1,4) zeros(1,1) inf*ones(1,3) 5 3 inf*ones(1,7); 6 | inf*ones(1,5) zeros(1,1) inf*ones(1,3) 5 3 inf*ones(1,6); 7 | inf*ones(1,6) zeros(1,1) inf*ones(1,3) 5 3 inf*ones(1,5); 8 | inf*ones(1,7) zeros(1,1) inf*ones(1,3) 5 inf*ones(1,5); 9 | inf*ones(1,8) zeros(1,1) inf*ones(1,3) 2 1 inf*ones(1,3); 10 | inf*ones(1,9) zeros(1,1) inf*ones(1,3) 2 1 inf*ones(1,2); 11 | inf*ones(1,10) zeros(1,1) inf*ones(1,3) 2 1 inf*ones(1,1); 12 | inf*ones(1,11) zeros(1,1) inf*ones(1,3) 2 inf*ones(1,1); 13 | inf*ones(1,12) zeros(1,1) inf*ones(1,4); inf*ones(1,13) zeros(1,1) inf*ones(1,2) 0; 14 | inf*ones(1,14) zeros(1,1) inf*ones(1,1) 0; inf*ones(1,15) zeros(1,2); inf*ones(1,16) zeros(1,1)]; 15 | [path,dist]=Mindist(M,1,17,1); -------------------------------------------------------------------------------- /第10章/例10.9/mintree.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第10章/例10.9/mintree.m -------------------------------------------------------------------------------- /第10章/例10.9/例10.9.txt: -------------------------------------------------------------------------------- 1 | clear 2 | M=[0 4 2 inf*ones(1,5);4 0 inf*ones(1,2) 2 5 inf*ones(1,2);2 inf 0 2 4 inf*ones(1,3); 3 | inf*ones(1,2) 2 0 6 inf*ones(1,2) 7;inf 2 4 6 0 inf 3 5;inf 5 inf*ones(1,3) 0 3 inf; 4 | inf*ones(1,4) 3 3 0 2;inf*ones(1,3) 7 5 inf 2 0]; 5 | [t,c]=mintree(M,'k') 6 | 7 | 8 | clear 9 | M1=[0 4 2 -inf*ones(1,5);4 0 -inf*ones(1,2) 2 5 -inf*ones(1,2); 10 | 2 -inf 0 2 4 -inf*ones(1,3);-inf*ones(1,2) 2 0 6 -inf*ones(1,2) 7; 11 | -inf 2 4 6 0 -inf 3 5;-inf 5 -inf*ones(1,3) 0 3 -inf; 12 | -inf*ones(1,4) 3 3 0 2;-inf*ones(1,3) 7 5 -inf 2 0]; 13 | [t,c]=mintree(M1,'k','max'); -------------------------------------------------------------------------------- /第11章/例11.1/myga.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.1/myga.m -------------------------------------------------------------------------------- /第11章/例11.1/optifun14.m: -------------------------------------------------------------------------------- 1 | function y=optifun14(x) 2 | y=20+x(1)^2-10*cos(2*pi*x(1))+x(2)^2-10*cos(2*pi*x(2)); 3 | -------------------------------------------------------------------------------- /第11章/例11.1/optifun16.m: -------------------------------------------------------------------------------- 1 | function y=optifun16(x) 2 | g1=gcon1(x); 3 | g2=gcon2(x); 4 | punishment1=100; 5 | punishment2=200; 6 | punishment3=1000; 7 | punishment4=3000; 8 | q1=max(0,g1); 9 | %q1=min(0,g1); 10 | gamma1=1; 11 | if q1<=1e-9 12 | theta1=0; 13 | elseif q1>1e-9&&q1<0.001 14 | theta1=punishment1; 15 | elseif q1>=0.001&&q1<0.1 16 | theta1=punishment2; 17 | elseif q1>=0.1&&q1<1 18 | theta1=punishment3; 19 | else 20 | theta1=punishment4; 21 | gamma1=2; 22 | end 23 | lamda1=theta1*q1^gamma1; 24 | q2=max(0,g2); 25 | %q2=min(0,g2); 26 | gamma2=1; 27 | if q2<=1e-9 28 | theta2=0; 29 | elseif q2>1e-9&&q2<0.001 30 | theta2=punishment1; 31 | elseif q2>=0.001&&q2<0.1 32 | theta2=punishment2; 33 | elseif q2>=0.1&&q2<1 34 | theta2=punishment3; 35 | else 36 | theta2=punishment4; 37 | gamma2=2; 38 | end 39 | lamda2=theta2*q2^gamma2; 40 | y=100*(x(2)-x(1)^2)^2+(1-x(1))^2+(lamda1+lamda2); 41 | 42 | function result=gcon1(x) 43 | result=-x(1)-x(2)^2; 44 | 45 | function result=gcon2(x) 46 | result=-x(1)^2-x(2); -------------------------------------------------------------------------------- /第11章/例11.1/例11.1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.1/例11.1.txt -------------------------------------------------------------------------------- /第11章/例11.10/optifun24.asv: -------------------------------------------------------------------------------- 1 | function [y1,y2]=optifun24(x) 2 | y1=sin(x(1)+x(2))-6*exp(x(1))*x(2); 3 | y2=5*x(1)^2-4*x(2)-100; 4 | y=y1^2+y2^2; 5 | y=-1/(1+sqrt(y)); 6 | 7 | y=x^4-3*x^3-4 -------------------------------------------------------------------------------- /第11章/例11.10/optifun24.m: -------------------------------------------------------------------------------- 1 | function y=optifun24(x) 2 | y1=sin(x(1)+x(2))-6*exp(x(1))*x(2); 3 | y2=5*x(1)^2-4*x(2)-100; 4 | y=y1^2+y2^2; 5 | y=-1/(1+sqrt(y)); 6 | -------------------------------------------------------------------------------- /第11章/例11.10/例11.10.txt: -------------------------------------------------------------------------------- 1 | for i=1:10 2 | [val_x(i,:),val_f(i)]=gaES(@optifun24,300,500,-10*ones(2,1),10*ones(2,1)); 3 | end -------------------------------------------------------------------------------- /第11章/例11.11/boundtest.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.11/boundtest.m -------------------------------------------------------------------------------- /第11章/例11.11/gaDE.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.11/gaDE.m -------------------------------------------------------------------------------- /第11章/例11.11/optifun25.m: -------------------------------------------------------------------------------- 1 | function y=optifun25(x) 2 | y=x(1)^2*(4-2.1*x(1)^2+x(1)^4/3)+x(1)*x(2)+(4*x(2)^2-4)*x(2)^2; 3 | 4 | -------------------------------------------------------------------------------- /第11章/例11.11/例11.11.txt: -------------------------------------------------------------------------------- 1 | [val_x,val_f]=gaDE(@optifun25,150,1000,[-3;-2],[3;2]) -------------------------------------------------------------------------------- /第11章/例11.12/gaDE1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.12/gaDE1.m -------------------------------------------------------------------------------- /第11章/例11.12/optifun26.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.12/optifun26.m -------------------------------------------------------------------------------- /第11章/例11.12/例11.12.txt: -------------------------------------------------------------------------------- 1 | val_f=gaDE1(@optifun26,40,800,zeros(200,1),ones(200,1),3) -------------------------------------------------------------------------------- /第11章/例11.13/memetic.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.13/memetic.m -------------------------------------------------------------------------------- /第11章/例11.13/optifun27.m: -------------------------------------------------------------------------------- 1 | function y=optifun27(x) 2 | y=x(1)*sin(4*pi*x(1))-x(2)*sin(4*pi*x(2)+pi+1); 3 | y=-y; -------------------------------------------------------------------------------- /第11章/例11.13/例11.13.txt: -------------------------------------------------------------------------------- 1 | cbest=memetic(@optifun27,80,3,0.8,0.02,1000,[-1;-1],[2;2]) 2 | -------------------------------------------------------------------------------- /第11章/例11.14/city2d.m: -------------------------------------------------------------------------------- 1 | function [num,d]=city2d(city) 2 | [r,c]=size(city); 3 | if r==2 4 | num=c; 5 | d=squareform(pdist(city)); 6 | elseif c==2 7 | num=r; 8 | d=squareform(pdist(city)); 9 | else 10 | num=r; 11 | d=city; 12 | end -------------------------------------------------------------------------------- /第11章/例11.14/memeticTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.14/memeticTSP.m -------------------------------------------------------------------------------- /第11章/例11.14/value.m: -------------------------------------------------------------------------------- 1 | function fval=value(route,d)%used for reckoning the goal value of the selected traveling route 2 | n=length(d); 3 | fval=0; 4 | for i=1:n 5 | if i==n 6 | fval=fval+d(route(i),route(1)); 7 | else 8 | fval=fval+d(route(i),route(i+1)); 9 | end 10 | end -------------------------------------------------------------------------------- /第11章/例11.14/例11.14.txt: -------------------------------------------------------------------------------- 1 | clear 2 | city=[31 32;32 39;40 30;37 69;27 68;37 52;38 46;31 62;30 48;21 47;25 55;16 57; 3 | 17 63;42 41;17 33;25 32;5 64;8 52;12 42;7 38;5 25;10 17;45 35;42 57;32 22; 4 | 27 23;56 37;52 41;49 49;58 48;57 58;39 10;46 10;59 15;51 21;48 28;52 33; 5 | 58 27;61 33;62 63;20 26;5 6;13 13;21 10;30 15;36 16;62 42;63 69;52 64;43 67]; 6 | cbest=memeticTSP(city,50,7,1000); -------------------------------------------------------------------------------- /第11章/例11.15/例11.14.txt: -------------------------------------------------------------------------------- 1 | clear 2 | fitness=@(x)(4*cos(x(1)*2)+x(2))*exp(2*x(2)); 3 | A=[1 2];b=6; 4 | Lb=[0 0];ub=[6 6]; 5 | options=gaoptimset('tolfun',1e-6); 6 | [x,fval,exitflag]=ga(@(x)fitness(x),2,A,b,[],[],Lb,ub,[],options); 7 | -------------------------------------------------------------------------------- /第11章/例11.2/myga1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.2/myga1.m -------------------------------------------------------------------------------- /第11章/例11.2/optifun15.m: -------------------------------------------------------------------------------- 1 | function y=optifun15(x) 2 | y=sin(x(1))*sin(x(2))/x(1)/x(2); 3 | -------------------------------------------------------------------------------- /第11章/例11.2/例11.2.txt: -------------------------------------------------------------------------------- 1 | popsize=[];iterm_max=[];pm=[];px=[];LB=[-5.12;-5.12];UB=[5.12;5.12]; 2 | [max_x,maxfval]=myga1(@optifun15,LB,UB,popsize,iterm_max,px,pm,1e-8) -------------------------------------------------------------------------------- /第11章/例11.3/gaexhause.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.3/gaexhause.m -------------------------------------------------------------------------------- /第11章/例11.3/optifun20.m: -------------------------------------------------------------------------------- 1 | function y=optifun20(x) 2 | y=-x^2+2*x+0.5; -------------------------------------------------------------------------------- /第11章/例11.3/例11.3.txt: -------------------------------------------------------------------------------- 1 | [MinValue,MinFounction]=gaexhause(@optifun20,-10,10,0.001) -------------------------------------------------------------------------------- /第11章/例11.4/optifun18.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.4/optifun18.m -------------------------------------------------------------------------------- /第11章/例11.4/例11.4.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.4/例11.4.txt -------------------------------------------------------------------------------- /第11章/例11.5/optifun19.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.5/optifun19.m -------------------------------------------------------------------------------- /第11章/例11.5/press.m: -------------------------------------------------------------------------------- 1 | function [y1,b]=press(x,y) 2 | x1=[ones(length(y),1) x]; 3 | [b,bint,r]=regress(y,x1,0.01); 4 | y1=0; 5 | for i=1:length(y) 6 | hi(i)=x1(i,:)*inv(x1'*x1)*x1(i,:)'; 7 | y1=y1+(r(i)/(1-hi(i)))^2; 8 | end 9 | -------------------------------------------------------------------------------- /第11章/例11.5/例11.5.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.5/例11.5.txt -------------------------------------------------------------------------------- /第11章/例11.6/GAJSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.6/GAJSP.m -------------------------------------------------------------------------------- /第11章/例11.6/例11.6.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | NIND=40;MAXGEN=50;GGAP = 0.9;P_Cross = 0.8;P_Mutation = 0.6; 3 | Jm={3,2,1;1,3,2;2,3,1;1,2,1;3,1,2;}; 4 | T={27,8,10;6,10,5;14,10,3;25,20,16;5,12,28;}; 5 | MakeSpan =GAJSP(NIND,MAXGEN,GGAP,P_Cross,P_Mutation,Jm,T); 6 | 7 | 8 | Jm={[3,10],1,2,[4,7],[6,8],5;2,3,[5,8],[6,7],1,[4,10];[3,9],[4,7],[6,8],1,[2,10],5;... 9 | 4,[1,9],[3,7],[2,8],5,6;5,[2,7],[3,10],[6,9],1,[4,8];2,[4,7],[6,9],1,[5,8],3;}; 10 | T = {[3,5],10,9,[5,4],[3,3],10;6,8,[1,4],[5,6],3,[3,3];[1,4],[5,7],[5,6],5,[9,11],1;... 11 | 7,[4,3],[4,6],[3,5],1,3;6,[10,12],[7,9],[8,8],5,[4,7];2,[4,7],[6,9],1,[5,8],3;}; 12 | MakeSpan =GAJSP(NIND,MAXGEN,GGAP,P_Cross,P_Mutation,Jm,T); -------------------------------------------------------------------------------- /第11章/例11.7/gaEP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.7/gaEP.m -------------------------------------------------------------------------------- /第11章/例11.7/optifun21.m: -------------------------------------------------------------------------------- 1 | function y=optifun21(x) 2 | y=0; 3 | for i=1:length(x) 4 | y=y+x(i)^2-10*cos(2*pi*x(i))+10; 5 | end -------------------------------------------------------------------------------- /第11章/例11.7/例11.7.txt: -------------------------------------------------------------------------------- 1 | [minx,minf]=gaEP(@optifun21,100,500,-5.12*ones(10,1),5.12*ones(10,1),3) -------------------------------------------------------------------------------- /第11章/例11.8/gaEP1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.8/gaEP1.m -------------------------------------------------------------------------------- /第11章/例11.8/optifun22.m: -------------------------------------------------------------------------------- 1 | function y=optifun22(x) 2 | c=[160 87 18 71 176 101 35 145 117 54]; 3 | a=[198 30 167 130 35 20 105 196 94 126]; 4 | b=546; 5 | a1=find(x==1); 6 | if sum(a(a1))>b 7 | y=-1; 8 | else 9 | y=sum(c(a1)); 10 | end 11 | -------------------------------------------------------------------------------- /第11章/例11.8/例11.8.txt: -------------------------------------------------------------------------------- 1 | [maxx,maxf]=gaEP1(@optifun22,200,500) -------------------------------------------------------------------------------- /第11章/例11.9/gaES.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第11章/例11.9/gaES.m -------------------------------------------------------------------------------- /第11章/例11.9/optifun23.m: -------------------------------------------------------------------------------- 1 | function y=optifun23(x) 2 | y=200-(x(1)^2+x(2)-11)^2-(x(2)^2+x(1)-7)^2; 3 | y=-y; 4 | -------------------------------------------------------------------------------- /第11章/例11.9/例11.9.txt: -------------------------------------------------------------------------------- 1 | for i=1:20 2 | [val_x(i,:),val_f(i)]=gaES(@optifun23,300,500,-5.12*ones(2,1),5.12*ones(2,1)); 3 | end 4 | 5 | -------------------------------------------------------------------------------- /第12章/例12.1/MainAnealTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.1/MainAnealTSP.m -------------------------------------------------------------------------------- /第12章/例12.1/TSPplot.m: -------------------------------------------------------------------------------- 1 | function y=TSPplot(city,route) 2 | n=length(route); 3 | plot(city(:,1),city(:,2),'o'); 4 | hold on 5 | for i=1:n+1 6 | if i<=n 7 | x(i)=city(route(i),1); 8 | y(i)=city(route(i),2); 9 | else 10 | x(i)=city(route(1),1); 11 | y(i)=city(route(1),2); 12 | end 13 | end 14 | y=plot(x,y,'o-'); 15 | if n<=30 16 | gname; 17 | end -------------------------------------------------------------------------------- /第12章/例12.1/exchange.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.1/exchange.m -------------------------------------------------------------------------------- /第12章/例12.1/matrixinsert.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.1/matrixinsert.m -------------------------------------------------------------------------------- /第12章/例12.1/例12.1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.1/例12.1.txt -------------------------------------------------------------------------------- /第12章/例12.2/MainAneal.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.2/MainAneal.m -------------------------------------------------------------------------------- /第12章/例12.2/optifun28.m: -------------------------------------------------------------------------------- 1 | function y=optifun28(x) 2 | a=0;b=0; 3 | for i=1:5 4 | a=a+i*cos((i+1)*x(1)+i); 5 | b=b+i*cos((i+1)*x(2)+i); 6 | end 7 | y=a*b; 8 | -------------------------------------------------------------------------------- /第12章/例12.2/例12.2.txt: -------------------------------------------------------------------------------- 1 | LB=[-10;-10];UB=[10;10]; 2 | [best_fval,best_x]=MainAneal(@optifun28,LB,UB) 3 | -------------------------------------------------------------------------------- /第12章/例12.3/optifun29.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.3/optifun29.m -------------------------------------------------------------------------------- /第12章/例12.3/例12.3.txt: -------------------------------------------------------------------------------- 1 | LB=[0;0;0];UB=[100;4;1]; 2 | [best_fval,best_x]=MainAneal(@optifun29,LB,UB) 3 | -------------------------------------------------------------------------------- /第12章/例12.4/cluster_dis.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.4/cluster_dis.m -------------------------------------------------------------------------------- /第12章/例12.4/fy.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.4/fy.m -------------------------------------------------------------------------------- /第12章/例12.4/guiyi_range.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.4/guiyi_range.m -------------------------------------------------------------------------------- /第12章/例12.4/myclass.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.4/myclass.m -------------------------------------------------------------------------------- /第12章/例12.4/optifun33.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第12章/例12.4/optifun33.m -------------------------------------------------------------------------------- /第12章/例12.4/例12.4.txt: -------------------------------------------------------------------------------- 1 | x=[6.1700 46.9100 1.0000 11.0000 24.0000 53.7800 1.5000 2.3000 291.2800 2.0000 2 | 6.2000 43.1100 1.1000 8.0000 130.0000 50.3600 1.5000 1.9100 243.0000 2.0000 3 | 6.2000 38.9000 3.0000 13.5000 27.0000 49.4900 1.5000 1.4500 369.5000 2.0000 4 | 6.2000 38.9000 1.2000 13.5000 7.0000 49.4900 1.5000 1.5000 369.5000 2.0000 5 | 6.2000 38.9000 1.2000 14.0000 30.0000 49.4900 1.5000 1.7800 369.5000 2.0000 6 | 520.0000 65.0000 79.0000 11.0000 63.0000 53.2600 7.5000 0.7400 175.5000 0.5000 7 | 520.0000 50.0000 15.0000 14.0000 15.0000 64.3600 8.0000 1.3700 218.8000 0.5000 8 | 520.0000 46.0000 2.5000 15.0000 9.0000 64.3600 8.0000 1.4500 230.0000 0.5000 9 | 520.0000 45.0000 68.0000 14.0000 75.0000 64.3600 8.0000 1.0100 187.5000 0.5000 10 | 7.9800 28.0000 0.6000 18.0000 10.0000 64.3600 8.0000 2.0100 344.0000 0.5000 11 | 520.0000 43.0000 1.5000 11.0000 2.0000 64.3600 8.0000 1.9100 295.4000 2.0000 12 | 520.0000 50.0000 4.0000 13.0000 10.0000 63.9700 8.0000 2.5500 412.4000 2.0000 13 | 520.0000 50.0000 100.0000 16.0000 153.0000 64.3600 2.0000 2.3500 369.1000 1.0000 14 | 520.0000 42.0000 32.0000 12.0000 19.0000 66.5500 7.5000 0.6900 152.0000 0.5000]; 15 | data=guiyi_range(x,[0 1]); 16 | LB=[0 0;0 0;0 0;0 0;0 0;0 0;0 0;0 0;0 0;0 0];UB=[1 1;1 1;1 1;1 1;1 1;1 1;1 1;1 1;1 1;1 1]; 17 | [best_fval,best_x]=MainAneal(@(x)optifun33(x,data),LB,UB) 18 | y=myclass(data,best_x) 19 | -------------------------------------------------------------------------------- /第13章/例13.1/TSOATSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.1/TSOATSP.m -------------------------------------------------------------------------------- /第13章/例13.1/例13.1.txt: -------------------------------------------------------------------------------- 1 | city=[41 94;37 84;54 67;25 62;7 64;2 99;68 58;71 44;54 62;83 69;64 60;18 54; 2 | 22 60;83 46;91 38;25 38;24 42;58 69;71 71;74 78;87 76;18 40;13 40;82 7; 3 | 62 32;58 35;45 21;41 26;44 35;4 50]; 4 | cBest=TSOATSP(city); 5 | gname -------------------------------------------------------------------------------- /第13章/例13.2/TSOA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.2/TSOA.m -------------------------------------------------------------------------------- /第13章/例13.2/optifun13.m: -------------------------------------------------------------------------------- 1 | function y=optifun13(x) 2 | y=(sin(sqrt(x(1)^2+x(2)^2))^2-0.5)/(1+0.001*(x(1)^2+x(2)^2))^2-0.5; 3 | -------------------------------------------------------------------------------- /第13章/例13.2/selectCandidate.m: -------------------------------------------------------------------------------- 1 | function Candidate=selectCandidate(v,Candidate_Num,varnum,iterm,iterm_max,LB,UB,pm,type) 2 | for i=1:Candidate_Num 3 | Candidate(i).x=v; 4 | if type==1 5 | for j=1:varnum 6 | if rand>pm 7 | v1=v(j)-UB(j); 8 | v2=LB(j)-v(j); 9 | fg=rand*(1-iterm/iterm_max)^2; 10 | if rand>0.5 11 | Candidate(i).x(j)=v(j)+v1*fg; 12 | else 13 | Candidate(i).x(j)=v(j)+v2*fg; 14 | end 15 | else 16 | Candidate(i).x(j)=v(j)+rand*(UB(j)-LB(j)); 17 | end 18 | if Candidate(i).x(j)>=UB(j) 19 | Candidate(i).x(j)=LB(j)+(Candidate(i).x(j)-UB(j)); 20 | elseif Candidate(i).x(j)<=LB(j) 21 | Candidate(i).x(j)=UB(j)-(LB(j)-Candidate(i).x(j)); 22 | end 23 | end 24 | elseif type==2 25 | b=ceil(rand*varnum); 26 | Candidate(i).x(b)=LB(b)+rand*(UB(b)-LB(b)); 27 | if Candidate(i).x(b)>=UB(b) 28 | Candidate(i).x(b)=LB(b)+(Candidate(i).x(b)-UB(b)); 29 | elseif Candidate(i).x(b)<=LB(b) 30 | Candidate(i).x(b)=UB(b)-(LB(b)-Candidate(i).x(b)); 31 | end 32 | end 33 | end 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /第13章/例13.2/例13.2.txt: -------------------------------------------------------------------------------- 1 | cBest=TSOA(@optifun13,2,[-10;-10],[10;10]); 2 | -------------------------------------------------------------------------------- /第13章/例13.3/TSOA1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/TSOA1.m -------------------------------------------------------------------------------- /第13章/例13.3/cluster_center.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/cluster_center.m -------------------------------------------------------------------------------- /第13章/例13.3/cluster_dis.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/cluster_dis.m -------------------------------------------------------------------------------- /第13章/例13.3/correct.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/correct.m -------------------------------------------------------------------------------- /第13章/例13.3/crossvalindtest.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/crossvalindtest.m -------------------------------------------------------------------------------- /第13章/例13.3/guiyi.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/guiyi.m -------------------------------------------------------------------------------- /第13章/例13.3/optifun40.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第13章/例13.3/optifun40.m -------------------------------------------------------------------------------- /第13章/例13.3/例13.3.txt: -------------------------------------------------------------------------------- 1 | data=[1.0500 6.4500 0.9000 1.5000 2.7700 0.6300 2.0800 1.0000 1.0000 2.0000 0.9700 0.3500 2 | 0.7500 6.4500 0.8000 1.5000 3.8000 0.5000 2.0800 1.0000 1.0000 2.0000 1.3300 0.3500 3 | 0.6800 6.4500 0.8000 1.5000 4.2000 0.4700 2.0800 1.0000 1.0000 2.0000 1.4700 0.3500 4 | 0.7500 3.0800 0.8000 1.2000 2.0100 0.5000 1.4500 1.0000 1.0000 2.0000 1.3000 0.6600 5 | 1.0500 3.0800 0.9000 1.2000 1.4700 0.6300 1.4500 1.0000 1.0000 2.0000 0.9700 0.6600 6 | 0.5900 3.0800 0.7500 1.2000 2.5300 0.4300 1.4500 1.0000 1.0000 2.0000 0.1700 0.6600 7 | 1.4800 6.4500 0.9000 1.5000 3.8300 1.0200 2.0800 1.0000 2.0000 2.0000 1.3400 0.3500 8 | 0.7500 6.0000 0.8000 1.5000 2.6100 0.5000 2.3800 1.0000 1.0000 3.0000 1.3300 0.5100 9 | 1.0500 6.0000 0.9000 1.5000 1.9000 0.6300 2.6800 1.0000 1.0000 3.0000 0.9700 0.5100 10 | 0.5900 6.0000 0.7500 1.5000 3.2700 0.4300 2.3800 1.0000 1.0000 3.0000 1.6700 0.5100 11 | 1.7700 6.0000 1.0000 1.5000 2.2000 1.0500 2.3800 1.0000 2.0000 3.0000 1.1200 0.5100 12 | 1.4800 6.0000 0.9000 1.5000 2.6300 1.0200 2.3800 1.0000 2.0000 3.0000 1.3400 0.5100 13 | 1.7700 4.8400 1.0000 1.6000 1.8100 1.0500 2.4000 1.0000 2.0000 3.0000 1.1200 0.6200 14 | 0.6800 3.7000 0.8000 1.7000 1.8100 0.4700 2.0500 2.0000 1.0000 3.0000 1.4700 0.8100 15 | 1.7700 9.7700 1.0000 1.9000 2.6700 1.0500 3.4200 1.0000 2.0000 4.0000 1.3400 0.4200 16 | 1.4800 9.7700 0.9000 1.9000 3.1900 1.0200 3.4200 1.0000 2.0000 4.0000 1.3400 0.4200 17 | 0.7500 3.2300 0.8000 1.2000 1.4600 0.5000 1.8600 1.0000 1.0000 3.0000 1.3300 0.9200 18 | 1.4800 3.2300 0.9000 1.2000 1.4600 1.0200 1.8600 1.0000 2.0000 3.0000 1.3400 0.9100 19 | 1.4800 2.8000 0.9000 1.3000 1.1900 1.0200 1.8300 1.0000 2.0000 3.0000 1.3400 1.1300 20 | 1.4800 2.9700 0.9000 1.3000 1.3100 1.0200 1.8900 1.0000 2.0000 3.0000 1.3400 1.0200 21 | 1.5700 1.8200 1.6000 1.7000 1.1800 1.3300 1.4200 1.0000 2.0000 2.0000 0.8400 0.7100 22 | 1.7700 1.8200 1.0000 1.7000 1.5800 1.0200 1.4300 1.0000 2.0000 2.0000 1.3400 0.7100 23 | 2.5600 6.0000 1.4000 1.5000 1.0200 1.6800 2.3800 1.0000 2.0000 3.0000 0.5200 0.5100 24 | 2.5600 4.6900 1.4000 1.4000 0.8800 1.6800 2.4800 1.0000 2.0000 3.0000 0.5200 0.5900 25 | 0.7500 2.0600 0.8000 1.7000 1.3700 0.5000 1.4000 1.0000 1.0000 2.0000 1.3300 0.9700 26 | 0.6300 2.0600 1.4000 1.7000 0.9800 0.6700 1.4000 1.0000 1.0000 2.0000 0.9500 0.9700 27 | 1.4800 2.6300 0.9000 1.2000 1.1400 1.0200 1.8500 1.0000 2.0000 3.0000 1.3400 1.1800 28 | 1.4800 2.7300 0.9000 1.3000 1.1700 1.0200 1.8500 1.0000 2.0000 3.0000 1.3400 1.1500 29 | 1.4800 2.8000 0.9000 1.3000 1.1900 1.0200 1.8300 1.0000 2.0000 3.0000 1.3400 1.1300 30 | 1.4800 3.0000 0.9000 1.3000 1.2500 1.0200 1.9000 1.0000 2.0000 3.0000 1.3400 1.0700 31 | 1.4800 3.1300 0.9000 1.3000 1.3300 1.0200 1.9400 1.0000 2.0000 3.0000 1.3400 1.0100 32 | 1.4800 3.1600 0.9000 1.2000 1.3400 1.0200 1.7600 1.0000 2.0000 2.0000 1.3400 1.0000 33 | 1.4800 2.6800 0.9000 1.2000 1.1600 1.0200 1.8500 2.0000 2.0000 3.0000 1.3400 1.1600 34 | 1.4800 2.8600 0.9000 1.2000 1.2000 1.0200 1.8500 2.0000 2.0000 3.0000 1.3400 1.1200 35 | 1.4800 3.0000 0.9000 1.3000 1.2500 1.0200 1.9000 2.0000 2.0000 3.0000 1.3400 1.0700 36 | 1.4800 3.1300 0.9000 1.3000 1.3300 1.0200 1.9400 2.0000 2.0000 3.0000 1.3400 1.0100]; 37 | data=guiyi(data); 38 | target=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]'; 39 | cBest=TSOA1(@(x)optifun40(x,data,target)) -------------------------------------------------------------------------------- /第14章/例14.1/antTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.1/antTSP.m -------------------------------------------------------------------------------- /第14章/例14.1/例14.1.txt: -------------------------------------------------------------------------------- 1 | city=[48 21;52 26;55 50;50 50;41 46;51 42;55 45;38 33;33 34;45 35; 2 | 40 37;50 30;55 34;54 38;26 13;15 5;21 48;29 39;33 44;15 14; 3 | 16 19;12 17;50 40;22 53;21 36;20 30;26 29;40 20;36 26;62 48; 4 | 67 41;62 35;65 27;62 24;55 20;35 51;30 50;45 42;21 45;36 6; 5 | 6 25;11 28;26 59;30 60;22 22;27 24;30 20;35 16;54 10;50 15; 6 | 44 13;35 60;40 60;40 66;31 76;47 66;50 70;57 72;55 65;2 38; 7 | 7 43;9 56;15 56;10 70;17 64;55 57;62 57;70 64;64 4;59 5' 8 | 50 4;60 15;66 14;66 8;43 26]; 9 | [Shortest_Route,Shortest_Length]=antTSP(city,1000,50,1,2,0.1,100) -------------------------------------------------------------------------------- /第14章/例14.2/antmin.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.2/antmin.m -------------------------------------------------------------------------------- /第14章/例14.2/optifun31.m: -------------------------------------------------------------------------------- 1 | function y=optifun31(x) 2 | y=-abs((1-x)*(x^2)*sin(200*pi*x)); -------------------------------------------------------------------------------- /第14章/例14.2/optifun32.m: -------------------------------------------------------------------------------- 1 | function y=optifun32(x) 2 | y=-x(1)/(1+abs(x(2))); 3 | %y=-((x(1)-1)^2+(x(2)-2.2)^2+1); -------------------------------------------------------------------------------- /第14章/例14.2/例14.2.txt: -------------------------------------------------------------------------------- 1 | [x_best1,y_best1]=antmin(@optifun31,1) 2 | [x_best2,y_best2]=antmin(@optifun32,2) -------------------------------------------------------------------------------- /第14章/例14.3/antcluster.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.3/antcluster.m -------------------------------------------------------------------------------- /第14章/例14.3/center_dis.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.3/center_dis.m -------------------------------------------------------------------------------- /第14章/例14.3/cluster_center.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.3/cluster_center.m -------------------------------------------------------------------------------- /第14章/例14.3/cluster_dis.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.3/cluster_dis.m -------------------------------------------------------------------------------- /第14章/例14.3/guiyi.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第14章/例14.3/guiyi.m -------------------------------------------------------------------------------- /第14章/例14.3/例14.3.txt: -------------------------------------------------------------------------------- 1 | data=[11.8530 0.4800 14.3600 25.2100 2 | 3.6810 0.3270 13.5700 25.1200 3 | 48.2870 0.3860 14.5000 25.9000 4 | 4.7410 0.1400 6.9000 15.7000 5 | 4.2230 0.3400 3.8000 7.1000 6 | 6.4420 0.1900 4.7000 9.1000 7 | 16.2340 0.3900 3.4000 5.4000 8 | 10.5850 0.4200 2.4000 4.7000 9 | 48.6210 0.0820 2.0570 3.8470 10 | 288.1490 0.1480 1.7630 2.9680 11 | 316.6040 0.3170 1.4530 2.4320 12 | 307.3100 0.1730 1.6270 2.7290 13 | 82.1700 0.1050 1.2170 2.1880 14 | 3.7770 0.8700 15.4000 28.2000 15 | 62.8560 0.3400 5.2000 9.0000 16 | 3.2990 0.1800 3.0000 5.2000]; 17 | [pattern_best1,d_best1]=antcluster(data,1); 18 | [pattern_best2,d_best2]=antcluster(data,2); -------------------------------------------------------------------------------- /第15章/例15.1/mypso.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第15章/例15.1/mypso.m -------------------------------------------------------------------------------- /第15章/例15.1/optifun35.m: -------------------------------------------------------------------------------- 1 | function y=optifun35(x) 2 | a=[0.1957 0.1947 0.1735 0.16 0.0844 0.0627 0.0456 0.0342 0.0323 0.0235 0.0246]; 3 | b=[0.25 0.5 1 2 4 6 8 10 12 14 16]; 4 | y=0; 5 | for i=1:11 6 | y=y+(a(i)-(x(1)*(1+b(i)*x(2)))/(1+x(3)*b(i)+x(4)*b(i)^2))^2; 7 | end -------------------------------------------------------------------------------- /第15章/例15.1/例15.1.txt: -------------------------------------------------------------------------------- 1 | [bestx,bestf]=mypso(@optifun35,1) -------------------------------------------------------------------------------- /第15章/例15.2/mypsoTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第15章/例15.2/mypsoTSP.m -------------------------------------------------------------------------------- /第15章/例15.2/例15.2.txt: -------------------------------------------------------------------------------- 1 | city=[15.2000 3.0000 2 | 10.0000 25.0000 3 | 360.0000 20.0000 4 | 50.0000 6.0000 5 | 46.0000 92.0000 6 | 50.3000 70.6000 7 | 90.5400 658.7000 8 | 100.0000 360.0000 9 | 154.0000 82.0000 10 | 79.0000 659.0000 11 | 360.4000 258.1000 12 | 39.4000 56.8000 13 | 99.5000 887.0000 14 | 65.0000 68.4000 15 | 302.4000 54.0000 16 | 58.6800 78.0000 17 | 98.3600 65.6000 18 | 100.2000 200.3000 19 | 87.0000 6.0000 20 | 65.9000 12.3000]; 21 | [bestx,bestf]=mypsoTSP(city) -------------------------------------------------------------------------------- /第15章/例15.3/optifun37.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第15章/例15.3/optifun37.m -------------------------------------------------------------------------------- /第15章/例15.3/例15.3.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | data=[0.5506 0.4845 0.4412 0.4114 0.3892 0.3722 0.3609 0.3572 3 | 0.3796 0.3334 0.3061 0.2920 0.2875 0.2905 0.2998 0.3150 4 | 0.1194 0.1159 0.1146 0.1150 0.1167 0.1193 0.1226 0.1263 5 | 0.1140 0.1120 0.1111 0.1097 0.1071 0.1035 0.0991 0.0949 6 | 0.1893 0.1951 0.2050 0.2182 0.2327 0.2446 0.2500 0.2478 7 | 1.6467 1.4927 1.3725 1.2952 1.2648 1.2843 1.3587 1.4956 8 | 0.6381 0.5683 0.5145 0.4727 0.4404 0.4168 0.4022 0.3967 9 | 1.1603 1.0765 0.9714 0.8523 0.7231 0.6015 0.5029 0.4309 10 | 0.9099 0.8653 0.8214 0.7822 0.7498 0.7210 0.6913 0.6590 11 | 2.5992 1.7953 1.3344 1.0749 0.9298 0.8566 0.8360 0.8607 12 | 10.3278 5.3582 4.4362 3.6904 3.1263 2.7111 2.4125 2.2056 13 | 3.0437 2.9108 2.7801 2.6114 2.4126 2.2335 2.1051 2.0338 14 | 2.6691 2.3974 2.2283 2.1057 1.9711 1.8110 1.6618 1.5543 15 | 10.2730 10.1025 5.9994 10.0321 10.2416 10.6632 7.3412 8.3367 16 | 4.4999 5.0419 5.6885 10.2655 10.5334 10.3907 5.9740 5.3950]; 17 | data=guiyi(data);target=[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]'; 18 | [bestx,bestf]=mypso(@(x)optifun37(x,data,target),2) -------------------------------------------------------------------------------- /第15章/例15.4/COApso.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第15章/例15.4/COApso.m -------------------------------------------------------------------------------- /第15章/例15.4/createCOA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第15章/例15.4/createCOA.m -------------------------------------------------------------------------------- /第15章/例15.4/optifun38.m: -------------------------------------------------------------------------------- 1 | function y=optifun38(x) 2 | y=(3/(0.05+(x(1)^2+x(2)^2)))^2+(x(1)^2+x(2)^2)^2; 3 | y=-y; -------------------------------------------------------------------------------- /第15章/例15.4/例15.4.txt: -------------------------------------------------------------------------------- 1 | [bestx,bestf]=COApso(@optifun38,1) -------------------------------------------------------------------------------- /第15章/例15.5/例15.5.txt: -------------------------------------------------------------------------------- 1 | options = optimoptions('particleswarm','SwarmSize',100,'HybridFcn',@fmincon); 2 | [x,fval]=particleswarm(@optifun39,2,[-2.048:-2.048],[2.048;2.048],options); 3 | -------------------------------------------------------------------------------- /第16章/例16.1/fish.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.1/fish.m -------------------------------------------------------------------------------- /第16章/例16.1/optifun41.m: -------------------------------------------------------------------------------- 1 | function y=optifun41(x) 2 | y=cos(2*pi*x(1))*cos(2*pi*x(2))*exp(-(x(1)^2+x(2)^2)/10); -------------------------------------------------------------------------------- /第16章/例16.1/例16.1.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=fish(@optifun41) -------------------------------------------------------------------------------- /第16章/例16.2/fishdstc.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/fishdstc.m -------------------------------------------------------------------------------- /第16章/例16.2/fishevaluate.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/fishevaluate.m -------------------------------------------------------------------------------- /第16章/例16.2/fishfollow.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/fishfollow.m -------------------------------------------------------------------------------- /第16章/例16.2/fishmove.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/fishmove.m -------------------------------------------------------------------------------- /第16章/例16.2/fishprey.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/fishprey.m -------------------------------------------------------------------------------- /第16章/例16.2/fishswarm.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/fishswarm.m -------------------------------------------------------------------------------- /第16章/例16.2/newfish.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.2/newfish.m -------------------------------------------------------------------------------- /第16章/例16.2/optifun42.m: -------------------------------------------------------------------------------- 1 | function y=optifun42(x) 2 | y=-x(1)*sin(sqrt(abs(x(1))))-x(2)*sin(sqrt(abs(x(2)))); 3 | 4 | 5 | -------------------------------------------------------------------------------- /第16章/例16.2/例16.2.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=newfish(@optifun42,30,100,[-500;-500],[500;500],15,50,8) 2 | -------------------------------------------------------------------------------- /第16章/例16.3/fishTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.3/fishTSP.m -------------------------------------------------------------------------------- /第16章/例16.3/例16.3.txt: -------------------------------------------------------------------------------- 1 | city=[16.47 16.47 20.09 22.39 25.23 22.00 20.47 17.20 16.30 14.05 16.35 21.52 19.41 20.09 2 | 96.10 94.44 92.54 93.37 97.24 96.05 97.02 96.29 97.38 98.12 97.38 95.59 97.13 94.55]'; 3 | [best_route,fval]=fishTSP(city,30,100,10,30,2) -------------------------------------------------------------------------------- /第16章/例16.4/例16.4.txt: -------------------------------------------------------------------------------- 1 | format long 2 | x0=0;xi=1e-5.*rand(1,20)+x0; 3 | [best_x,fval]=newfish(@(x)optifun43(x,xi,0,1),30,500,-4,4,30,100,8) -------------------------------------------------------------------------------- /第16章/例16.5/Dec.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.5/Dec.m -------------------------------------------------------------------------------- /第16章/例16.5/binfish.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.5/binfish.m -------------------------------------------------------------------------------- /第16章/例16.5/fishevaluate3_1.m: -------------------------------------------------------------------------------- 1 | function afish=fishevaluate3_1(fun,afish0,afish1,parameter) 2 | af_follow=fishfollow3_1(fun,afish0,afish1,parameter); 3 | af_swarm=fishswarm3_1(fun,afish0,afish1,parameter); 4 | af_prey=fishprey3_1(fun,afish0,parameter); 5 | afish2=af_follow; 6 | if fun(af_swarm)f_max 11 | f_max=f; 12 | max_i=i; 13 | end 14 | end 15 | end 16 | if (f_max/n>parameter.delta*fun(afish0)) 17 | afish=afish0; 18 | m=find((afish1(max_i,:)-afish0)~=0); 19 | n=randperm(length(m)); 20 | if length(m)>parameter.step 21 | n=n(1:parameter.step); 22 | end 23 | afish(m(n))=afish1(max_i,m(n)); 24 | return 25 | else 26 | afish=fishprey3_1(fun,afish0,parameter); 27 | end 28 | -------------------------------------------------------------------------------- /第16章/例16.5/fishmove3_1.m: -------------------------------------------------------------------------------- 1 | function afish=fishmove3_1(afish0,parameter) 2 | NC=length(afish0); 3 | afish=afish0; 4 | if rand<0.8 5 | m=ceil(NC*rand(1,parameter.step)); 6 | afish(m)=abs(afish0(m)-1); 7 | else 8 | afish=afish0; 9 | end -------------------------------------------------------------------------------- /第16章/例16.5/fishprey3_1.m: -------------------------------------------------------------------------------- 1 | function afish=fishprey3_1(fun,afish0,parameter) 2 | NC=length(afish0); 3 | for i=1:parameter.try_number 4 | afish1=afish0; 5 | afish=afish0; 6 | n=randperm(NC); 7 | n=n(1:parameter.visual); 8 | afish1(n)=abs(afish0(n)-1); 9 | m=randperm(parameter.visual); 10 | if length(m)>parameter.step 11 | m=m(1:parameter.step); 12 | end 13 | y1=fun(afish1); 14 | y2=fun(afish0); 15 | if y1>y2 16 | afish(n(m))=afish1(n(m)); 17 | return 18 | end 19 | end 20 | afish=fishmove3_1(afish0,parameter); -------------------------------------------------------------------------------- /第16章/例16.5/fishswarm3_1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第16章/例16.5/fishswarm3_1.m -------------------------------------------------------------------------------- /第16章/例16.5/optifun15_1.m: -------------------------------------------------------------------------------- 1 | function y=optifun15_1(x,LB,UB,CodeLen) 2 | temp=Dec(LB,UB,x,CodeLen); 3 | y=sin(temp(1))*sin(temp(2))/temp(1)/temp(2); -------------------------------------------------------------------------------- /第16章/例16.5/例16.5.txt: -------------------------------------------------------------------------------- 1 | LB=[-10;-10];UB=[10;10]; 2 | eps1=1e-8;nvar=2; 3 | CodeLen=nvar*max(ceil(log2((UB-LB)./eps1 + 1))); 4 | [best_x,fval]=binfish(@(x)optifun15_1(x,LB,UB,CodeLen),30,[-10;-10],[10;10],2,1000,8,4,10,3) 5 | 6 | -------------------------------------------------------------------------------- /第16章/例16.6/optifun45.m: -------------------------------------------------------------------------------- 1 | function y=optifun45(x) 2 | I=[0.10 0.30 0.50 1 3 | 0.17 0.33 0.50 1 4 | 0.32 0.48 0.48 1 5 | 0.12 0.30 0.50 1 6 | 0.10 0.30 0.50 1]; 7 | PI0=[0.2564 0.3509 0.4803 0.900]; 8 | for i=1:4 9 | y1=0; 10 | for j=1:5 11 | y1=y1+I(j,i); 12 | end 13 | y1=y1/5; 14 | y=(PI0(i)-x(1)*y1^x(2))^2; 15 | end 16 | y=-y; 17 | 18 | -------------------------------------------------------------------------------- /第16章/例16.6/例16.6.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=newfish(@optifun45,30,1000,[0;0],[1;1],10,30,0.1) -------------------------------------------------------------------------------- /第17章/例17.1/SFLA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第17章/例17.1/SFLA.m -------------------------------------------------------------------------------- /第17章/例17.1/optifun46.m: -------------------------------------------------------------------------------- 1 | function y=optifun46(x) 2 | y=4*x(1)^2-2.1*x(1)^4+x(1)^6/3+x(1)*x(2)-4*x(2)^2+4*x(2)^4; -------------------------------------------------------------------------------- /第17章/例17.1/例17.1.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=SFLA(@optifun46,[-5;-5],[5;5],40,4,2000,30) -------------------------------------------------------------------------------- /第17章/例17.2/SFLATSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第17章/例17.2/SFLATSP.m -------------------------------------------------------------------------------- /第17章/例17.2/例17.2.txt: -------------------------------------------------------------------------------- 1 | city =[16.4700 96.1000 2 | 16.4700 94.4400 3 | 20.0900 92.5400 4 | 22.3900 93.3700 5 | 25.2300 97.2400 6 | 22.0000 96.0500 7 | 20.4700 97.0200 8 | 17.2000 96.2900 9 | 16.3000 97.3800 10 | 14.0500 98.1200 11 | 16.3500 97.3800 12 | 21.5200 95.5900 13 | 19.4100 97.1300 14 | 20.0900 94.5500]; 15 | [best_x,fval]=SFLATSP(city,40,5,200,30,0.02) -------------------------------------------------------------------------------- /第17章/例17.3/SFLAcluster.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第17章/例17.3/SFLAcluster.m -------------------------------------------------------------------------------- /第17章/例17.3/optifun33.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第17章/例17.3/optifun33.m -------------------------------------------------------------------------------- /第17章/例17.3/例17.3.txt: -------------------------------------------------------------------------------- 1 | x=[-0.5370 -0.0669 -0.7694 -0.1289 2 | -0.5513 -0.3345 0.2463 0.5991 3 | 1.1903 -0.0372 1.5112 1.0693 4 | 0.2798 1.6874 0.8073 -2.1613 5 | -1.7413 -0.7508 -0.6960 0.4474 6 | -0.6296 -0.8994 -0.4760 1.2209 7 | 1.7647 0.7656 -1.0077 -0.9783 8 | 0.0998 2.0793 1.8412 -0.6294 9 | -0.9102 -0.8697 -0.1754 0.9479 10 | -0.5370 -0.8102 -1.3744 -0.8569 11 | 0.7245 -0.5724 -0.3844 0.1289 12 | 0.8473 -0.1858 0.4773 0.3413]; 13 | data=guiyi_range(x,[0 1]); 14 | LB=[0 0 0 0;0 0 0 0;0 0 0 0;0 0 0 0];UB=[1 1 1 1;1 1 1 1;1 1 1 1;1 1 1 1]; 15 | [class,fval]=SFLAcluster(@(x)optifun33(x,data),100,10,500,30,LB,UB,data) -------------------------------------------------------------------------------- /第17章/例17.4/bitSFLA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第17章/例17.4/bitSFLA.m -------------------------------------------------------------------------------- /第17章/例17.4/optifun47.m: -------------------------------------------------------------------------------- 1 | function y=optifun47(x) 2 | %V=878; 3 | %V=700; 4 | %weight=[44 46 90 72 91 40 75 35 8 54 78 40 77 15 61 17 75 29 75 63]; 5 | %weight=[56 20 54 42 15]; 6 | %weight=[13 18 21 29 11 28 19]; 7 | %profit=[92 4 43 83 84 68 92 82 6 44 32 18 56 83 25 96 70 48 14 58]; 8 | %profit=[7 5 9 6 3]; 9 | %profit=[21 25 27 37 19 33 25]; 10 | %y=-profit*x'+8000*((min(0,V-weight*x'))^2+(min(0,2-x(1)-x(2)-x(3)))^2+(min(0,x(4)+x(5)-1))^2+(min(0,x(6)+x(7)-1))^2); 11 | y=3*x(1)^5+4*x(1)*x(3)+2*x(2)^3-7*x(4)*x(5)*x(6)+2*x(4)^2+5*x(5)^3+... 12 | 8*x(6)^2-4*x(1)*x(2)-3*x(1)*x(3)+8000*((min(0,4*x(1)+3*x(2)+2*x(3)+3*x(4)+4*x(5)+6*x(6)-14))^2+... 13 | (min(0,3*x(1)+2*x(2)+3*x(3)+4*x(4)+2*x(5)+3*x(6)-10))^2); 14 | 15 | 16 | -------------------------------------------------------------------------------- /第17章/例17.4/例17.4.txt: -------------------------------------------------------------------------------- 1 | [bestx,fval]=bitSFLA(@optifun47,100,20,500,50,6) -------------------------------------------------------------------------------- /第18章/例18.1/Dec1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.1/Dec1.m -------------------------------------------------------------------------------- /第18章/例18.1/qGA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.1/qGA.m -------------------------------------------------------------------------------- /第18章/例18.1/例18.1.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=qGA(@optifun48) -------------------------------------------------------------------------------- /第18章/例18.2/niching.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.2/niching.m -------------------------------------------------------------------------------- /第18章/例18.2/optifun49.m: -------------------------------------------------------------------------------- 1 | function y=optifun49(x) 2 | y=(x(2)-5.1*x(1)^2/(4*pi^2)+5*x(1)/pi-6)^2+10*(1-1/(8*pi))*cos(x(1))+10; -------------------------------------------------------------------------------- /第18章/例18.2/qGA1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.2/qGA1.m -------------------------------------------------------------------------------- /第18章/例18.2/rotationq.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.2/rotationq.m -------------------------------------------------------------------------------- /第18章/例18.2/trimatrix.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.2/trimatrix.m -------------------------------------------------------------------------------- /第18章/例18.2/例18.2.txt: -------------------------------------------------------------------------------- 1 | [cbest1,cbest2]=qGA1(@optifun49) -------------------------------------------------------------------------------- /第18章/例18.3/isin_TSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.3/isin_TSP.m -------------------------------------------------------------------------------- /第18章/例18.3/qGATSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.3/qGATSP.m -------------------------------------------------------------------------------- /第18章/例18.3/例18.3.txt: -------------------------------------------------------------------------------- 1 | city =[16.4700 96.1000 2 | 16.4700 94.4400 3 | 20.0900 92.5400 4 | 22.3900 93.3700 5 | 25.2300 97.2400 6 | 22.0000 96.0500 7 | 20.4700 97.0200 8 | 17.2000 96.2900 9 | 16.3000 97.3800 10 | 14.0500 98.1200 11 | 16.3500 97.3800 12 | 21.5200 95.5900 13 | 19.4100 97.1300 14 | 20.0900 94.5500]; 15 | [route,f]=qGATSP(city) -------------------------------------------------------------------------------- /第18章/例18.4/optifun50.m: -------------------------------------------------------------------------------- 1 | function y=optifun50(x) 2 | %y=abs(sin(30*x))*(1-x/2); 3 | y=sin(sum(abs(x-5)))/sum(abs(x-5)); -------------------------------------------------------------------------------- /第18章/例18.4/qGA3.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第18章/例18.4/qGA3.m -------------------------------------------------------------------------------- /第18章/例18.4/例18.4.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=qGA3(@optifun50) -------------------------------------------------------------------------------- /第19章/例19.1/ABC.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.1/ABC.m -------------------------------------------------------------------------------- /第19章/例19.1/optifun51.m: -------------------------------------------------------------------------------- 1 | function y=optifun51(x) 2 | y=-sum1(x); 3 | -------------------------------------------------------------------------------- /第19章/例19.1/sum1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.1/sum1.m -------------------------------------------------------------------------------- /第19章/例19.1/例19.1.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=ABC(@optifun51,200,3000,-100.*ones(10,1),100.*ones(10,1),20) -------------------------------------------------------------------------------- /第19章/例19.2/newABC.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.2/newABC.m -------------------------------------------------------------------------------- /第19章/例19.2/optifun52.m: -------------------------------------------------------------------------------- 1 | function y=optifun52(x) 2 | NC=length(x); 3 | y1=0; 4 | y=1; 5 | for i=1:NC 6 | y1=y1+abs(x(i)); 7 | y=y*abs(x(i)); 8 | end 9 | y=-y-y1; -------------------------------------------------------------------------------- /第19章/例19.2/例19.2.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=newABC(@optifun52,100,500,-10.*ones(30,1),10.*ones(30,1),100) -------------------------------------------------------------------------------- /第19章/例19.3/optifun53.m: -------------------------------------------------------------------------------- 1 | function y=optifun53(x,net) 2 | y=-sim(net,x'); -------------------------------------------------------------------------------- /第19章/例19.3/例19.3.txt: -------------------------------------------------------------------------------- 1 | LB=[-5;-5];UB=[5;5]; 2 | for i=1:4000 3 | x(i,:)=LB'+(UB-LB)'.*rand(1,2); 4 | y(i,1)=x(i,1)^2+x(i,2)^2; 5 | end 6 | k1=randperm(4000); 7 | input_train=x(k1(1:3900),:)'; 8 | output_train=y(k1(1:3900),1)'; 9 | net=newff(input_train,output_train,5); 10 | net.trainParam.epochs=100; 11 | net.trainParam.lr=0.1; 12 | net.trainParam.goal=1e-7; 13 | net=train(net,input_train,output_train); 14 | [best_x,fval]=newABC(@(x)optifun53(x,net),50,500,-5.*ones(2,1),5.*ones(2,1),100) 15 | 16 | -------------------------------------------------------------------------------- /第19章/例19.4/optifun54.m: -------------------------------------------------------------------------------- 1 | function y=optifun54(x) 2 | y1=[37.831 45.191 52.551 58.912 66.172 74.632 81.993 89.353 3 | 40.684 45.075 51.032 58.298 65.885 72.541 77.537 85.896 4 | 43.785 46.181 49.065 54.487 61.503 71.830 81.674 89.248 5 | 43.785 44.221 50.073 56.116 64.201 72.789 82.807 93.749 6 | 44.015 46.179 50.134 55.380 62.387 72.292 84.821 95.659]; 7 | y2=[43.785 45.646 50.209 55.758 62.882 72.520 83.301 94.633]; 8 | y=sum((y2-x*y1).^2)+5000*(sum(x)-1)^2; 9 | if y<0 10 | y=1+abs(y); 11 | else 12 | y=1/(1+y); 13 | end -------------------------------------------------------------------------------- /第19章/例19.4/例19.4.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=newABC(@optifun54,100,3000,zeros(5,1),ones(5,1),300) -------------------------------------------------------------------------------- /第19章/例19.5/optifun55.m: -------------------------------------------------------------------------------- 1 | function y=optifun55(x) 2 | x=realbit(x); 3 | y1=3*x(1)*x(2)*x(4)+2*x(2)*x(3)*x(4)+9*x(2)*x(4)+5*x(1)-2*x(2)+7*x(4)-17; 4 | y2=x(1)*x(2)*x(3)*x(4)+2*x(2)*x(3)*x(4)+x(2)*x(4)+6*x(1)-2*x(2)+6*x(4)-6; 5 | y3=x(1)*x(2)*x(3)*x(4)+2*x(2)*x(3)*x(4)+x(2)*x(4)+7*x(1)-2*x(2)+7*x(4)-7; 6 | y=y1+5000*(y2^2+y3^2); 7 | if y>0 8 | y=1/(1+y); 9 | else 10 | y=1+abs(y); 11 | end 12 | -------------------------------------------------------------------------------- /第19章/例19.5/realbit.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.5/realbit.m -------------------------------------------------------------------------------- /第19章/例19.5/例19.5.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=newABC(@optifun55,100,500,-5.*ones(4,1),5.*ones(4,1),300); 2 | realbit(best_x) -------------------------------------------------------------------------------- /第19章/例19.6/TSP_opt.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/TSP_opt.m -------------------------------------------------------------------------------- /第19章/例19.6/TSPop.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/TSPop.m -------------------------------------------------------------------------------- /第19章/例19.6/bitABC.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/bitABC.m -------------------------------------------------------------------------------- /第19章/例19.6/greed.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/greed.m -------------------------------------------------------------------------------- /第19章/例19.6/init_ABC.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/init_ABC.m -------------------------------------------------------------------------------- /第19章/例19.6/isin_TSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/isin_TSP.m -------------------------------------------------------------------------------- /第19章/例19.6/matrixinsert.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第19章/例19.6/matrixinsert.m -------------------------------------------------------------------------------- /第19章/例19.6/例19.6.txt: -------------------------------------------------------------------------------- 1 | city =[16.4700 96.1000 2 | 16.4700 94.4400 3 | 20.0900 92.5400 4 | 22.3900 93.3700 5 | 25.2300 97.2400 6 | 22.0000 96.0500 7 | 20.4700 97.0200 8 | 17.2000 96.2900 9 | 16.3000 97.3800 10 | 14.0500 98.1200 11 | 16.3500 97.3800 12 | 21.5200 95.5900 13 | 19.4100 97.1300 14 | 20.0900 94.5500]; 15 | [best_x,fval]=bitABC(30,500,10,city,'tsp') -------------------------------------------------------------------------------- /第20章/例20.1/chaos.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.1/chaos.m -------------------------------------------------------------------------------- /第20章/例20.1/optifun56.m: -------------------------------------------------------------------------------- 1 | function y=optifun56(x) 2 | y=4+4.5*x(1)-4*x(2)+x(1)^2+2*x(2)^2-2*x(1)*x(2)+x(1)^4-2*x(1)^2*x(2); 3 | 4 | -------------------------------------------------------------------------------- /第20章/例20.1/optifun57.m: -------------------------------------------------------------------------------- 1 | function y=optifun57(x) 2 | y1=(1+(x(1)+x(2)+1)^2*(19-14*x(1)+3*x(1)^2-14*x(2)+6*x(1)*x(2)+3*x(2)^2)); 3 | y2=(30+(2*x(1)-3*x(2))^2*(18-32*x(1)+12*x(1)^2+48*x(2)-36*x(1)*x(2)+27*x(2)^2)); 4 | y=y1*y2; -------------------------------------------------------------------------------- /第20章/例20.1/例20.1.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=chaos(@optifun56,[-8;-8],[8;8],10000,500,1) 2 | 3 | [best_x,fval]=chaos(@optifun57,[-2;-2],[2;2],10000,30,500,2) -------------------------------------------------------------------------------- /第20章/例20.2/chaosTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.2/chaosTSP.m -------------------------------------------------------------------------------- /第20章/例20.2/例20.2.txt: -------------------------------------------------------------------------------- 1 | city =[16.4700 96.1000 2 | 16.4700 94.4400 3 | 20.0900 92.5400 4 | 22.3900 93.3700 5 | 25.2300 97.2400 6 | 22.0000 96.0500 7 | 20.4700 97.0200 8 | 17.2000 96.2900 9 | 16.3000 97.3800 10 | 14.0500 98.1200 11 | 16.3500 97.3800 12 | 21.5200 95.5900 13 | 19.4100 97.1300 14 | 20.0900 94.5500]; 15 | [best_route,fval]=chaosTSP(city,5000,0.3,0.8,1.01) -------------------------------------------------------------------------------- /第20章/例20.3/delsample.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.3/delsample.m -------------------------------------------------------------------------------- /第20章/例20.3/newchoas.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.3/newchoas.m -------------------------------------------------------------------------------- /第20章/例20.3/optifun58.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.3/optifun58.m -------------------------------------------------------------------------------- /第20章/例20.3/例20.3.txt: -------------------------------------------------------------------------------- 1 | xmin=newchoas(@optifun58,[-1 1],[-2;-2],[2;2],50,500,1e-6) -------------------------------------------------------------------------------- /第20章/例20.4/chaos_serch.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.4/chaos_serch.m -------------------------------------------------------------------------------- /第20章/例20.4/gachaos.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第20章/例20.4/gachaos.m -------------------------------------------------------------------------------- /第20章/例20.4/optifun59.m: -------------------------------------------------------------------------------- 1 | function y=optifun59(x) 2 | y=sin(x(1))*sin(x(2))/(x(1)*x(2)); 3 | -------------------------------------------------------------------------------- /第20章/例20.4/例20.4.txt: -------------------------------------------------------------------------------- 1 | cbest=gachaos(@optifun59,30,500,0.1,0.9,-10.*ones(2,1),10.*ones(2,1)) -------------------------------------------------------------------------------- /第21章/例21.1/IA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.1/IA.m -------------------------------------------------------------------------------- /第21章/例21.1/optifun64.m: -------------------------------------------------------------------------------- 1 | function y=optifun64(x) 2 | p=[0.1312 0.1696 0.5569 0.0124 0.8283 0.5886 3 | 0.2329 0.4135 0.8307 0.3736 0.1004 0.9991 4 | 0.2348 0.1415 0.3522 0.2883 0.3047 0.6650 5 | 0.4047 0.8828 0.8732 0.5743 0.1091 0.0381]; 6 | c=[1 1.2 3 3.2]; 7 | a=[10 3 17 3.5 1.7 8 8 | 0.05 10 17 0.1 8 14 9 | 3 3.5 1.7 10 17 8 10 | 17 8 0.05 10 0.1 14]; 11 | y=0; 12 | for i=1:4 13 | b=0; 14 | for j=1:6 15 | b=b+a(i,j)*(x(j)-p(i,j))^2; 16 | end 17 | y=y+c(i)*exp(-b); 18 | end 19 | y=-y; 20 | 21 | -------------------------------------------------------------------------------- /第21章/例21.1/例21.1.txt: -------------------------------------------------------------------------------- 1 | [bestx,f]=IA(@optifun64,50,3000,0.2,0.9,0.9,[0;0;0;0;0;0],[1;1;1;1;1;1]) -------------------------------------------------------------------------------- /第21章/例21.2/dyIA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.2/dyIA.m -------------------------------------------------------------------------------- /第21章/例21.2/optifun70.m: -------------------------------------------------------------------------------- 1 | function [y,y1,g1,g2]=optifun70(x) 2 | a1=(max(0,((x(1)-0.05)^2+(x(2)-2.5)^2-4.84)))^2; 3 | a2=(max(0,(-x(1)^2-(x(2)-2.5)^2+4.84)))^2; 4 | y1=(x(1)^2+x(2)-11)^2+(x(1)+x(2)^2-7)^2; 5 | y=(x(1)^2+x(2)-11)^2+(x(1)+x(2)^2-7)^2+10000*(a1+a2); 6 | g1=4.84-(x(1)-0.05)^2+(x(2)-2.5)^2; 7 | g2=x(1)^2+(x(2)-2.5)^2-4.84; 8 | 9 | -------------------------------------------------------------------------------- /第21章/例21.2/例21.2.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=dyIA(@optifun70,100,1000,0.9,[0;0],[6;6]) 2 | [y,y1,g1,g2]=optifun70(best_x); -------------------------------------------------------------------------------- /第21章/例21.3/IAGA.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/IAGA.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_bacterin.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_bacterin.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_caldensity.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_caldensity.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_calpd.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_calpd.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_calpf.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_calpf.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_densitysort.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_densitysort.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_fitsort.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_fitsort.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_operator.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.3/ICA_operator.m -------------------------------------------------------------------------------- /第21章/例21.3/ICA_select.m: -------------------------------------------------------------------------------- 1 | function m_antibody=ICA_select(m_antibody) 2 | antibodynum=size(m_antibody,2); 3 | a=0.7; 4 | P=zeros(1,antibodynum); 5 | for i=1:antibodynum 6 | P(i)=a*m_antibody(i).pf+(1-a)*m_antibody(i).pd; 7 | end 8 | c=zeros(1,antibodynum); 9 | for i=1:antibodynum 10 | if i==1 11 | c(i)=P(i); 12 | else 13 | c(i)=c(i-1)+P(i); 14 | end 15 | end 16 | c=c/c(antibodynum); 17 | for i=1:antibodynum 18 | p=rand; 19 | index=1; 20 | while c(index)

0.5 23 | pop(i).x(j)=pop(i).x(j)+v1*fg; 24 | else 25 | pop(i).x(j)=pop(i).x(j)+v2*fg; 26 | end 27 | pop(i).x(j)=boundtest(pop(i).x(j),LB(j),UB(j)); 28 | end 29 | end 30 | end 31 | end -------------------------------------------------------------------------------- /第21章/例21.3/optifun71.m: -------------------------------------------------------------------------------- 1 | function y=optifun71(x) 2 | fa=1+(x(1)+x(2)+1)^2*(19-14*x(1)+3*x(1)^2-14*x(2)+6*x(1)*x(2)+3*x(2)^2); 3 | fa=1/fa; 4 | fb=30+(2*x(1)-3*x(2))^2*(18-32*x(1)+12*x(1)^2+48*x(2)-36*x(1)*x(2)+27*x(2)^2); 5 | fb=1/fb; 6 | y=fa*fb; 7 | -------------------------------------------------------------------------------- /第21章/例21.3/例21.3.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=IAGA(@optifun71,100,500,0.9,0.2,[-2;-2],[2;2]) -------------------------------------------------------------------------------- /第21章/例21.4/ICA_TSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.4/ICA_TSP.m -------------------------------------------------------------------------------- /第21章/例21.4/ICA_operator_TSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.4/ICA_operator_TSP.m -------------------------------------------------------------------------------- /第21章/例21.4/gacrossover_TSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.4/gacrossover_TSP.m -------------------------------------------------------------------------------- /第21章/例21.4/gamutation_TSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.4/gamutation_TSP.m -------------------------------------------------------------------------------- /第21章/例21.4/my_delrow.m: -------------------------------------------------------------------------------- 1 | function y=my_delrow(y,x) 2 | for i=1:length(x) 3 | y(find(y==x(i)))=[]; 4 | end -------------------------------------------------------------------------------- /第21章/例21.4/例21.4.txt: -------------------------------------------------------------------------------- 1 | city =[16.4700 96.1000 2 | 16.4700 94.4400 3 | 20.0900 92.5400 4 | 22.3900 93.3700 5 | 25.2300 97.2400 6 | 22.0000 96.0500 7 | 20.4700 97.0200 8 | 17.2000 96.2900 9 | 16.3000 97.3800 10 | 14.0500 98.1200 11 | 16.3500 97.3800 12 | 21.5200 95.5900 13 | 19.4100 97.1300 14 | 20.0900 94.5500]; 15 | [best_route,fval]=ICA_TSP(city,50,500,0.9,0.1) -------------------------------------------------------------------------------- /第21章/例21.5/IAEP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.5/IAEP.m -------------------------------------------------------------------------------- /第21章/例21.5/gaussmution.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.5/gaussmution.m -------------------------------------------------------------------------------- /第21章/例21.5/optifun72.m: -------------------------------------------------------------------------------- 1 | function y=optifun72(x) 2 | y=-cos(x(1))*cos(x(2))*exp(-(x(1)-pi)^2-(x(2)-pi)^2); 3 | y=-y; -------------------------------------------------------------------------------- /第21章/例21.5/例21.5.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=IAEP(@optifun72,50,500,[-100;-100],[100;100],3) -------------------------------------------------------------------------------- /第21章/例21.6/IAES.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.6/IAES.m -------------------------------------------------------------------------------- /第21章/例21.6/IAES_mutation.m: -------------------------------------------------------------------------------- 1 | function [oldchome,newsigma]=IAES_mutation(oldchome,oldsigma,LB,UB) 2 | popsize=size(oldchome,2); 3 | nvar=length(oldchome(1).x); 4 | for j=1:popsize 5 | a=randn; 6 | for k=1:nvar 7 | newsigma(j,k)=oldsigma(j,k)*exp(a+randn); 8 | oldchome(j).x(k)=oldchome(j).x(k)+normrnd(0,newsigma(j,k),1,1); 9 | oldchome(j).x(k)=boundtest(oldchome(j).x(k),LB(k),UB(k)); 10 | end 11 | end -------------------------------------------------------------------------------- /第21章/例21.6/IAES_recombination1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.6/IAES_recombination1.m -------------------------------------------------------------------------------- /第21章/例21.6/IAES_select1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.6/IAES_select1.m -------------------------------------------------------------------------------- /第21章/例21.6/optifun73.m: -------------------------------------------------------------------------------- 1 | function y=optifun73(x) 2 | %y=sin(sqrt(x(1)^2+x(2)^2))-cos(sqrt(abs(x(1)^2-x(2)^2)))-0.02*(x(2)-4.96)^2-0.02*(x(1)-5.87)^2; 3 | y=exp(-(x(1)-0.1)^2)*(sin(5*pi*x(1)^(3/4)))^6; -------------------------------------------------------------------------------- /第21章/例21.6/例21.6.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=IAES(@optifun73,50,500,0,2) -------------------------------------------------------------------------------- /第21章/例21.7/IAClone.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.7/IAClone.m -------------------------------------------------------------------------------- /第21章/例21.7/clone.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.7/clone.m -------------------------------------------------------------------------------- /第21章/例21.7/clonemutation.m: -------------------------------------------------------------------------------- 1 | function cloneantibody=clonemutation(cloneantibody,iterm,iterm_max,pm,LB,UB) 2 | cloneantibodynum=size(cloneantibody,2); 3 | NC=length(cloneantibody(1).x); 4 | for i=1:cloneantibodynum 5 | for j=1:NC 6 | p=rand; 7 | if p0.5 12 | cloneantibody(i).x(j)=cloneantibody(i).x(j)+v1*fg; 13 | else 14 | cloneantibody(i).x(j)=cloneantibody(i).x(j)+v2*fg; 15 | end 16 | cloneantibody(i).x(j)=boundtest(cloneantibody(i).x(j),LB(j),UB(j)); 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /第21章/例21.7/cloneselection.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.7/cloneselection.m -------------------------------------------------------------------------------- /第21章/例21.7/optifun74.m: -------------------------------------------------------------------------------- 1 | function y=optifun74(x) 2 | y=x(1)^2-0.4*cos(3*pi*x(1))+2*x(2)^2-0.6*cos(4*pi*x(2))-1; 3 | y=-y; -------------------------------------------------------------------------------- /第21章/例21.7/例21.7.txt: -------------------------------------------------------------------------------- 1 | [best_x,fval]=IAClone(@optifun74,50,3000,0.3,[-10;-10],[10;10]) -------------------------------------------------------------------------------- /第21章/例21.8/IAClone_bit.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.8/IAClone_bit.m -------------------------------------------------------------------------------- /第21章/例21.8/ICA_operator_bit.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第21章/例21.8/ICA_operator_bit.m -------------------------------------------------------------------------------- /第21章/例21.8/clonemutation_bit.m: -------------------------------------------------------------------------------- 1 | function cloneantibody=clonemutation_bit(cloneantibody,pm) 2 | cloneantibodynum=size(cloneantibody,2); 3 | NC=length(cloneantibody(1).x); 4 | for i=1:cloneantibodynum 5 | for j=1:NC 6 | p=rand; 7 | if p0); 9 | k=0; 10 | while k<500 11 | I1=redu(1:n,I,'c'); 12 | xB=x0(I);xN=redu(x0,I,'c'); 13 | B=A(:,I);N=redu(A,I,'c'); 14 | g0=eval(subs(z,x_syms,x0))'; 15 | gB=g0(I,:);gN=redu(g0,I,'r'); 16 | r=gN-(B^-1*N)'*gB; 17 | for i=1:n-m 18 | if r(i)>0 19 | dN(i,1)=-xN(i)*r(i); 20 | else 21 | dN(i,1)=-r(i); 22 | end 23 | end 24 | dB=-B^-1*N*dN; 25 | dk(I,1)=dB;dk(I1,1)=dN; 26 | if norm(dk)0); 32 | if ~isempty(a1) 33 | b2=min(-xB(a1)'./dB(a1)); 34 | else 35 | b2=inf; 36 | end 37 | if ~isempty(a2) 38 | b3=max(-xB(a2)'./dB(a2)); 39 | else 40 | b3=inf; 41 | end 42 | L1=min(b2,b3); 43 | id1=find(dB<0);id2=find(dN<0); 44 | dkj=[];xkj=[]; 45 | if ~isempty(id1) 46 | dkj=[dkj dB(id1)']; 47 | xkj=[xkj xB(id1)]; 48 | end 49 | if ~isempty(id2) 50 | dkj=[dkj dN(id2)']; 51 | xkj=[xkj xN(id2)]; 52 | end 53 | alpha_av=min(-xkj./dkj); 54 | alpha=mygoldcut1(fun,x0,dk,0,alpha_av,x_syms); 55 | xk=x0+alpha*dk'; 56 | if abs(alpha-L1)<1e-6 57 | a3=findzeros(xk,0); 58 | y3=mycompare1(a3,I); 59 | y4=[]; 60 | for i=1:length(y3) 61 | y4=[y4 find(I==y3(i))]; 62 | end 63 | I=redu(I,y4,'c'); 64 | [a4,b4]=max(xk(I1)); 65 | I=sort([I b4]); 66 | end 67 | x0=xk; 68 | k=k+1; 69 | end 70 | end 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /第3章/例3.5/例3.5.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms x y t u 3 | fun=2*x^2+2*y^2-2*x*y-4*x-6*y; 4 | A=[1 1 1 0 ;1 5 0 1];b=[2 5]; 5 | x0=[0 0 2 5];esp=0.0001; 6 | x_syms=[sym(x) sym(y) sym(t) sym(u)]; 7 | [xmin,minf]=wolfe1(fun,A,x0,x_syms,esp); 8 | 9 | -------------------------------------------------------------------------------- /第3章/例3.6/dlg11.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.6/dlg11.m -------------------------------------------------------------------------------- /第3章/例3.6/lghess1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.6/lghess1.m -------------------------------------------------------------------------------- /第3章/例3.6/lgmatrix1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.6/lgmatrix1.m -------------------------------------------------------------------------------- /第3章/例3.6/myhessian1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.6/myhessian1.m -------------------------------------------------------------------------------- /第3章/例3.6/newlag1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.6/newlag1.m -------------------------------------------------------------------------------- /第3章/例3.6/例3.6.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms x y 3 | fun=1-x^2+exp(-x-y)+y^2-2*x*y+exp(x)-3*y; 4 | hfun=[x^2+y^2-5]; 5 | x0=[1 1];x_syms=[sym(x) sym(y)];esp=0.0001; 6 | [xmin,minf]=newlag1(fun,hfun,x0,x_syms,esp); -------------------------------------------------------------------------------- /第3章/例3.7/BB.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.7/BB.m -------------------------------------------------------------------------------- /第3章/例3.7/sqp1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.7/sqp1.m -------------------------------------------------------------------------------- /第3章/例3.7/例3.7.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms x y z 3 | fun=(x^2+y^2+z^2)/2; 4 | hfun=[x+2*y-z-4;x-y+z+2]; 5 | x0=[0 0 0];esp=0.0001; 6 | x_syms=[sym(x) sym(y) sym(z)]; 7 | [xmin,minf]=sqp1(fun,hfun,x0,x_syms,esp); 8 | -------------------------------------------------------------------------------- /第3章/例3.8/newquad.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.8/newquad.m -------------------------------------------------------------------------------- /第3章/例3.8/newsqp1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第3章/例3.8/newsqp1.m -------------------------------------------------------------------------------- /第3章/例3.8/例3.8.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms x1 x2 x3 x4 x5 3 | fun=exp(x1*x2*x3*x4*x5)-(x1^3+x2^3+1)^2/2; 4 | hfun=[x1^2+x2^2+x3^2+x4^2+x5^2-10;x2*x3-5*x4*x5;x1^3+x2^3+1]; 5 | gfun=[]; 6 | x_syms=[sym(x1) sym(x2) sym(x3) sym(x4) sym(x5)]; 7 | x0=[-1.7 1.5 1.8 -.6 -0.6]; 8 | mu=[0 0 0]'; 9 | lam=[]; 10 | [xmin1,minf,mu,lam]=newsqp1(fun,hfun,gfun,x0,x_syms); 11 | 12 | clear 13 | syms x1 x2 14 | fun=exp(-x1-x2)+x1^2+2*x1*x2+x2^2+2*x1+6*x2; 15 | gfun=[2-x1-x2;x1;x2]; 16 | hfun=[]; 17 | x_syms=[sym(x1) sym(x2)]; 18 | x0=[1 1]; 19 | [xmin2,minf,mu,lam]=newsqp1(fun,hfun,gfun,x0,x_syms); -------------------------------------------------------------------------------- /第3章/例3.9/mycon1.m: -------------------------------------------------------------------------------- 1 | function [c,ceq]=mycon1(x) 2 | ceq=x(2)*x(3)+x(1)*x(2)+x(1)*x(3)-75; 3 | c=[]; -------------------------------------------------------------------------------- /第3章/例3.9/optifun7.m: -------------------------------------------------------------------------------- 1 | function y=optifun7(x) 2 | y=-x(1)*x(2)*x(3); -------------------------------------------------------------------------------- /第3章/例3.9/例3.9.txt: -------------------------------------------------------------------------------- 1 | clear 2 | options = optimset('Algorithm','sqp'); 3 | [x,val]=fmincon(@optifun7,[1 2 3],[],[],[],[],zeros(3,1),[inf;inf;inf],@mycon1,options); -------------------------------------------------------------------------------- /第4章/例4.1/myls.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第4章/例4.1/myls.m -------------------------------------------------------------------------------- /第4章/例4.1/例4.1.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[136.5 215;136.5 250;136.5 180;138.5 250;138.5 180; 3 | 138.5 215;138.5 215;138.5 215;140.5 180;140.5 215;140.5 250]; 4 | b=[6.2 7.5 4.8 5.1 4.6 4.6 4.9 4.1 2.8 3.1 4.3]'; 5 | [x,r]=myls(A,b); -------------------------------------------------------------------------------- /第4章/例4.2/mylm.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第4章/例4.2/mylm.m -------------------------------------------------------------------------------- /第4章/例4.2/例4.2.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms k0 k1 x b 3 | fun=k0*exp(k1*x)-b; 4 | x_syms=[sym(x) sym(b)];b_syms=[sym(k0) sym(k1)];x0=[1.4 -1.0]; 5 | x=[0.1 0.2 0.3 0.4 0.5]';b=[1.22 1.00 0.82 0.67 0.55]'; 6 | [xmin,minf]=mylm(fun,x0,x_syms,x,b,b_syms); -------------------------------------------------------------------------------- /第4章/例4.3/mylm1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第4章/例4.3/mylm1.m -------------------------------------------------------------------------------- /第4章/例4.3/例4.3.txt: -------------------------------------------------------------------------------- 1 | clear 2 | x0=3.*ones(100,1); 3 | [xmin,minf]=mylm1(x0); -------------------------------------------------------------------------------- /第4章/例4.4/newls1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第4章/例4.4/newls1.m -------------------------------------------------------------------------------- /第4章/例4.4/例4.4.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms k0 k1 x b 3 | fun=k0-k1/x-b; 4 | x_syms=[sym(x) sym(b)];b_syms=[sym(k0) sym(k1)]; 5 | x0=[10 100]; esp=1e-6; 6 | x1=[2:16]';b0=[6.42 8.2 9.58 9.5 9.7 10 9.93 9.99 10.49 10.59 10.6 10.8 10.6 10.9 10.76]'; 7 | [beta,minf,stats]=newls1(fun,x1,b0,x0,x_syms,b_syms,1,esp); 8 | [beta1,minf1,stats1]=newls1(fun,x1,b0,x0,x_syms,b_syms,2,esp); 9 | -------------------------------------------------------------------------------- /第4章/例4.5/orthfun.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第4章/例4.5/orthfun.m -------------------------------------------------------------------------------- /第4章/例4.5/simplify.m: -------------------------------------------------------------------------------- 1 | function r = simplify(s,varargin) 2 | %SIMPLIFY Symbolic simplification. 3 | % SIMPLIFY(S) simplifies each element of the symbolic matrix S. 4 | % 5 | % SIMPLIFY(S,N) or, equivalently, SIMPLIFY(S,'Steps',N), 6 | % searches for a simplification in N steps. The default value of N is 1. 7 | % 8 | % SIMPLIFY(S,'Seconds',T) aborts the search for a simpler version 9 | % at the next end of a simplification step after T seconds. The results 10 | % will depend on the speed and system load of your computer and may 11 | % not be reproducible. 12 | % 13 | % SIMPLIFY(S,'IgnoreAnalyticConstraints',VAL) controls the level of 14 | % mathematical rigor to use on the analytical constraints while simplifying 15 | % (branch cuts, division by zero, etc). The options for VAL are TRUE or 16 | % FALSE. Specify TRUE to relax the level of mathematical rigor 17 | % in the rewriting process. The default is FALSE. 18 | % 19 | % SIMPLIFY(S, 'Criterion', 'preferReal') 20 | % discourages simplify from returning results 21 | % containing complex numbers. 22 | % 23 | % Examples: 24 | % simplify(sin(x)^2 + cos(x)^2) returns 1. 25 | % simplify(exp(c*log(sqrt(alpha+beta)))) returns (alpha + beta)^(c/2). 26 | % simplify(sqrt(x^2)) returns sqrt(x^2), 27 | % simplify(sqrt(x^2),'IgnoreAnalyticConstraints',true) returns x. 28 | % 29 | % See also SYM/FACTOR, SYM/EXPAND, SYM/COLLECT. 30 | 31 | % Copyright 1993-2014 The MathWorks, Inc. 32 | 33 | if builtin('numel',s) ~= 1, s = normalizesym(s); end 34 | 35 | posintvalidator = @(value) isnumeric(value) && 0 <= value && round(value) == value; 36 | 37 | p = inputParser; 38 | p.addOptional('Steps', 1, posintvalidator); 39 | p.addParameter('Seconds', sym(inf), @(value) isnumeric(value) && 0 <= value); 40 | p.addParameter('Criterion', 'default'); 41 | p.addParameter('IgnoreAnalyticConstraints', false); 42 | p.parse(varargin{:}); 43 | 44 | steps = sym(p.Results.Steps); 45 | seconds = sym(p.Results.Seconds); 46 | criterion = validatestring(p.Results.Criterion, {'default', 'preferReal'}); 47 | if strcmp(criterion, 'preferReal') 48 | criterion = evalin(symengine, 'Simplify::preferReal'); 49 | else 50 | criterion = evalin(symengine, 'Simplify::defaultValuation'); 51 | end 52 | if sym.checkIgnoreAnalyticConstraintsValue(p.Results.IgnoreAnalyticConstraints) 53 | IAC = 'TRUE'; 54 | else 55 | IAC = 'FALSE'; 56 | end 57 | 58 | rSym = feval(symengine, 'simplify', s,... 59 | evalin(symengine, ['IgnoreAnalyticConstraints =', IAC]),... 60 | sym('Steps') == steps,... 61 | sym('Seconds') == seconds,... 62 | sym('Criterion') == criterion,... 63 | evalin(symengine, 'OutputType = "Best"') ... 64 | ); 65 | r = privResolveOutput(rSym, s); 66 | -------------------------------------------------------------------------------- /第4章/例4.5/例4.5.txt: -------------------------------------------------------------------------------- 1 | clear 2 | x1=[0.50 1.00 1.50 2.00 2.5 3.0]; 3 | b=[1.01 1.08 1.16 1.25 1.29 1.30]; 4 | m=2; 5 | [fun,res]=orthfun(x1,b,2); -------------------------------------------------------------------------------- /第4章/例4.6/optifun8.m: -------------------------------------------------------------------------------- 1 | function y=optifun8(x,x1) 2 | y=x(1)+x(2)*exp(-0.02*x(3)*x1); 3 | -------------------------------------------------------------------------------- /第4章/例4.6/optifun9.m: -------------------------------------------------------------------------------- 1 | function y=optifun9(x) 2 | x1=1:10; 3 | y1=[3.5 3.0 2.6 2.3 2.1 1.9 1.7 1.6 1.5 1.4]; 4 | y=y1-(x(1)+x(2)*exp(-0.02*x(3)*x1)); 5 | -------------------------------------------------------------------------------- /第4章/例4.6/例4.6.txt: -------------------------------------------------------------------------------- 1 | clear 2 | x1=1:10;y1=[3.5 3.0 2.6 2.3 2.1 1.9 1.7 1.6 1.5 1.4]; 3 | x0=[1 1 1]; 4 | options=optimset('display','off'); 5 | k1=lsqcurvefit(@optifun8,x0,x1,y1,[],[],options); 6 | k2=lsqnonlin(@optifun9,x0,[],[],options); -------------------------------------------------------------------------------- /第5章/例5.1/dualsimplex.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.1/dualsimplex.m -------------------------------------------------------------------------------- /第5章/例5.1/findeye.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.1/findeye.m -------------------------------------------------------------------------------- /第5章/例5.1/findzeros.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.1/findzeros.m -------------------------------------------------------------------------------- /第5章/例5.1/mycompare.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.1/mycompare.m -------------------------------------------------------------------------------- /第5章/例5.1/mycompare1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.1/mycompare1.m -------------------------------------------------------------------------------- /第5章/例5.1/mysimplex.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.1/mysimplex.m -------------------------------------------------------------------------------- /第5章/例5.1/例5.1.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[2 2 1 0 0;1 2 0 1 0;4 0 0 0 1];b=[12;8;16];c=[-2 -3 0 0 0]; 3 | [minx1,minf2,A2,s]=mysimplex(A,b,c); 4 | 5 | clear 6 | A=[1 0 1 0 0;0 1 0 1 0;1 2 0 0 1];b=[4;3;8];c=[-1 -2 0 0 0]; 7 | [minx2,minf2,A2,s]=mysimplex(A,b,c); 8 | 9 | clear 10 | A=[1 0 0 1/4 -8 -1 9;0 1 0 1/2 -12 -1/2 3;0 0 1 0 0 1 0]; 11 | b=[0;0;1];c=[0 0 0 -3/4 20 -1/2 6]; 12 | [minx3,minf3,A2,s]=mysimplex(A,b,c); 13 | 14 | 15 | clear 16 | A=[1 -2 1 1 0;4 -1 -2 0 1;-2 0 1 0 0];b=[11;-3;1];c=[-3 1 1 0 0]; 17 | [minx4,minf4,A2,s]=mysimplex(A,b,c); 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /第5章/例5.2/simplex1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.2/simplex1.m -------------------------------------------------------------------------------- /第5章/例5.2/例5.2.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[2 1 1 1 0 0;1 2 3 0 1 0;2 2 1 0 0 1]; 3 | b=[2;5;6];c=[-3 -1 -3 0 0 0]; 4 | minx=simplex1(A,b,c); -------------------------------------------------------------------------------- /第5章/例5.3/例5.3.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[1 2 1 1;1 2 3 0 ;2 1 5 0];b=[10;15;20];c=[-1 -2 -3 1]; 3 | minx=simplex1(A,b,c); 4 | -------------------------------------------------------------------------------- /第5章/例5.4/例5.4.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[-1 -2 1 0 0;1 0 0 1 0;-3 -1 0 0 1];b=[-4;5;-6];c=[1 2 0 0 0]; 3 | minx=dualsimplex(A,b,c); -------------------------------------------------------------------------------- /第5章/例5.5/例5.5.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[1 1 1 0 0;2 3 0 1 0;3 2 0 0 1];b=[150;240;300];c=[-2.4 -1.8 0 0 0]; 3 | [minx,minf,A2,s]=mysimplex(A,b,c); 4 | -------------------------------------------------------------------------------- /第5章/例5.6/progturn.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.6/progturn.m -------------------------------------------------------------------------------- /第5章/例5.6/例5.6.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | A=[1 1 1 1 0;1 4 7 0 1];b=[3;9];c=[-2 -3 -1 0 0]; 3 | [minx,minf,A2,s]=mysimplex(A,b,c); 4 | 5 | 6 | A=[1 0 -1 4/3 -1/3;0 1 2 -1/3 1/3];b=[1;2];sigma=[0 0 2 -5/3 -1/3];f0=-8; 7 | y=progturn(A,b,sigma,f0,2,3); 8 | 9 | A=[1 0 -1 4/3 -1/3;0 1 2 -1/3 1/3];b=[1;2]; 10 | sigma=[0 0 -5 1 -1];f0=-6; 11 | y=progturn(A,b,sigma,f0,1,4); 12 | 13 | 14 | y=progturn(A,[-1/3;10/3],[0 0 -3 -5/3 -1/3],-28/3,1,5); 15 | 16 | 17 | -------------------------------------------------------------------------------- /第5章/例5.7/例5.7.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第5章/例5.7/例5.7.txt -------------------------------------------------------------------------------- /第6章/例6.1/decisfun1.m: -------------------------------------------------------------------------------- 1 | function u=decisfun1(k,x) 2 | if x==1 3 | u=[2;3;4]; 4 | elseif (x==2)||(x==3)||(x==4) 5 | u=[5;6;7]; 6 | elseif (x==5)||(x==6)||(x==7) 7 | u=[8;9]; 8 | elseif (x==8)||(x==9) 9 | u=10; 10 | elseif x==10 11 | u=10; 12 | end -------------------------------------------------------------------------------- /第6章/例6.1/dyprog.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.1/dyprog.m -------------------------------------------------------------------------------- /第6章/例6.1/exhaus.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.1/exhaus.m -------------------------------------------------------------------------------- /第6章/例6.1/findletter.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.1/findletter.m -------------------------------------------------------------------------------- /第6章/例6.1/road.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.1/road.m -------------------------------------------------------------------------------- /第6章/例6.1/simplex.m: -------------------------------------------------------------------------------- 1 | function [xsol, fval, lambda, exitflag, output, basicVarIdx, nonbasicVarIdx, delrows] = simplex(c, Ale, ble, ... 2 | Aeq, beq, lb, ub, options, defaultopt, computeLambda) 3 | %SIMPLEX Simplex method for general linear programming problem. 4 | % min c' * x 5 | % subject to Ale * x <= ble; 6 | % Aeq * x = beq; 7 | % lb <= x <= ub. 8 | % 9 | % XSOL = SIMPLEX(c,A,b,Aeq,beq) solves the problem above while 10 | % additionally satisfying the equality constraints Aeq * xsol = beq. 11 | % 12 | % XSOL = SIMPLEX(c,A,b,Aeq,beq,LB,UB) defines a set of lower and upper 13 | % bounds on the design variables, XSOL, so that the solution XSOL is always in the 14 | % range LB <= XSOL <= UB. If LB is [] or if any of the entries in LB are 15 | % -Inf, that is taken to mean the corresponding variable is not bounded below. 16 | % If UB is [] or if any of the entries in UB are Inf, that is taken to mean 17 | % the corresponding variable is not bounded above. 18 | % 19 | % XSOL = SIMPLEX(c,A,b,Aeq,beq,LB,UB,OPTIONS) minimizes with the default 20 | % optimization parameters replaced by values in the structure OPTIONS, an 21 | % argument created with the OPTIMSET function. See OPTIMSET for details. 22 | % Used options are Display, TolFun, LargeScale, MaxIter. Use OPTIONS = [] as 23 | % a place holder if no options are set. 24 | % 25 | % [XSOL,FVAL] = SIMPLEX(c,A,b) returns the value of the objective 26 | % function at XSOL: FVAL = c' * XSOL. 27 | % 28 | % [XSOL,FVAL,LAMBDA] = SIMPLEX(c,A,b) returns the set of Lagrange multipliers, 29 | % LAMBDA, at the solution, XSOL. 30 | % LAMBDA.ineqlin are the Lagrange multipliers for the inequalities. 31 | % LAMBDA.eqlin are the Lagrange multipliers for the equalities. 32 | % LAMBDA.lb the Lagrange multipliers for the lower bounds. 33 | % LAMBDA.ub are the Lagrange multipliers for the upper bounds. 34 | % 35 | % [XSOL,FVAL,LAMBDA,EXITFLAG] = SIMPLEX(c,A,b) describes the exit conditions: 36 | % If EXITFLAG is: 37 | % > 0 then SIMPLEX converged with a solution XSOL. 38 | % = 0 then SIMPLEX did not converge within the maximum number of iterations. 39 | % < 0 then the problem was infeasible or unbounded. 40 | % 41 | % [XSOL,FVAL,LAMBDA,EXITFLAG,OUTPUT] = SIMPLEX(c,A,b) returns a structure: 42 | % OUTPUT.iterations: number of iterations. 43 | % OUTPUT.cgiterations: []. 44 | 45 | % Copyright 1990-2015 The MathWorks, Inc. 46 | 47 | % Handle missing arguments 48 | if nargin < 10 49 | computeLambda = false; 50 | end 51 | 52 | if nargin < 8, options = []; 53 | if nargin < 7, ub = []; 54 | if nargin < 6, lb = []; 55 | if nargin < 5, beq = []; 56 | if nargin < 4, Aeq = []; 57 | end, end, end, end, end 58 | 59 | if nargout > 5 60 | restoreBasis = true; 61 | else 62 | restoreBasis = false; 63 | end 64 | 65 | % Set the default output fields 66 | output.iterations = 0; 67 | output.algorithm = 'simplex'; 68 | output.cgiterations = []; 69 | exitflag = 1; 70 | basicVarIdx = []; % indices of basic variables 71 | nonbasicVarIdx = []; % indices of nonbasic variables 72 | delrows = []; % logical vector indicating deleted rows 73 | 74 | % Options setup: tol and verbosity 75 | tol = optimget(options,'TolFun',defaultopt,'fast'); 76 | switch optimget(options,'Display',defaultopt,'fast') 77 | case {'none','off'} 78 | verbosity = 0; % error only 79 | case 'iter' 80 | verbosity = 2; % iter and final 81 | case 'final' 82 | verbosity = 1; 83 | case 'testing' 84 | verbosity = 4; % all 85 | otherwise 86 | verbosity = 1; % final message 87 | end 88 | 89 | % Check the input data 90 | [c, Ale, ble, Aeq, beq, lb, ub, exitflagDatacheck] = datacheck(c, Ale, ble, Aeq, beq, lb, ub, verbosity); 91 | n_orig = length(c); 92 | nslacks = length(ble); 93 | meq = length(beq); 94 | 95 | maxiter = optimget(options,'MaxIter',defaultopt,'fast'); 96 | if ischar(maxiter) 97 | if isequal(lower(maxiter),'100*numberofvariables') 98 | maxiter = 100*n_orig; 99 | else 100 | error(message('optim:simplex:InvalidMaxIter')) 101 | end 102 | end 103 | 104 | % Final output messages: 105 | presolveTermMsg = sprintf('Optimization terminated during preprocessing phase.'); 106 | presolveOptMsg = sprintf('Optimization terminated during preprocessing phase.'); 107 | presolveInfsMsg = sprintf('Exiting: The constraints are overly stringent; no feasible point exists.'); 108 | unboundedMsg = sprintf('Exiting: The problem is unbounded; the constraints are not restrictive enough.'); 109 | excdMaxiterPhase1Msg = sprintf(['Exiting: Maximum number of iterations exceeded in Phase 1;' ... 110 | '\n' ' increase options.MaxIterations.']); 111 | phaseoneInfsMsg = sprintf('Exiting: The constraints are overly stringent; no feasible starting point found.'); 112 | excdMaxiterPhase2Msg = sprintf('Exiting: Maximum number of iterations exceeded; increase options.MaxIterations.'); 113 | optMsg = sprintf('Optimization terminated.'); 114 | 115 | % Preprocessing the problem. 116 | % First transform into the standard LP form with equality constraints and bounds by adding slack variables. 117 | % The variable ndel represents the number of original variables deleted from the presolve procedure. 118 | [c0, cc, AA, bb, lbb, ubb, ndel, resolveSol, lambda, lamindx, exitflagp, MarkVars, MarkConstr] = ... 119 | simplexpresolve(c, Ale, ble, Aeq, beq, lb, ub, verbosity, computeLambda, restoreBasis); 120 | 121 | if exitflagp < 0 122 | exitflag = exitflagp; 123 | if exitflag == -1 % This value will be remapped to -2 in linprog.m 124 | msg = sprintf([presolveTermMsg '\n' presolveInfsMsg]); 125 | elseif exitflag == -2 % This value will be remapped to -3 in linprog.m 126 | msg = sprintf([presolveTermMsg '\n' unboundedMsg]); 127 | else 128 | error(message('optim:simplex:WrongStatus')) 129 | end 130 | if verbosity > 0 131 | disp(msg) 132 | end 133 | % At presolve stage, nothing to return to output yet. 134 | xsol = []; 135 | fval = []; 136 | lambda.ineqlin = []; 137 | lambda.eqlin = []; 138 | lambda.lower = []; 139 | lambda.upper = []; 140 | output.message = msg; 141 | return; 142 | elseif exitflagp == 0 143 | % Constraint matrix is empty, all variables are fixed, singleton, etc - 144 | % no more variables left to solve for (presolve solves the problem 145 | % already); call postsolve to restore the solution and the lambda to 146 | % the original system. 147 | xs = zeros(0,1); 148 | fv = 0.0; 149 | dualvars = struct('y', [], 'z', [], 'w', []); 150 | caseflag1 = 1; % Empty constraint matrix 151 | [xsol, fval, lambda, exitflagps, basicVarIdx, nonbasicVarIdx, delrows] = simplexpostsolve(xs, fv, c0, c, resolveSol, verbosity, ... 152 | lambda, lamindx, dualvars, Ale, Aeq, computeLambda, caseflag1, MarkVars, basicVarIdx, nonbasicVarIdx, restoreBasis, MarkConstr, []); 153 | exitflag = exitflagps; 154 | 155 | if (exitflag == 1) 156 | msg = sprintf(['Solution determined by the constraints.\n' presolveOptMsg]); 157 | elseif (exitflag == -1) % This value will be remapped to -2 in linprog.m 158 | msg = sprintf([presolveTermMsg '\n' presolveInfsMsg]); 159 | else 160 | error(message('optim:simplex:WrongStatus')); 161 | end 162 | % Display the final statement 163 | if verbosity > 0 164 | disp(msg) 165 | end 166 | output.message = msg; 167 | return; 168 | end 169 | 170 | %Step 3. phase 1 to find the first feasible basis by solving an auxiliary piecewise linear problem 171 | [c1, A, b, lbs, ubs, basicVarIdx, nonbasicVarIdx, x1opt, exitflagPhase1, lamindx, delrows1] = ... 172 | simplexphaseone(cc, AA, bb, lbb, ubb, n_orig, ndel, nslacks, maxiter, tol, verbosity, lamindx, computeLambda); 173 | exitflag = exitflagPhase1; 174 | % exitflagPhase1 = 1, feasible; 175 | % exitflagPhase1 = 0, exceed the maximum iteration; 176 | % exitflagPhase1 = -1, infeasible; 177 | % exitflagPhase1 = -2, unbounded. 178 | 179 | % Note that empty objective coefficients is recasted as zeros(n_orig, 1) in linprog. 180 | if (exitflagPhase1 == 1) && all(c == 0) 181 | computeLambda = false; 182 | if (verbosity >=2) 183 | disp(getString(message('optim:simplex:SkipPhase2'))); 184 | end 185 | end 186 | 187 | % Step 4. phase 2 is the main iterations to achieve optimality 188 | if exitflagPhase1 == 1 && any(c ~= 0) 189 | [xs, fv, dualvars, exitflagPhase2, niters, basicVarIdx, nonbasicVarIdx] = simplexphasetwo(c1, A, b, lbs, ubs, basicVarIdx, nonbasicVarIdx, x1opt, maxiter, tol, verbosity, computeLambda); 190 | output.iterations = niters; 191 | exitflag = exitflagPhase2; 192 | % exitflagPhase2 = 1, optimal; exitflagPhase2 =0, maxiter is exceeded in Phase 2; 193 | % exitflagPhase2 = -1, infeasible; exitflagPhase2 = -2, unbounded. 194 | else 195 | xs = x1opt; 196 | fv = c1' * x1opt; 197 | dualvars = struct('y', [], 'z', [], 'w', []); 198 | end 199 | 200 | % Step 5. postsolve restores the solution and the lambda (if applicable) to the original system 201 | caseflag2 = 2; % Nonempty constraint matrix 202 | if exitflag <= 0 203 | computeLambda = false; 204 | restoreBasis = false; 205 | end 206 | [xsol, fval, lambda, exitflagps, basicVarIdx, nonbasicVarIdx, delrows] = simplexpostsolve(xs, fv, c0, c, resolveSol, verbosity, ... 207 | lambda, lamindx, dualvars, Ale, Aeq, computeLambda, caseflag2, MarkVars, basicVarIdx, nonbasicVarIdx, restoreBasis, MarkConstr, delrows1); 208 | if exitflag == 1 209 | exitflag = exitflagps; 210 | end 211 | 212 | % Make sure output vector is full. 213 | xsol = full(xsol); 214 | 215 | % The values -1 and -2 of exitflag will be remapped to -2 and -3, 216 | % respectively, in linprog.m 217 | if exitflag == 1 218 | msg = optMsg; 219 | elseif exitflagPhase1 == 0 % Note exitflag == 0. 220 | msg = excdMaxiterPhase1Msg; 221 | elseif exitflag == 0 222 | msg = excdMaxiterPhase2Msg; 223 | elseif exitflagPhase1 == -1 % Note exitflag == -1. 224 | msg = phaseoneInfsMsg; 225 | elseif exitflag == -1 226 | msg = presolveInfsMsg; 227 | elseif exitflag == -2 228 | msg = unboundedMsg; 229 | else 230 | error(message('optim:simplex:WrongStatus')); 231 | end 232 | % Display the final statement 233 | if verbosity > 0 234 | disp(msg) 235 | end 236 | output.message = msg; 237 | 238 | %-------------------------------------------------------------------------- 239 | %-----------------------SIMPLEX Subfunctions---------------------------- 240 | %-------------------------------------------------------------------------- 241 | function [c, Ale, ble, Aeq, beq, lb, ub, exitflagDatacheck] = ... 242 | datacheck(c, Ale, ble, Aeq, beq, lb, ub, verbosity) 243 | % DATACHECK Check input data on the consistency of dimensions and boundary conditions. 244 | 245 | exitflagDatacheck = 1; 246 | % Check input data 247 | if (nargin < 3), ble = []; 248 | if (nargin < 2), Ale = []; 249 | if (nargin < 1) 250 | error(message('optim:simplex:NotEnoughInputs')); 251 | end, end, end 252 | 253 | [mle, nle] = size(Ale); 254 | [meq, neq] = size(Aeq); 255 | nvars = max( [length(c), length(ub), length(lb), neq, nle] ); 256 | 257 | if isempty(ub) 258 | ub = inf(nvars, 1); 259 | if (verbosity >= 4) 260 | disp(' Assuming all variables are unbounded above.'); 261 | end 262 | end 263 | 264 | if isempty(lb) 265 | lb = -inf(nvars, 1); 266 | if (verbosity >= 4) 267 | disp(' Assuming all variables are unbounded below.'); 268 | end 269 | end 270 | 271 | if isempty(c), c=zeros(nvars,1); end 272 | if isempty(Ale), Ale=zeros(0,nvars); end 273 | if isempty(ble), ble=zeros(0,1); end 274 | if isempty(Aeq), Aeq=zeros(0,nvars); end 275 | if isempty(beq), beq=zeros(0,1); end 276 | 277 | c = c(:); 278 | lb = lb(:); 279 | ub = ub(:); 280 | ble = ble(:); 281 | beq = beq(:); 282 | 283 | if (size(c,2) ~= 1) 284 | error(message('optim:simplex:InvalidFirstInput')) 285 | end 286 | 287 | if any(isnan([c; lb; ub])) || any(isnan([ble;beq])) || any(any(isnan([Ale; Aeq]))) 288 | exitflagDatacheck = -1; 289 | error(message('optim:simplex:NaNInput')); 290 | end 291 | 292 | if any(any(isinf([c'; Ale; Aeq]))) || any(isinf([ble;beq])) 293 | exitflagDatacheck = -1; 294 | error(message('optim:simplex:InfInput')); 295 | end 296 | 297 | %-------------------------------------------------------------------------- 298 | %-------------END of SIMPLEX and its subfunctions----------------------- 299 | %-------------------------------------------------------------------------- 300 | -------------------------------------------------------------------------------- /第6章/例6.1/subfun1.m: -------------------------------------------------------------------------------- 1 | function v=subfun1(k,x,u,d) 2 | xz=[x==1 & u==2,x==1 & u==3,x==1 & u==4,... 3 | x==2 & u==5,x==2 & u==6,x==2 & u==7,... 4 | x==3 & u==5,x==3 & u==6,x==3 & u==7,... 5 | x==4 & u==5,x==4 & u==6,x==4 & u==7,... 6 | x==5 & u==8,x==5 & u==9,... 7 | x==6 & u==8,x==6 & u==9,... 8 | x==7 & u==8,x==7 & u==9,... 9 | x==8 & u==10,... 10 | x==9 & u==10]; 11 | v=xz*d; -------------------------------------------------------------------------------- /第6章/例6.1/trafun1.m: -------------------------------------------------------------------------------- 1 | function w=trafun1(k,x,u) 2 | w=u; -------------------------------------------------------------------------------- /第6章/例6.1/例6.1.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | x={[1];[2 3 4];[5 6 7];[8 9];[10]}; 3 | d={[2 4 3];[7 4 6;3 2 4;4 1 5];[1 4;6 3;3 3];[3;4]}; 4 | str={'A';'B1,B2,B3';'C1,C2,C3';'D1,D2';'E'}; 5 | [y,rd2]=road(x,d,str); 6 | 7 | 8 | clear; 9 | x=[1 2 5 8 10;NaN 3 6 9 NaN;NaN 4 7 NaN NaN]; 10 | str={'A';'B1,B2,B3';'C1,C2,C3';'D1,D2';'E'}; 11 | d=[2 4 3 7 4 6 3 2 4 4 1 5 1 4 6 3 3 3 3 4]'; 12 | [y,fval,rd2]=dyprog(x,d,@decisfun1,@subfun1,@trafun1,[],str); 13 | 14 | clear 15 | b=[1;0;0;0;0;0;0;0;0;1];f=[2 4 3 7 4 6 3 2 4 4 1 5 1 4 6 3 3 3 3 4]; 16 | A=[1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;1 0 0 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 | 0 1 0 0 0 0 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0;0 0 1 0 0 0 0 0 0 -1 -1 -1 0 0 0 0 0 0 0 0 18 | 0 0 0 1 0 0 1 0 0 1 0 0 -1 -1 0 0 0 0 0 0;0 0 0 0 1 0 0 1 0 0 1 0 0 0 -1 -1 0 0 0 0 19 | 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 -1 -1 0 0;0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 -1 0 20 | 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 -1;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1]; 21 | [x,fval]=bintprog(f,[],[],A,b); 22 | 23 | 24 | -------------------------------------------------------------------------------- /第6章/例6.2/decisfun2.m: -------------------------------------------------------------------------------- 1 | function u=decisfun2(k,x) 2 | if k==4 3 | u=x; 4 | else 5 | u=0:x; 6 | end -------------------------------------------------------------------------------- /第6章/例6.2/intprog.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.2/intprog.m -------------------------------------------------------------------------------- /第6章/例6.2/myinteger.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.2/myinteger.m -------------------------------------------------------------------------------- /第6章/例6.2/subfun2.m: -------------------------------------------------------------------------------- 1 | function v=subfun2(k,x,u,c) 2 | v=([0,10,20,30,40,50,60]==u)*c(:,k); -------------------------------------------------------------------------------- /第6章/例6.2/trafun2.m: -------------------------------------------------------------------------------- 1 | function w=trafun2(k,x,u) 2 | w=x-u; -------------------------------------------------------------------------------- /第6章/例6.2/例6.2.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | c=[0,0,0,0;20,20,25,25;50,40,60,40;65,50,85,50;80,55,100,60;85,60,110,65;85,65,115,70]; 3 | x=[ 0 0 0 0;10 10 10 10; 20 20 20 20; 30 30 30 30 4 | 40 40 40 40; 50 50 50 50;60 60 60 60]; 5 | [y,fval]=dyprog(x,-c,@decisfun2,@subfun2,@trafun2); 6 | 7 | 8 | clear 9 | f=-[0 20 50 65 80 85 85 0 20 40 50 55 60 65 0 25 60 85 100 110 115 0 25 40 50 60 65 70 0 0 0 0]; 10 | Aeq=[ones(1,7) zeros(1,25);zeros(1,7) ones(1,7) zeros(1,18); 11 | zeros(1,14) ones(1,7) zeros(1,11);zeros(1,21) ones(1,7) zeros(1,4) 12 | 0 10 20 30 40 50 60 zeros(1,21) -1 0 0 0 13 | zeros(1,7) 0 10 20 30 40 50 60 zeros(1,14) 0 -1 0 0 14 | zeros(1,14) 0 10 20 30 40 50 60 zeros(1,7) 0 0 -1 0 15 | zeros(1,21) 0 10 20 30 40 50 60 0 0 0 -1;zeros(1,28) 1 1 1 1]; 16 | Beq=[1 1 1 1 0 0 0 0 60];LB=zeros(32,1);UB = [ones(1,28) 60 60 60 60]'; 17 | [x,val,stats]=intprog(f,[],[],1:32,Aeq,Beq,LB,UB); 18 | 19 | -------------------------------------------------------------------------------- /第6章/例6.3/decisfun3.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.3/decisfun3.m -------------------------------------------------------------------------------- /第6章/例6.3/decisfun4.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.3/decisfun4.m -------------------------------------------------------------------------------- /第6章/例6.3/subfun3.m: -------------------------------------------------------------------------------- 1 | function v=subfun3(k,x,u,c) 2 | v=c(k)*u; 3 | v=-v; -------------------------------------------------------------------------------- /第6章/例6.3/trafun3.m: -------------------------------------------------------------------------------- 1 | function w=trafun3(k,x,u) 2 | ww=[3,4,2,5]; 3 | w=x-ww(k)*u; -------------------------------------------------------------------------------- /第6章/例6.3/例6.3.txt: -------------------------------------------------------------------------------- 1 | clear 2 | c=[4,5,3,6];T=12; 3 | x=nan*ones(T+1,4); 4 | x(:,1)=(0:T); 5 | x(:,2)=(0:T)'; 6 | x(:,3)=(0:T)'; 7 | x(:,4)=(0:T)'; 8 | [y,fval]=dyprog(x,c,@decisfun3,@subfun3,@trafun3); 9 | 10 | 11 | clear 12 | T=12;x=nan*ones(T+1,4); 13 | x(1:T-6,1)=(7:T); 14 | x(1:T-3,2) = (4:T)'; 15 | x(:,3)=(0:T)'; 16 | x(:,4)=(0:T)'; 17 | c=[4,5,3,6]; 18 | [y,fval]=dyprog(x,c,@decisfun4,@subfun3,@trafun3); 19 | -------------------------------------------------------------------------------- /第6章/例6.4/decisfun6.m: -------------------------------------------------------------------------------- 1 | function [u1,u2]=decisfun6(k,x1,x2) 2 | w=[3 4 5];v=[8 6 4]; 3 | u1=0:1:min(x1/w(k),x2/v(k)); 4 | u2=1; 5 | -------------------------------------------------------------------------------- /第6章/例6.4/dyprog2.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.4/dyprog2.m -------------------------------------------------------------------------------- /第6章/例6.4/subfun6.m: -------------------------------------------------------------------------------- 1 | function f=subfun6(k,x1,x2,u1,u2) 2 | c=[4 5 6]; 3 | f=-c(k)*u1; 4 | -------------------------------------------------------------------------------- /第6章/例6.4/trafun6.m: -------------------------------------------------------------------------------- 1 | function s=trafun6(k,x1,x2,u1,u2) 2 | w=[3 4 5];v=[8 6 4]; 3 | s(1)=x1-u1*w(k); 4 | s(2)=x2-u1*v(k); 5 | 6 | -------------------------------------------------------------------------------- /第6章/例6.4/例6.4.txt: -------------------------------------------------------------------------------- 1 | a1=0:10;b1=0:22;s1=nan*ones(11,1);s1(1)=10;s2=nan*ones(23,1);s2(1)= 22; 2 | x1=[s1 a1' a1'];x2=[s2 b1' b1']; 3 | [y,fval]=dyprog2(x1,x2,@decisfun6,@subfun6,@trafun6); 4 | -------------------------------------------------------------------------------- /第6章/例6.5/decisfun5.m: -------------------------------------------------------------------------------- 1 | function [u1,u2]=decisfun5(k,x1,x2) 2 | u1=0:1:x1;u2=0:1:x2; 3 | -------------------------------------------------------------------------------- /第6章/例6.5/subfun5.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.5/subfun5.m -------------------------------------------------------------------------------- /第6章/例6.5/trafun5.m: -------------------------------------------------------------------------------- 1 | function s=trafun5(k,x1,x2,u1,u2) 2 | s(1)=x1-u1;s(2)=x2-u2; 3 | -------------------------------------------------------------------------------- /第6章/例6.5/例6.5.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | a1=0:3;b1=0:3;s1=nan*ones(4,1);s1(1)=3;s2=nan*ones(4,1);s2(1)=3; 3 | x1=[s1 a1' a1'];x2=[s2 b1' b1']; 4 | [y,fval]=dyprog2(x1,x2,@decisfun5,@subfun5,@trafun5); 5 | -------------------------------------------------------------------------------- /第6章/例6.6/decisfun7.m: -------------------------------------------------------------------------------- 1 | function u=decisfun7(k,x) 2 | q=10*[6 7 12 6]; 3 | if q(k)-x<0 4 | u=0:100; 5 | else 6 | u=q(k)-x:100; 7 | end 8 | u=u(:); -------------------------------------------------------------------------------- /第6章/例6.6/subfun7.m: -------------------------------------------------------------------------------- 1 | function v=subfun7(k,x,u,c) 2 | v=c(k)*u+2*x; -------------------------------------------------------------------------------- /第6章/例6.6/trafun7.m: -------------------------------------------------------------------------------- 1 | function y=trafun7(k,x,u) 2 | q=10*[6 7 12 6]; 3 | y=x+u-q(k); -------------------------------------------------------------------------------- /第6章/例6.6/例6.6.txt: -------------------------------------------------------------------------------- 1 | clear 2 | x=nan*ones(14,4);c=[70 72 80 76]; 3 | x(1:7,1)=10*(0:6)';x(1:11,2)=10*(0:10)';x(1:12,3)=10*(2:13)';x(1:7,4)=10*(0:6)'; 4 | [y,fval]=dyprog(x,c,@decisfun7,@subfun7,@trafun7); 5 | 6 | clear; 7 | Aeq=[1 zeros(1,8) -10 0 0 0;0 1 zeros(1, 7) 0 -10 0 0; 0 0 1 zeros(1,6) 0 0 -10 0; 8 | 0 0 0 1 zeros(1,5) 0 0 0 -10;zeros(1,4) 1 zeros(1,8);1 0 0 0 1 -1 zeros(1,7); 9 | 0 1 0 0 0 1 -1 zeros(1,6);0 0 1 0 0 0 1 -1 zeros(1,5);0 0 0 1 0 0 0 1 -1 zeros(1,4); 10 | 0 0 0 0 0 0 0 0 1 0 0 0 0]; 11 | LB=[0 0 0 0 0 0 0 0 0 0 0 0 0]'; 12 | UB=[100 100 100 100 100 100 100 100 0 10 10 10 10]'; 13 | f=[70 72 80 76 2*ones(1,5) 0 0 0 0];Beq=[0;0;0;0;20;60;70;120;60;0]; 14 | [x,val]=intprog(f,[],[],[10 11 12 13],Aeq,Beq,LB,UB); 15 | 16 | -------------------------------------------------------------------------------- /第6章/例6.7/decisfun8.m: -------------------------------------------------------------------------------- 1 | function u=decisfun8(k,x) 2 | u=0:x; -------------------------------------------------------------------------------- /第6章/例6.7/objfun8.m: -------------------------------------------------------------------------------- 1 | function y=objfun8(v,f) 2 | y=max(v,f); -------------------------------------------------------------------------------- /第6章/例6.7/subfun8.m: -------------------------------------------------------------------------------- 1 | function v=subfun8(k,x,u,t) 2 | if k==5 3 | v=0; 4 | else 5 | v=u*t(k); 6 | end 7 | -------------------------------------------------------------------------------- /第6章/例6.7/trafun8.m: -------------------------------------------------------------------------------- 1 | function y=trafun8(k,x,u) 2 | y=x-u; 3 | -------------------------------------------------------------------------------- /第6章/例6.7/例6.7.txt: -------------------------------------------------------------------------------- 1 | clear 2 | n=500;x1=[n;nan*ones(n,1)];x2=0:n;x3=[0;nan*ones(n,1)]; 3 | x=[x1 x2' x2' x2' x3]; t=[0.5;0.1;0.2;0.05]; 4 | [y,fval]=dyprog(x,t,@decisfun8,@subfun8,@trafun8,@objfun8); 5 | -------------------------------------------------------------------------------- /第6章/例6.8/decisfun9.m: -------------------------------------------------------------------------------- 1 | function u=decisfun9(k,x) 2 | if k==5 3 | u=x; 4 | else 5 | u=1:x-1; 6 | end 7 | u=u(:); -------------------------------------------------------------------------------- /第6章/例6.8/optifun10.m: -------------------------------------------------------------------------------- 1 | function f=optifun10(x) 2 | f=-prod(x(6:10)); -------------------------------------------------------------------------------- /第6章/例6.8/optifun11.m: -------------------------------------------------------------------------------- 1 | function [C,Ceq]=optifun11(x) 2 | C=[]; 3 | Ceq=[1-0.1^x(1)-x(6);1-0.2^x(2)-x(7);1-0.5^x(3)-x(8);1-0.3^x(4)-x(9);1-0.4^x(5)-x(10)]; -------------------------------------------------------------------------------- /第6章/例6.8/subfun9.m: -------------------------------------------------------------------------------- 1 | function v=subfun9(k,x,u,c) 2 | v=1-(1-c(k)).^u; 3 | v=-v; -------------------------------------------------------------------------------- /第6章/例6.8/trafun9.m: -------------------------------------------------------------------------------- 1 | function y=trafun9(k,x,u) 2 | y=x-u; -------------------------------------------------------------------------------- /第6章/例6.8/例6.8.txt: -------------------------------------------------------------------------------- 1 | clear 2 | n=15; x1=[n;nan*ones(n-1,1)];x2=1:n;x2=x2';x=[x1 x2 x2 x2 x2];c=[0.9 0.8 0.5 0.7 0.6]; 3 | [y,fval]=dyprog(x,c,@decisfun9,@subfun9,@trafun9,@objfun9); 4 | 5 | 6 | x1=[3*ones(1,5) 0.5*ones(1,5)]'; 7 | Aeq=[ones(1,5) zeros(1,5)];Beq=15; 8 | LB=[ones(1,5) 0.5*ones(1,5)];UB=[10*ones(1,5) ones(1,5)]; 9 | options=optimset('Algorithm','sqp','display','off'); 10 | [x,fval]=fmincon(@optifun10,x1,[],[],Aeq,Beq,LB,UB,@optifun11,options); 11 | for i=1:5;x(i)=round(x(i));end 12 | x(6)=1-0.1^x(1);x(7)=1-0.2^x(2);x(8)=1-0.5^x(3);x(9)=1-0.3^x(4);x(10)=1-0.4^x(5); 13 | f=x(6)*x(7)*x(8)*x(9)*x(10); 14 | -------------------------------------------------------------------------------- /第6章/例6.9/dyprogTSP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.9/dyprogTSP.m -------------------------------------------------------------------------------- /第6章/例6.9/findpos1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第6章/例6.9/findpos1.m -------------------------------------------------------------------------------- /第6章/例6.9/例6.9.txt: -------------------------------------------------------------------------------- 1 | clear 2 | d=[0 8 5 6;6 0 8 5;7 9 0 5;9 7 8 0]; 3 | [road,f]=dyprogTSP(d); 4 | -------------------------------------------------------------------------------- /第7章/例7.1/greedy.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.1/greedy.m -------------------------------------------------------------------------------- /第7章/例7.1/例7.1.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[2 3 4 5];f=[3 4 5 7];b=9; 3 | y=greedy(f,A,b); 4 | -------------------------------------------------------------------------------- /第7章/例7.2/findeye.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.2/findeye.m -------------------------------------------------------------------------------- /第7章/例7.2/gomory.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.2/gomory.m -------------------------------------------------------------------------------- /第7章/例7.2/mydec.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.2/mydec.m -------------------------------------------------------------------------------- /第7章/例7.2/例7.2.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[-1 1 1 0;24 4 0 1];b=[1;25];c=[-1 -27 0 0]; 3 | [x,val]=gomory(A,b,c); 4 | -------------------------------------------------------------------------------- /第7章/例7.3/例7.3.txt: -------------------------------------------------------------------------------- 1 | clear 2 | f=[-3 -2];A=[2 3;4 2];b=[14;18];Aeq=[];Beq=[];lb=[0;0];ub=[inf;inf];I=[1 2]; 3 | [x,val]=intprog(f,A,b,I,Aeq,Beq,lb,ub); 4 | -------------------------------------------------------------------------------- /第7章/例7.4/例7.4.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | f=[-5 -6 -7 0 0 0 0]; b=[2;0;0;0;60;180];I=[1 2 3 4 5 6 7]; 3 | lb=[0;0;0;0;0;0;0];ub=[inf;inf;inf;1;1;1;1]; 4 | A=[0 0 0 1 1 1 0;1 0 0 -14 0 0 0;0 1 0 0 -10 0 0; 5 | 0 0 1 0 0 -18 0;3 4 2 0 0 0 -100;4 6 2 0 0 0 100]; 6 | [x,val]=intprog(f,A,b,I,[],[],lb,ub); 7 | -------------------------------------------------------------------------------- /第7章/例7.5/例7.5.txt: -------------------------------------------------------------------------------- 1 | f=[-1.5 -1]; A=[0.3 0.5;0.7 0.2];b=[36;40];I=1; 2 | [x,val,stats]=intprog(f,A,b,I,[],[],[],[]); 3 | -------------------------------------------------------------------------------- /第7章/例7.6/mybintprog.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.6/mybintprog.m -------------------------------------------------------------------------------- /第7章/例7.6/mydec2bin.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.6/mydec2bin.m -------------------------------------------------------------------------------- /第7章/例7.6/例7.6.txt: -------------------------------------------------------------------------------- 1 | clear 2 | f=[-21 -25 -27 -37 -19 -33 -25]; 3 | A=[13 18 21 29 11 28 19;1 1 1 0 0 0 0;0 0 0 -1 -1 0 0;0 0 0 0 0 -1 -1]; 4 | b=[700;2;-1;-1]; 5 | [xmin,val]=mybintprog(f,A,b); 6 | -------------------------------------------------------------------------------- /第7章/例7.7/Hungarian.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.7/Hungarian.m -------------------------------------------------------------------------------- /第7章/例7.7/findcount.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.7/findcount.m -------------------------------------------------------------------------------- /第7章/例7.7/lattice.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第7章/例7.7/lattice.m -------------------------------------------------------------------------------- /第7章/例7.7/例7.7.txt: -------------------------------------------------------------------------------- 1 | clear 2 | C=[2 15 13 4;10 4 14 15;9 14 16 13;7 8 11 9]; 3 | [x,val,C]=Hungarian(C); 4 | -------------------------------------------------------------------------------- /第7章/例7.8/例7.8.txt: -------------------------------------------------------------------------------- 1 | C=[7 5 9 8 11;9 12 7 11 9;8 5 4 6 9;7 3 6 9 6;4 6 7 5 11]; 2 | [x,val,C]=Hungarian(C); 3 | -------------------------------------------------------------------------------- /第7章/例7.9/例7.9.txt: -------------------------------------------------------------------------------- 1 | clear 2 | f=[7 5 6 8 9];A=[-3 1 -1 -1 2;-1 -3 1 2 -1;1 1 -3 -1 -1];b=[-2;0;1]; 3 | [x,val]=bintprog(f,A,b); 4 | -------------------------------------------------------------------------------- /第8章/例8.1/myquad.asv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第8章/例8.1/myquad.asv -------------------------------------------------------------------------------- /第8章/例8.1/myquad.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第8章/例8.1/myquad.m -------------------------------------------------------------------------------- /第8章/例8.1/例8.1.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | H=[3 -1 0;-1 2 -1;0 -1 1]; 3 | c=[1;1;1]; 4 | A=[1 2 1]; 5 | b=4; 6 | [x,lamda,minf]=myquad(H,c,A,b) -------------------------------------------------------------------------------- /第8章/例8.2/myquad1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第8章/例8.2/myquad1.m -------------------------------------------------------------------------------- /第8章/例8.2/例8.2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第8章/例8.2/例8.2.txt -------------------------------------------------------------------------------- /第8章/例8.3/newquad.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第8章/例8.3/newquad.m -------------------------------------------------------------------------------- /第8章/例8.3/例8.3.txt: -------------------------------------------------------------------------------- 1 | clear 2 | c=[-2 0 0 0]'; 3 | H=[2 -2 0 0;-2 2 0 0;0 0 0 0;0 0 0 0]; 4 | Ae=[1 1 1 0;1 5 0 1]; 5 | be=[3 6]'; 6 | Ai=eye(4); 7 | bi=[0 0 0 0]; 8 | [xmin,minf,mu,lm]=newquad(H,c,Ae,be,Ai,bi); 9 | -------------------------------------------------------------------------------- /第8章/例8.4/例8.4.txt: -------------------------------------------------------------------------------- 1 | H=[1 -1;-1 2];f=[-2;-6];A=[1 1;-1 2;2 1];b=[2;2;3];lb=zeros(2,1); 2 | options=optimset('Largescale','off'); 3 | [x,fval]=quadprog(H,f,A,b,[],[],lb,[],[],options); 4 | -------------------------------------------------------------------------------- /第9章/例9.1/例9.1.txt: -------------------------------------------------------------------------------- 1 | clear 2 | syms x1 x2 3 | x_syms=[sym(x1) sym(x2)]; 4 | fun=0.3*x1*x2+0.7*x1^2+0.7*x2^2; 5 | gfun=[2.5-x1;x1-x2;x1;x2;x1^2*x2-1;4*x2-x1];hfun=[];x0=[1 1]; 6 | [xmin,minf]=newsqp1(fun,hfun,gfun,x0,x_syms); 7 | -------------------------------------------------------------------------------- /第9章/例9.2/例9.2.txt: -------------------------------------------------------------------------------- 1 | clear; 2 | syms x1 x2 x3 3 | fun=x1+x2+x3-208; 4 | gfun=[240-3*x1;250-2*x2;420-4*x3;x1+x2+x3-208;x1;x2;x3]; 5 | hfun=[];x0=[1 1 1];x_syms=[sym(x1) sym(x2) sym(x3)]; 6 | [xmin1,minf1]=newsqp1(fun,hfun,gfun,x0,x_syms); 7 | 8 | fun=-15*x1-14*x2-12*x3; 9 | [xmin2,minf2]=newsqp1(fun,hfun,gfun,x0,x_syms); 10 | 11 | fun=-3*x1; 12 | [xmin3,minf3]=newsqp1(fun,hfun,gfun,x0,x_syms); 13 | 14 | A=[0.1 0.1 0.1 -1;-15*0.8 -14*0.8 -12*0.8 -1;-0.3 0 0 -1;3 0 0 0;0 2 0 0;0 0 4 0;-1 -1 -1 0]; 15 | B=[20.8;-4210*0.8;-24;240;250;420;-208];f=[0 0 0 1];LB=[0;0;0;0]; 16 | [x,val]=linprog(f,A,B,[],[],LB,[]); 17 | 18 | -------------------------------------------------------------------------------- /第9章/例9.3/例9.3.txt: -------------------------------------------------------------------------------- 1 | A=[320 350 390;0 0 -130];b=[3410;-156];Aeq=[1 1 1];beq=10; 2 | options=optimset('Algorithm','sqp','display','off'); 3 | [x,fval]=fmincon(@(x)-34*x(1)-32*x(2)-34*x(3),[1 1 1],A,b,Aeq,beq,[0;0;0],[],[],options); 4 | 5 | A=[320 350 390;0 0 -130;-34 -32 -34]; 6 | b=[3410;-156;-340]; 7 | [x,fval]=fmincon(@(x) 50*x(1)+48*x(2)+40*x(3),[1 1 1],A,b,Aeq,beq,[0;0;0],[],[],options); 8 | 9 | -------------------------------------------------------------------------------- /第9章/例9.4/例9.4.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第9章/例9.4/例9.4.txt -------------------------------------------------------------------------------- /第9章/例9.5/例9.5.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第9章/例9.5/例9.5.txt -------------------------------------------------------------------------------- /第9章/例9.6/findeye.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第9章/例9.6/findeye.m -------------------------------------------------------------------------------- /第9章/例9.6/multgoal.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第9章/例9.6/multgoal.m -------------------------------------------------------------------------------- /第9章/例9.6/progturn.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cggos/opt_matlab/4989cbf2e93c9b577235dee61c9d6ca2a947d490/第9章/例9.6/progturn.m -------------------------------------------------------------------------------- /第9章/例9.6/例9.6.txt: -------------------------------------------------------------------------------- 1 | clear 2 | A=[4 5 -1 1 0 0 0 0 0 0;4 2 0 0 -1 1 0 0 0 0;80 100 0 0 0 0 -1 1 0 0;1 1 0 0 0 0 0 0 -1 1]; 3 | b=[80;48;800;7]; 4 | f=[0 0 1 0 1 0 0 0 0 0;0 0 0 0 0 0 0 1 0 0;0 0 0 0 0 0 0 0 0 1]; 5 | minx=multgoal(f,A,b); 6 | -------------------------------------------------------------------------------- /第9章/例9.7/例9.7.txt: -------------------------------------------------------------------------------- 1 | clear 2 | goal=[200 -100 -50];weight=[2040 -100 50];x0=[60 60]; 3 | A=[2 1;-1 -1;-1 0];b=[200 -100 -50];lb=zeros(2,1); 4 | [x,fval]=fgoalattain(@optifun12,x0,goal,weight,A,b,[],[],lb,[]); 5 | --------------------------------------------------------------------------------