func DTLZ2mGenerator(opt *goga.Optimiser, nf int) (ng int, fcn goga.MinProb_t) { nx := nf + 10 ng = nx * 2 opt.FltMin = make([]float64, nx) opt.FltMax = make([]float64, nx) for i := 0; i < nx; i++ { opt.FltMin[i], opt.FltMax[i] = -0.01, 1.01 } fcn = func(f, g, h, x []float64, ξ []int, cpu int) { var failed bool for i := 0; i < nx; i++ { g[0+i*2] = x[i] g[1+i*2] = 1.0 - x[i] if g[0+i*2] < 0 { failed = true } if g[1+i*2] < 0 { failed = true } } if failed { return } var c float64 for i := nf - 1; i < nx; i++ { c += math.Pow((x[i] - 0.5), 2.0) } for i := 0; i < nf; i++ { f[i] = (1.0 + c) for j := 0; j < nf-1-i; j++ { f[i] *= math.Cos(x[j] * PI / 2.0) } if i > 0 { j := nf - 1 - i f[i] *= math.Sin(x[j] * PI / 2.0) } } } opt.Multi_fcnErr = func(f []float64) float64 { var sum float64 for i := 0; i < nf; i++ { sum += f[i] * f[i] } return sum - 1.0 } opt.RptFmin = make([]float64, nf) opt.RptFmax = make([]float64, nf) for i := 0; i < nf; i++ { opt.RptFmax[i] = 1 } return }
func runone(ncpu int) (nsol, tf int, elaspsedTime time.Duration) { // input filename fn, fnkey := io.ArgToFilename(0, "ground10", ".sim", true) // GA parameters var opt goga.Optimiser opt.Read("ga-" + fnkey + ".json") opt.GenType = "rnd" nsol, tf = opt.Nsol, opt.Tf postproc := true if ncpu > 0 { opt.Ncpu = ncpu postproc = false } // FEM data := make([]*FemData, opt.Ncpu) for i := 0; i < opt.Ncpu; i++ { data[i] = NewData(fn, fnkey, i) } io.Pforan("MaxWeight = %v\n", data[0].MaxWeight) // set integers if data[0].Opt.BinInt { opt.CxInt = goga.CxInt opt.MtInt = goga.MtIntBin opt.BinInt = data[0].Ncells } // set floats opt.FltMin = make([]float64, data[0].Nareas) opt.FltMax = make([]float64, data[0].Nareas) for i := 0; i < data[0].Nareas; i++ { opt.FltMin[i] = data[0].Opt.Amin opt.FltMax[i] = data[0].Opt.Amax } // initialise optimiser opt.Nova = 2 // weight and deflection opt.Noor = 4 // mobility, feasibility, maxdeflection, stress opt.Init(goga.GenTrialSolutions, func(sol *goga.Solution, cpu int) { mob, fail, weight, umax, _, errU, errS := data[cpu].RunFEM(sol.Int, sol.Flt, 0, false) sol.Ova[0] = weight sol.Ova[1] = umax sol.Oor[0] = mob sol.Oor[1] = fail sol.Oor[2] = errU sol.Oor[3] = errS }, nil, 0, 0, 0) // initial solutions var sols0 []*goga.Solution if false { sols0 = opt.GetSolutionsCopy() } // benchmark initialTime := time.Now() defer func() { elaspsedTime = time.Now().Sub(initialTime) }() // solve opt.Verbose = true opt.Solve() goga.SortByOva(opt.Solutions, 0) // post processing if !postproc { return } // check nfailed, front0 := goga.CheckFront0(&opt, true) // save results var log, res bytes.Buffer io.Ff(&log, opt.LogParams()) io.Ff(&res, PrintSolutions(data[0], opt.Solutions)) io.Ff(&res, io.Sf("\n\nnfailed = %d\n", nfailed)) io.WriteFileVD("/tmp/goga", fnkey+".log", &log) io.WriteFileVD("/tmp/goga", fnkey+".res", &res) // plot Pareto-optimal front feasibleOnly := true plt.SetForEps(0.8, 350) if strings.HasPrefix(fnkey, "ground10") { _, ref, _ := io.ReadTable("p460_fig300.dat") plt.Plot(ref["w"], ref["u"], "'b-', label='reference'") } fmtAll := &plt.Fmt{L: "final solutions", M: ".", C: "orange", Ls: "none", Ms: 3} fmtFront := &plt.Fmt{L: "final Pareto front", C: "r", M: "o", Ms: 3, Ls: "none"} goga.PlotOvaOvaPareto(&opt, sols0, 0, 1, feasibleOnly, fmtAll, fmtFront) plt.Gll("weight ($f_0$)", "deflection ($f_1)$", "") //, "leg_out=1, leg_ncol=4, leg_hlen=1.5") if strings.HasPrefix(fnkey, "ground10") { plt.AxisRange(1800, 14000, 1, 6) } // plot selected results ia, ib, ic, id, ie := 0, 0, 0, 0, 0 nfront0 := len(front0) io.Pforan("nfront0 = %v\n", nfront0) if nfront0 > 4 { ib = nfront0 / 10 ic = nfront0 / 5 id = nfront0 / 2 ie = nfront0 - 1 } A := front0[ia] B := front0[ib] C := front0[ic] D := front0[id] E := front0[ie] wid, hei := 0.20, 0.10 draw_truss(data[0], "A", A, 0.17, 0.75, wid, hei) draw_truss(data[0], "B", B, 0.20, 0.55, wid, hei) draw_truss(data[0], "C", C, 0.28, 0.33, wid, hei) draw_truss(data[0], "D", D, 0.47, 0.22, wid, hei) draw_truss(data[0], "E", E, 0.70, 0.18, wid, hei) // save figure plt.SaveD("/tmp/goga", fnkey+".eps") // tex file title := "Shape and topology optimisation. Results." label := "topoFront" document := true compact := true tex_results("/tmp/goga", "tmp_"+fnkey, title, label, data[0], A, B, C, D, E, document, compact) document = false tex_results("/tmp/goga", fnkey, title, label, data[0], A, B, C, D, E, document, compact) return }
func main() { // GA parameters opt := new(goga.Optimiser) opt.Default() opt.Nsol = 6 opt.Ncpu = 1 opt.Tf = 10 opt.EpsH = 1e-3 opt.Verbose = true opt.GenType = "latin" //opt.GenType = "halton" //opt.GenType = "rnd" opt.NormFlt = false opt.UseMesh = true opt.Nbry = 3 // define problem opt.RptName = "9" opt.RptFref = []float64{0.0539498478} opt.RptXref = []float64{-1.717143, 1.595709, 1.827247, -0.7636413, -0.7636450} opt.FltMin = []float64{-2.3, -2.3, -3.2, -3.2, -3.2} opt.FltMax = []float64{+2.3, +2.3, +3.2, +3.2, +3.2} ng, nh := 0, 3 fcn := func(f, g, h, x []float64, ξ []int, cpu int) { f[0] = math.Exp(x[0] * x[1] * x[2] * x[3] * x[4]) h[0] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3] + x[4]*x[4] - 10.0 h[1] = x[1]*x[2] - 5.0*x[3]*x[4] h[2] = math.Pow(x[0], 3.0) + math.Pow(x[1], 3.0) + 1.0 } // check if false { f := make([]float64, 1) h := make([]float64, 3) fcn(f, nil, h, opt.RptXref, nil, 0) io.Pforan("f(xref) = %g (%g)\n", f[0], opt.RptFref[0]) io.Pforan("h0(xref) = %g\n", h[0]) io.Pforan("h1(xref) = %g\n", h[1]) io.Pforan("h2(xref) = %g\n", h[2]) } // initialise optimiser nf := 1 opt.Init(goga.GenTrialSolutions, nil, fcn, nf, ng, nh) // output function T := make([]float64, opt.Tf+1) // [nT] X := utl.Deep3alloc(opt.Nflt, opt.Nsol, opt.Tf+1) // [nx][nsol][nT] F := utl.Deep3alloc(opt.Nova, opt.Nsol, opt.Tf+1) // [nf][nsol][nT] U := utl.Deep3alloc(opt.Noor, opt.Nsol, opt.Tf+1) // [nu][nsol][nT] opt.Output = func(time int, sols []*goga.Solution) { T[time] = float64(time) for j, s := range sols { for i := 0; i < opt.Nflt; i++ { X[i][j][time] = s.Flt[i] } for i := 0; i < opt.Nova; i++ { F[i][j][time] = s.Ova[i] } for i := 0; i < opt.Noor; i++ { U[i][j][time] = s.Oor[i] } } } // initial population fnk := "one-obj-prob9-dbg" //S0 := opt.GetSolutionsCopy() goga.WriteAllValues("/tmp/goga", fnk, opt) // solve opt.Solve() // print if false { io.Pf("%13s%13s%13s%13s%10s\n", "f0", "u0", "u1", "u2", "feasible") for _, s := range opt.Solutions { io.Pf("%13.5e%13.5e%13.5e%13.5e%10v\n", s.Ova[0], s.Oor[0], s.Oor[1], s.Oor[2], s.Feasible()) } } // plot: time series //a, b := 100, len(T) a, b := 0, 1 //len(T) if false { plt.SetForEps(2.0, 400) nrow := opt.Nflt + opt.Nova + opt.Noor for j := 0; j < opt.Nsol; j++ { for i := 0; i < opt.Nflt; i++ { plt.Subplot(nrow, 1, 1+i) plt.Plot(T[a:b], X[i][j][a:b], "") plt.Gll("$t$", io.Sf("$x_%d$", i), "") } } for j := 0; j < opt.Nsol; j++ { for i := 0; i < opt.Nova; i++ { plt.Subplot(nrow, 1, 1+opt.Nflt+i) plt.Plot(T[a:b], F[i][j][a:b], "") plt.Gll("$t$", io.Sf("$f_%d$", i), "") } } for j := 0; j < opt.Nsol; j++ { for i := 0; i < opt.Noor; i++ { plt.Subplot(nrow, 1, 1+opt.Nflt+opt.Nova+i) plt.Plot(T[a:b], U[i][j][a:b], "") plt.Gll("$t$", io.Sf("$u_%d$", i), "") } } plt.SaveD("/tmp/goga", fnk+"-time.eps") } // plot: x-relationships if true { plt.SetForEps(1, 700) ncol := opt.Nflt - 1 for i := 0; i < opt.Nflt-1; i++ { for j := i + 1; j < opt.Nflt; j++ { plt.Subplot(ncol, ncol, i*ncol+j) if opt.UseMesh { opt.Meshes[i][j].CalcDerived(0) opt.Meshes[i][j].Draw2d(false, false, nil, 0) } for k := 0; k < opt.Nsol; k++ { plt.Plot(X[i][k][a:b], X[j][k][a:b], "ls='none', marker='.'") } plt.Gll(io.Sf("$x_%d$", i), io.Sf("$x_%d$", j), "") } } plt.SaveD("/tmp/goga", fnk+"-x.eps") } }
func main() { // GA parameters opt := new(goga.Optimiser) opt.Default() opt.Nsol = 50 opt.Ncpu = 1 opt.Tf = 1000 opt.Nsamples = 10 opt.EpsH = 1e-3 opt.Verbose = false opt.GenType = "latin" opt.NormFlt = false // options for report opt.HistNsta = 6 opt.HistLen = 13 opt.RptFmtF = "%.5f" opt.RptFmtFdev = "%.2e" opt.RptFmtX = "%.3f" opt.RptName = "9" opt.RptFref = []float64{0.0539498478} opt.RptXref = []float64{-1.717143, 1.595709, 1.827247, -0.7636413, -0.7636450} opt.FltMin = []float64{-2.3, -2.3, -3.2, -3.2, -3.2} opt.FltMax = []float64{+2.3, +2.3, +3.2, +3.2, +3.2} ng, nh := 0, 3 fcn := func(f, g, h, x []float64, ξ []int, cpu int) { f[0] = math.Exp(x[0] * x[1] * x[2] * x[3] * x[4]) h[0] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3] + x[4]*x[4] - 10.0 h[1] = x[1]*x[2] - 5.0*x[3]*x[4] h[2] = math.Pow(x[0], 3.0) + math.Pow(x[1], 3.0) + 1.0 } // check if false { f := make([]float64, 1) h := make([]float64, 3) fcn(f, nil, h, opt.RptXref, nil, 0) io.Pforan("f(xref) = %g (%g)\n", f[0], opt.RptFref[0]) io.Pforan("h0(xref) = %g\n", h[0]) io.Pforan("h1(xref) = %g\n", h[1]) io.Pforan("h2(xref) = %g\n", h[2]) return } // initialise optimiser nf := 1 opt.Init(goga.GenTrialSolutions, nil, fcn, nf, ng, nh) // solve //opt.RunMany("/tmp/goga", "functions") opt.RunMany("", "") goga.StatF(opt, 0, true) opts := []*goga.Optimiser{opt} textSize := `\scriptsize \setlength{\tabcolsep}{0.5em}` miniPageSz, histTextSize := "4.1cm", `\fontsize{5pt}{6pt}` nRowPerTab := 9 title := "Constrained single objective problem 9" goga.TexReport("/tmp/goga", "tmp_one-obj-prob9", title, "one-ob-prob9", 1, nRowPerTab, true, false, textSize, miniPageSz, histTextSize, opts) goga.TexReport("/tmp/goga", "one-obj-prob9", title, "one-obj-prob9", 1, nRowPerTab, false, false, textSize, miniPageSz, histTextSize, opts) }
func main() { // GA parameters opt := new(goga.Optimiser) opt.Default() opt.Tf = 500 opt.Nsamples = 1000 opt.Verbose = false opt.GenType = "latin" // enlarge box; add more constraint equations strategy2 := true // options for report opt.HistNsta = 6 opt.HistLen = 13 opt.RptFmtF = "%.5f" opt.RptFmtFdev = "%.2e" opt.RptFmtX = "%.3f" opt.Ncpu = 4 opt.RptName = "2" opt.RptFref = []float64{-15.0} opt.RptXref = []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1} opt.FltMin = make([]float64, 13) opt.FltMax = make([]float64, 13) xmin, xmax := 0.0, 1.0 if strategy2 { xmin, xmax = -0.5, 1.5 } for i := 0; i < 9; i++ { opt.FltMin[i], opt.FltMax[i] = xmin, xmax } opt.FltMin[12], opt.FltMax[12] = xmin, xmax xmin, xmax = 0, 100 if strategy2 { xmin, xmax = -1, 101 } for i := 9; i < 12; i++ { opt.FltMin[i], opt.FltMax[i] = xmin, xmax } ng := 9 if strategy2 { ng += 9 + 9 + 3 + 3 + 2 } fcn := func(f, g, h, x []float64, ξ []int, cpu int) { s1, s2, s3 := 0.0, 0.0, 0.0 for i := 0; i < 4; i++ { s1 += x[i] s2 += x[i] * x[i] } for i := 4; i < 13; i++ { s3 += x[i] } f[0] = 5.0*(s1-s2) - s3 g[0] = 10.0 - 2.0*x[0] - 2.0*x[1] - x[9] - x[10] g[1] = 10.0 - 2.0*x[0] - 2.0*x[2] - x[9] - x[11] g[2] = 10.0 - 2.0*x[1] - 2.0*x[2] - x[10] - x[11] g[3] = 8.0*x[0] - x[9] g[4] = 8.0*x[1] - x[10] g[5] = 8.0*x[2] - x[11] g[6] = 2.0*x[3] + x[4] - x[9] g[7] = 2.0*x[5] + x[6] - x[10] g[8] = 2.0*x[7] + x[8] - x[11] if strategy2 { for i := 0; i < 9; i++ { g[9+i] = x[i] g[18+i] = 1.0 - x[i] } for i := 0; i < 3; i++ { g[27+i] = x[9+i] g[30+i] = 100.0 - x[9+i] } g[33] = x[12] g[34] = 1.0 - x[12] } } // number of trial solutions opt.Nsol = len(opt.FltMin) * 10 // initialise optimiser nf, nh := 1, 0 opt.Init(goga.GenTrialSolutions, nil, fcn, nf, ng, nh) // solve opt.RunMany("", "") goga.StatF(opt, 0, true) opts := []*goga.Optimiser{opt} textSize := `\scriptsize \setlength{\tabcolsep}{0.5em}` miniPageSz, histTextSize := "4.1cm", `\fontsize{5pt}{6pt}` nRowPerTab := 9 title := "Constrained single objective problem 2" goga.TexReport("/tmp/goga", "tmp_one-obj-prob2", title, "one-ob-prob2", 1, nRowPerTab, true, false, textSize, miniPageSz, histTextSize, opts) goga.TexReport("/tmp/goga", "one-obj-prob2", title, "one-obj-prob2", 1, nRowPerTab, false, false, textSize, miniPageSz, histTextSize, opts) }