Penalty function method for external points-constraints Optimization

Source: Internet
Author: User
Penalty function method for external points-constraints Optimization

The vertices penalty function (r increase, SUMT. Java) is used to solve the constraints optimization problem. The solution steps are as follows:

Step 1 input the target function and the constraint equation, construct the punishment Function Method of the outer point to solve the equation, and initialize the solution.

Step 2 perform a non-constrained optimization (Powell BWE) on the equation to obtain a new solution.

Step 3: the error between the new solution and the original solution is obtained. If the error meets the accuracy requirement, the solution is output. Otherwise, the factor R is added and Step 2 is executed.

BWE. Java (BWE. Java) is a n-dimensional unrestricted solution. One-dimensional solutions need to be called. One-dimensional solutions use the golden division method (GSM. Java ).

In the code that implements the algorithm, I removed the input processing and manually determined the input to reduce the code length.

I will package the files into my downloads. You are welcome to discuss them together.

(1) Penalty Function SUMT. Java:

Package ODM. method; import Java. util. arrays;/** unrestricted optimization method: penalty function method-External vertex Method */public class SUMT {private int n = 6; // dimension, number of variables private final double EPS = 1e-5; // precision private final double C = 5; // increment coefficient private double r = 0.1; // Penalty Factor, tends to infinite public SUMT () {finit (); algorithmprocess (); answeroutput () ;}// result private double [] Xs; private double ans; private void finit () {xs = new double [N]; arrays. fill (XS, 0); ans =-1; // Xs [0] = Xs [1] = Xs [2] = Xs [4] = 1; xs [3] = 3; Xs [5] = 5;} // main algorithm flow private void algorithmprocess () {int icnt = 0; // number of iterations double [] x = new double [N]; // convert it to a solution for the unrestricted optimization problem while (true) {icnt ++; BWE temp = new BWE (n, R, XS); // use the powell method to find the optimal function solution x = temp. retans (); If (retok (x) <= EPS) {// meets the accuracy requirement for (INT I = 0; I <n; I ++) xs [I] = x [I]; ans = temp. mans (); break;} r = C * r; For (INT I = 0; I <n; I ++) XS [I] = x [I];} system. out. println ("iterations:" + icnt);} // convergence condition (only one, not perfect) Private double retok (double [] X) {double sum = 0; for (INT I = 0; I <n; I ++) {sum + = math. pow (X [I]-xs [I], 2);} return math. SQRT (SUM) ;}// the result output private void answeroutput () {for (INT I = 0; I <n; I ++) system. out. printf ("%. 6f \ t ", xs [I]); system. out. printf ("%. 6f \ n ", ANS);} public static void main (string [] ARGs) {// todo auto-generated method stubnew SUMT ();}}

(2) Bao wilfa BWE. Java:

Package ODM. method; import Java. util. arrays; public class BWE {private double r; // initialization variable private double [] x0; // initial solution set private double [] [] E; // initial direction private int N; final private double EPS = 1e-5; private func F; // initialization: Initial Point, initial vector (N, N * n matrix ), dimension private void Init (int n) {This. x0 = new double [N]; If (r =-1) arrays. fill (this. x0, 0); else {} This. E = new double [N] [N]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j + +) {If (I! = J) E [I] [J] = 0; else E [I] [J] = 1 ;}} this. n = N; If (R! =-1) F = new func (r); elsef = new func ();} // search point, direction vector private double [] [] X; private double [] [] D; // reschedule the queue, queue operation private void queuedir (double [] X) {// Delete the first direction for (INT I = 0; I <N-1; I ++) {for (Int J = 0; j <n; j ++) {d [I] [J] = d [I + 1] [J] ;}// Insert a new direction to the end of the Team for (INT I = 0; I <N; I ++) d [N-1] [I] = x [I];} private void process () {x = new double [n + 1] [N]; D = new double [N] [N]; for (Int J = 0; j <n; j ++) x [0] [J] = x0 [J]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {d [I] [J] = E [I] [J] ;}} int K = 0; // number of iterations while (k <n) {for (INT I = 1; I <= N; I ++) {GSM T = new GSM (f, x [I-1], d [I-1]); X [I] = T. getos () ;}double [] x = new double [N]; for (INT I = 0; I <n; I ++) X [I] = x [N] [I]-X [0] [I]; queuedir (x); GSM T = new GSM (f, x [N], x); X [0] = T. getos (); k ++ ;}// print private void answeroutput () {for (INT I = 0; I <n; I ++) {system. out. printf ("X [% d] = %. 6f \ n ", I + 1, x [0] [I]); // system. out. print (X [0] [I] + "");} system. out. printf ("minimum value: %. 6f \ n ", F. fgetval (X [0]); // system. out. println (":" + F. fgetval (X [0]);} public BWE (int n) {This. R =-1; Init (n); process (); answeroutput ();} public BWE (int n, Double R, double [] X) {This. R = r; Init (n); For (INT I = 0; I <n; I ++) x0 [I] = x [I]; process ();} // return result, solution vector and optimal value public double [] retans () {return X [0];} public double Mans () {return F. fgetval (X [0], 0);}/* Public static void main (string [] ARGs) {// todo auto-generated method stubnew BWE (2 );} */}

(3) Golden division GSM. Java:

Package ODM. method;/** golden Division Method */public class GSM {private int N; // dimension private final double Landa = (math. SQRT (5)-1)/2; // 0.618 private double [] x1; private double [] X2; private double [] OS; private final double EPS = 1e-5; // encryption private extm em; // used to obtain the external push result // The optimal value output public double [] getos () {return OS;} // function, initial point, direction vector public GSM (func sample, double [] X, double [] e) {// For (INT I = 0; I <E. length; I ++) System. Out. print (E [I] + ""); system. out. println (); initial (sample, X, e); process (sample); answerprint (sample) ;}// print the private void answerprint (func sample) Result) {OS = new double [N]; for (INT I = 0; I <n; I ++) OS [I] = 0.5*(x1 [I] + x2 [I]); // system. out. println ("OS =" + OS [0] + "" + OS [1]); // system. out. println ("Ans =" + sample. fgetval (OS);} // vector fan value private double fanzhi (double [] B, double [] A) {double sum = 0; for (I NT I = 0; I <n; I ++) {If (B [I]-A [I]! = 0 & B [I] = 0) return EPS * (1e10); If (B [I] = 0) continue; sum + = math. pow (B [I]-A [I])/B [I], 2);} return math. pow (sum, 0.5);} // algorithm main flow private void process (func sample) {double [] xx1 = new double [N]; subarrayscopy (xx1 ); double YY1 = sample. fgetval (xx1); double [] xx2 = new double [N]; addarrayscopy (xx2); double yy2 = sample. fgetval (xx2); // iteration process while (true) {If (YY1> = yy2) {arrayscopy (xx1, X1); arrayscopy (xx2, xx1); YY1 = yy2; addarrayscopy (xx2); yy2 = sample. fgetval (xx2);} else {arrayscopy (xx2, X2); arrayscopy (xx1, xx2); yy2 = YY1; subarrayscopy (xx1); YY1 = sample. fgetval (xx1);} // system. out. println (fanzhi (X2, X1) + "/" + math. ABS (yy2-YY1)/yy2); If (fanzhi (X2, X1) <EPS & math. ABS (yy2-YY1) <EPS) Break ;}// obtain the external push Method Result: The left and right boundary Private void initial (func sample, double [] X, double [] E) {n = x. length; em = new extm (sample, X, e); X1 = em. getx1 (); x2 = em. getx3 () ;}// Vector Value assignment private void arrayscopy (double [] S, double [] e) {for (INT I = 0; I <n; I ++) E [I] = s [I];} // + landdeskvate void addarrayscopy (double [] ARR) {for (INT I = 0; I <n; I ++) arr [I] = x1 [I] + Landa * (X2 [I]-X1 [I]);} //-land1_vate void subarrayscopy (double [] ARR) {for (INT I = 0; I <n; I ++) arr [I] = x2 [I]-Landa * (X2 [I]-X1 [I]);}/* Public static void main (string [] ARGs) {// todo auto-generated method stubdouble [] C = {0, 0}; double [] d = {1, 0}; new GSM (New func (), c, d );}*/}


The above algorithm file contains the function equation. When golden splitting, one-dimensional search is used to determine the "High and Low" interval.

Function Equation func. Java, the extension method extm. java.

Func. Java:

Package ODM. method; public class func {private int N; // n-dimension private double [] Left; // function left boundary Private double [] Right; // function right boundary Private Double R; public func () {r =-1;} public func (double r) {This. R = r;} // defines the function and function value and returns public double fgetval (double [] X) {If (R! =-1) return fgetval (x, R); // 10*(X1 + x2-5) ^ 2 + (x1-x2) ^ 2 return 10 * Math. pow (X [0] + X [1]-5, 2) + math. pow (X [0]-X [1], 2); //} private double max (double A, double B) {return A> B? A: B;} public double fgetval (double [] X, double r) {double ret = 0; // function F1 // ret = math. pow (X [0]-5, 2) + 4 * Math. pow (X [1]-6, 2) // + R * (// + math. pow (max (64-x [0] * X [0]-X [1] * X [1], 0), 2) // + math. pow (max (X [1]-X [0]-10, 0), 0) // + math. pow (max (X [0]-10, 0), 2 )//); // function F2 // ret = x [0] * X [0] + X [1] * X [1] + R * (3o[ 0]> 0? [0]: 0) * ([0]> 0? 1x2 [0]: 0); // function f3ret = math. pow (X [0]-X [3], 2) + math. pow (X [1]-X [4], 2) + math. pow (X [2]-X [5], 2) + R * (+ math. pow (max (X [0] * X [0] + X [1] * X [1] + X [2] * X [2]-5, 0 ), 2) + math. pow (max (math. pow (X [3]-3, 2) + X [4] * X [4]-1, 0), 2) + math. pow (max (X [5]-8, 0), 2) + math. pow (max (4-X [5], 0), 2); return ret ;}}

Extm. Java:

Package ODM. method;/** determine the "high-low-high" interval using the external push Method */public class extm {private int N; // function dimension private double [] x1; private double [] X2; private double [] X3; private double Y1; private double Y2; private double Y3; private double H; // step size private double [] D; // vector public double [] getx1 () {return x1;} public double [] getx2 () {return X2;} public double [] getx3 () {return X3 ;} public double geth () {return h;} // function, initial point, direction public extm (func sample, double [] X, double [] e) {initial (sample, x, e); process (sample); answerprint () ;}// initialization stage private void initial (func sample, double [] X, double [] E) {n = x. length; X1 = new double [N]; x2 = new double [N]; X3 = new double [N]; H = 0.01; D = new double [N]; arrayscopy (E, 0, d); // For (INT I = 0; I <D. length; I ++) system. out. print (d [I]); system. out. println (); arrayscopy (x, 0, X1); Y1 = sample. fgetval (X1); arrayscopy (x, H, X2); y2 = sample. fgetval (X2) ;}// loop part private void process (func sample) {If (Y2> Y1) {H =-H; arrayscopy (x1, 0, X3 ); y3 = Y1;} else {arrayscopy (X2, H, X3); Y3 = sample. fgetval (X3);} while (Y3 <Y2) {H = 2 * h; // system. out. println ("H =" + H); arrayscopy (X2, 0, X1); Y1 = Y2; arrayscopy (X3, 0, X2); y2 = Y3; arrayscopy (X2, h, X3); Y3 = sample. fgetval (X3); // system. out. println ("X1 =" + X1 [0] + "" + X1 [1] + "Y1 =" + Y1); // system. out. println ("X2 =" + x2 [0] + "" + x2 [1] + "y2 =" + y2); // system. out. println ("X3 =" + X3 [0] + "" + X3 [1] + "Y3 =" + Y3) ;}// print the algorithm result private void answerprint () {// system. out. println ("X1 =" + X1 [0] + "" + X1 [1] + "Y1 =" + Y1); // system. out. println ("X2 =" + x2 [0] + "" + x2 [1] + "y2 =" + y2); // system. out. println ("X3 =" + X3 [0] + "" + X3 [1] + "Y3 =" + Y3 );} // vector transfer private void arrayscopy (double [] S, double C, double [] e) {for (INT I = 0; I <S. length; I ++) E [I] = d [I] * C + s [I];}/* Public static void main (string [] ARGs) {// todo auto-generated method stub // new extm ();}*/}


Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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