The program was debugged under VC 6.0.
Note: Please indicate the source!
/
/ Vector.h header file /
/ Define vector and its basic operation /
/
#include
#define maxlength 10
// Vector definition
Typedef struct {
INT tag; // Row vector logo. The line vector is 0, the column vector is 1.
INT DIMENSION; // vector dimension
Double ELEM [MAXLENGTH]; / / vector of elements
}
Vector Veccreat (int Tag, int N) {
// Establish a dimension of dim to n
Vector x;
X.TAG = Tag;
x.dimension = n;
For (int i = 0; i Cout << "Please enter the" << i 1 << "component:"; CIN >> X.Elem [I]; } Return X; } Double Vecmultiply (Vector A, Vector B) { // Row vector and column vector multiplication IF ((a.tag! = b.tag) && (a.dimension == b.dimension)) {// multiplication condition Double C = 0; For (int i = 0; i C = a.Elem [i] * B.Elem [i]; Return C; } } Vector vecadd (Vector a, vector b) { // Vector addition IF (A.TAG == B. Tag) && (a.dimension == b.dimension)) {// Add condition VECTOR C; C. Dimension = a.dimension; C.TAG = a.tag; For (int i = 0; i C.Elem [i] = a.lem [i] b.elem [i]; Return C; } } Vector Vecconvert (Vector A) { // vector transposition IF (a.tag == 0) a.tag = 1; IF (a.tag == 1) a.tag = 0; Return A; } Double Vecmole (Vector a) { / S Double Sum = 0; For (int i = 0; i SUM = a.Elem [i] * a.elem [i]; SUM = SQRT (SUM); Return SUM; } Vector Nummultiply (double n, vector a) { // number multiplier vector For (int i = 0; i a.lem [i] = n * a.elem [i]; Return A; } Void showpoint (Vector X, Double F) { // Display point, solution. Cout << "--- x = ("; For (int i = 0; i COUT << x.elem [i]; IF (i! = x.dimension-1) cout << "; } COUT << "--- f (x) =" << f << endl; cout << end1 } / // function.h header file / The function of the function and its derivative is completed here. / // * No content question * // // Target function - modified in Vecfun (VECTOR VX), X1 is changed to X [1] // // x2 is changed to X [2], according to this type ... // / #include #include "vector.h" #define size 10 #define max 1e300 Double min (double A, double b) { // Seeking two mins RETURN A
} Double Max (Double A, Double B) { // Search two numbers RETURN A> B? A: B; } Vector Vecgrad (Double (* PF) (Vector X), Vector Pointx) { / / Solve the gradient of vector // Use the Richard Expert Defining Algorithm to calculate the gradient GRAD of the POINTX point; Vector grad; grad.tag = 1; grad.dimension = pointx.dimension; // Initializing the bias guide Vector TemppNT1, Temppnt2; // Temporary vector Double H = 1e-3; For (int i = 0; i TEMPPNT1 = TEMPPNT2 = POINTX; Temppnt1.elem [i] = 0.5 * h; Temppnt2.elem [i] - = 0.5 * h; Grad.elem [i] = (4 * (PF (TemppNT1) -pf (TemppNT2))) / (3 * h); Temppnt1.elem [i] = 0.5 * h; Temppnt2.elem [i] - = 0.5 * h; Grad.elem [i] - = (PF (TemppNT1) -pf (TemppNT2)) / (6 * h); } Return Grad; } Double Vecfun (Vector VX) { // Optimal target multi-diversity function Double x [size]; For (INT i = 0; i X [i 1] = vx.elem [i]; // ---------- Constrained target function -------------- // returnx x [1] * x [1] x [2] * x [2]; // ---------- No constraint correct function -------------- // returnx x [1] * x [1] 4 * x [2] * x [2] 9 * x [3] * x [3] -2 * x [1] 18 * x [3] ; // case // ---------- Unconstrained non-quadruple function -------------- // RETURN (1-x [1]) * (1-x [1]) (1-x [4]) * (1-x [4]) (x [1] * x [1] - X [2]) * (x [1] * x [1] -X [2]) (x [2] * x [2] -X [3]) * (x [2] * x [2] -X [3]) (x [3] * x [3] - X [4]) * (x [3] * x [3] -X [4]); // Example 2 } Double Vecfun_SI (Vector VX) { // inequality constraint function Double x [size]; For (INT i = 0; i X [i 1] = vx.elem [i]; returnix x [1] 1; // inequality constraint function } Double Vecfun_hi (Vector VX) { // Equation constraint function Double x [size]; For (INT i = 0; i X [i 1] = vx.elem [i]; Return X [1] X [2] -2; // Equation Constraint Function } Double Vecfun_s (Vector X, Double V, Double L, Double U) { // Conversion of Conversion to Conversion Target Functions f (x, v, l, u) Double Sum1 = 0, SUM2 = 0, SUM3 = 0; // Declining three items // Sum1 Double Temp = Max (0, V-2 * U * VECFUN_SI (X)); SUM1 = (Temp * Temp-V * V) / (4 * u); // sum2 SUM2 = L * VECFUN_HI (X); // sum3 SUM3 = U * VECFUN_HI (X) * VECFUN_HI (X); // f (x, v, l, u) = f (x) Sum1-sum2 sum3 RETURN VECFUN (X) SUM1-SUM2 SUM3; } Vector VECFUND_S (Vector X, Double V, Double L, Double U) {// Using the Overloaded Function to Implement the Target Letter Division of num f (x, v, l, u) // Conversion to the Conversion of the Conversion Target F (X, V, L, U) of Unconstrained Problems Vector Sum1, Sum2, Sum3; // Define the three derivatives // Sum1 Sum1.dimension = x.dimension; Sum1.tag = 1; Sum2.dimension = x.dimension; Sum2.tag = 1; Sum3.dimension = x.dimension; sum3.tag = 1; Double Temp = Max (0, V-2 * U * VECFUN_SI (X)); IF (TEMP == 0) { For (int i = 0; i Sum1.elex [i] = 0; } Else { SUM1 = Nummultiply (- (V-2 * U * VECFUN_SI (X)), VECGRAD (VECFUN_SI, X)); } // - sum2 Sum2 = Nummultiply (-L, VECGRAD (VECFUN_HI, X)); // sum3 Sum3 = Nummultiply (2 * u * vecfun_hi (x), vecgrad (vecfun_hi, x)); // f = f (x) Sum1-sum2 sum3 Return Vecadd (VecAdd (Vecgrad (VECGRAD (VECFUN, X), SUM1), VECADD (SUM2, SUM3)); } /// / NonRestrict.h header file / / Contains a solving function with unconstrained issues: line search / / Conjugated gradient method, H termination standard / /// #include "restrict.h" Vector LINESEARCH (Vector X, Vector P, Double T) { // From point X, the target function F (X) is searched to the target function f (x) to obtain the extreme small point X2, T is the initial step length. Vector x2; Double L = 0.1, n = 0.4; // Conditions 1 and 2 parameters Double A = 0, B = max; // Double F1 = 0, F2 = 0, G1 = 0, G2 = 0; INT i = 0; // Iterative number Do { IF (F2-F1> L * T * G1) {b = t; t = (a b) / 2;} // change B, T cycle Do { IF (g2 F1 = vecfun (x); // f1 = f (x) G1 = vecmultiply (Vecconvert (vecgrad (vecgram (vecfun, x)), p); // g1 = ∨F (x) * P X2 = vecadd (Nummultiply (t, p), x); // x2 = x t *p IF (i && vecfun (x2) == f2) {RETURN X2;} // [Line Search " The decline is limited to the end. Return the current best point] F2 = vecfun (x2); // f2 = f (x2) G2 = vecmultiply (Vecconvert (Vecgrad (VECFUN, X2)), P); // g2 = ∨F (x2 * P //cout<<"("< .Elem [3] << ");"; // Output this result // cout << "t =" << t << "; // cout << "f (x) =" << f2 << endl; } While (G2 } While (F2-F1> L * T * G1); // Do not satisfy the condition I, change B, T cycle Return x2; } Int Himmulblau (Vector X0, Vector X1, Double F0, Double F1) { // h Terminate the criteria. Given XK, XK 1, FK, FK 1, determine if the XK 1 is extremely small. Return 1 is extremely small, otherwise returns 0 Double C1, C2, C3; // Definition and initialization termination limit C1 = C2 = 10E-5; C3 = 10E-4; IF (Vecmole (vecfun, x1)) IF (VECMOLE (VECADD (X1, Nummultiply (-1, X0)))) / (VECMOLE (X0) 1) IF (Fabs (F1-F0) / (FABS (F0) 1) Return 1; Return 0; } Void Fletcher_reeves (Vector XX, Vector & Minx, Double & Minf) { // Fletcher-Reeves conjugate gradient method. / / Given the initial point XX. Since the VECFUN function, the best point X and the best solution f, separately return to Minx and Minf INT K = 0, J = 0; // Iterative number, J is the total number of total Double C = 10e-1; // termination C INT n = xx.dimension; // Quality Dimension Double F0, F, A; // Function F (x), A is a coefficient of coefficients for the P-direction vector conjugate Vector G0, G; // gradient G0, g Vector P0, P; // Search Direction P0, P Vector x, x0; // optimal points and initial points Double T = 1; // Straight line search initial step length = 1 X = xx; // x0 f = vecfun (x); // f0 = f (x0) g = vecgrad (vecfun, x); // g0 P0 = Nummultiply (-1, g); // p0 = -G0, the initial search direction is the negative direction Do { X0 = x; f0 = f; g0 = g; X = lineSearch (x0, p0, t); // start from point x, along the P direction f = vecfun (x); // f = f (x) g = vecgrad (vecfun, x); // g = g (x) IF (Himmulblau (X0, X, F0, F) == 1) {// Meet H Targetics, return the most advantages and optimal solutions. COUT << "* The total number of generals:" << j << "----" << Endl; Minx = x; minf = f; Break; } Else {// Does not meet H criticism COUT << "*" << j << "iterative"; Showpoint (x, f); // Display intermediate results x, f IF (k == n) {// If n 1 iteration is performed K = 0; // Reset k = 0, p0 = -g P0 = Nummultiply (-1, g); T = vecmole (vecadd (x, nummultiply (-1, x0))) / vecmole (p0); // Next line Search Continue; // Enter the next loop, back straight search } ELSE {/ / Otherwise, calculate the conjugate vector P A = VECMOLE (G) * VECMOLE (G) / (VECMOLE (G0) * VECMOLE (G0)); P = vecadd (Nummultiply (-1, g), Nummultiply (A, P0)); } } IF (Vecmultiply (Vecconvert (P), G)) <= c) {// conjugate gradient direction or increased amount small P0 = Nummultiply (-1, g); // p0 = -g, k = 0 K = 0; } ELSE IF (Vecmultiply (Vecconvert (p), g) <- c) {// conjugate gradient direction decreased and lowered certificate P0 = p; // p0 = p, k = k 1 K; } Else {// conjugate gradient rises and lowered certificate P0 = Nummultiply (-1, p); // p0 = -p, k = k 1 K; } T = vecmole (vecadd (x, nummultiply (-1, x0))) / vecmole (p0); // Next line search step size } While ( J); } /// / Main.h header file / / Main program file, control flow of the entire program / /// #include "nonrestrict.h" Void printselect () { Cout << "*********************** << Endl; COUT << "***************************************" << endl; Cout << "*** Please select: ***" << Endl; COUT << "*** 1. Unconstrained minimization problem (conjugate gradient method) *** << endl; Cout << "*** 2. Constrained minimization problem (H multiplier method) *** << endl; COUT << "*** 3. Exit (exit) ***" << endl; COUT << "***************************************" << endl; } Void Sub1 () { CHAR Key; Cout << "------------------- Conjugate gradient regular unconstrained minimization problem ---------------------------------------------------------------------------------- "<< ENDL; Cout << "step:" << Endl; Cout << "◆ 1. Enter f (x) and its derivatives. (Refer to Function.h file description)" << ENDL; Cout << "----- Confirm that CIN >> KEY; IF (key == 'n' || key == 'n') return; Else IF (key == 'y' || key == 'y') { Vector x0; // starting point Int m; COUT << "◆ 2. Start point X0 initialization" << Endl << "----- Please enter the dimension of X0:"; Cin >> m; X0 = VECCREAT (1, M); Vector minx; // Survey minus point Double minf; // Survey minimum value Fletcher_reeves (x0, minx, minf); COUT << "◆ The best solution and the best value:"; Showpoint (minx, minf); } } Void Sub2 () { CHAR Key; COUT << "---------------- H multiplier method for the minimization problem -----------------" < ; Cout << "step:" << Endl; Cout << "◆ 1. Enter f (x) and its derivatives. (Refer to Function.h file description)" << ENDL; Cout << "---- confirms if CIN >> KEY; IF (key == 'n' || key == 'n') Return; Else IF (key == 'y' || key == 'y') { Vector x0; // starting point Int m; COUT << "◆ 2. Start point X0 initialization" << Endl << "----- Please enter the dimension of X0:"; Cin >> m; X0 = VECCREAT (1, M); Vector minx; // Survey minus point Double minf; // Survey minimum value Hesternes (X0, minx, minf); Showpoint (minx, minf); } } Void main () { INT MARK = 1; While (mark) { PRINTSELECT (); Int Sel; CIN >> SEL; Switch (sel) { Case 1: SUB1 (); Break; Case 2: SUB2 (); Break; Case 3: Mark = 0; Break; } } }