Chapter 5
A Complex Optimization

§5.2 Optimization by General-Purpose Methods

maxerror[{a_, b_, c_, d_}] := 
        Max[Abs[a - b + c - 
              d], 
FindMaximum[
                  t = (E^(I θ)); 
                  Abs[a (t^3) + b (t^2) + c t + d - 
                      1/Gamma[t]], 
 {θ, # - 
                      0.03, # + 0.03}, 
                  PrecisionGoal -> 
                    12][[1]] & /@ {1.40, 2.26}];

maxerror[{-0.603, 0.625, 1.02, 0.006}]

DifferentialEvolution[n_, seeds_] := 
        Module[{best, current, children, vals, 
            childval}, 
current = 
            Table[Random[Real, #] &  /@ 
                seeds, {n}]; 
vals = 
            maxerror /@ current; 
oldbest = 
            Min[vals]; 
Do[
            children = 
              Table[{1, 0.4, -0.4} . 
                  current[[
                    Table[Random[
                        Integer, {1, n}], {3}]]], {n}]; 
            Do[If[(childval = 
                      maxerror[
                        children[[
                          j]]]) < 
                  vals[[
                    j]], {current[[
                      j]], 
                    vals[[
                      j]]} = {children[[
                      j]], childval}], {j, 
                n}]; 
If[Mod[i, 10] == 0, 
              best = Min[vals]; 
If[
                Abs[best - oldbest] < (10^(-14)), Break[], 
                oldbest = best]], {i, 300}]; 
First[
            Sort[Transpose[{vals, current}]]]];

SeedRandom[0];
DifferentialEvolution[
      60,  {{-2, 2}, {-2, 2}, {-2, 2}, {-2, 2}}]