The GA Procedure |
Defining an Objective Function |
The GA procedure enables you to specify your objective to be optimized with a function you create, or as a standard objective function that the GA procedure provides. Currently the only standard objective you can specify without writing an objective function is the traveling salesman problem, which can be specified with a SetObj call . In the future, other objective functions will be added. You can designate a user objective function with the call
call SetObjFunc('name', minmax);
where name is the name of the function you have defined, and minmax is set to 0 to specify a minimum or 1 to specify a maximum.
A user objective function must have a numeric array as its first parameter. When the GA procedure calls your function, it passes an array in the first parameter that specifies the selected solution, which is referred to as the selection parameter. The selection parameter must not be altered in any way by your function. Your function should pass the selection parameter to a ReadMember call to read the elements of the selected solution into an array. Your function can then access this array to compute an objective value, which it must return. As with the genetic operator routines, you can define additional arguments to your objective function, and the GA procedure passes in variables with corresponding names that you have created in your global program. For example, the following statements set up an objective function that minimizes the sum of the squares of the solution elements:
call SetEncoding('R5'); n = 5; function sumsq(selected[*], n); /* set up a scratch array to hold solution elements */ array x[1] /nosym; /* allocate x to hold all solution elements */ call dynamic_array(x, n); /* read members of the selected solution into x */ call ReadMember(selected, 1, x); /* compute the sum of the squares */ sum = 0; do i = 1 to n; sq = x[i] * x[i]; sum = sum + sq; end; /* return the objective value */ return(sum); endsub; call SetObjFunc('sumsq', 0);
In this example, the function SUMSQ is defined, and the SetObjFunc call establishes it as the objective function. The 0 for the second parameter of the SetObjFunc call indicates that the objective should be minimized. Note that the second parameter to the SUMSQ function, n, is defined in the procedure, and the value assigned to it there is passed into the function.
Copyright © SAS Institute, Inc. All Rights Reserved.