示例#1
0
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)
}
示例#2
0
文件: ReliabFORM.go 项目: cpmech/goga
func solve_problem(fnkey string, problem int) (opt *goga.Optimiser) {

	// GA parameters
	opt = new(goga.Optimiser)
	opt.Default()

	// options for report
	opt.RptFmtF = "%.4f"
	opt.RptFmtX = "%.3f"
	opt.RptFmtFdev = "%.1e"
	opt.RptWordF = "\\beta"
	opt.HistFmt = "%.2f"
	opt.HistNdig = 3
	opt.HistDelFmin = 0.005
	opt.HistDelFmax = 0.005

	// FORM data
	var lsft LSF_T
	var vars rnd.Variables

	// simple problem or FEM sim
	if fnkey == "simple" {
		opt.Read("ga-simple.json")
		opt.ProbNum = problem
		lsft, vars = get_simple_data(opt)
		fnkey += io.Sf("-%d", opt.ProbNum)
		io.Pf("\n----------------------------------- simple problem %d --------------------------------\n", opt.ProbNum)
	} else {
		opt.Read("ga-" + fnkey + ".json")
		lsft, vars = get_femsim_data(opt, fnkey)
		io.Pf("\n----------------------------------- femsim %s --------------------------------\n", fnkey)
	}

	// set limits
	nx := len(vars)
	opt.FltMin = make([]float64, nx)
	opt.FltMax = make([]float64, nx)
	for i, dat := range vars {
		opt.FltMin[i] = dat.Min
		opt.FltMax[i] = dat.Max
	}

	// log input
	var buf bytes.Buffer
	io.Ff(&buf, "%s", opt.LogParams())
	io.WriteFileVD("/tmp/gosl", fnkey+".log", &buf)

	// initialise distributions
	err := vars.Init()
	if err != nil {
		chk.Panic("cannot initialise distributions:\n%v", err)
	}

	// plot distributions
	if opt.PlotSet1 {
		io.Pf(". . . . . . . .  plot distributions  . . . . . . . .\n")
		np := 201
		for i, dat := range vars {
			plt.SetForEps(0.75, 250)
			dat.PlotPdf(np, "'b-',lw=2,zorder=1000")
			//plt.AxisXrange(dat.Min, dat.Max)
			plt.SetXnticks(15)
			plt.SaveD("/tmp/sims", io.Sf("distr-%s-%d.eps", fnkey, i))
		}
		return
	}

	// objective function
	nf := 1
	var ng, nh int
	var fcn goga.MinProb_t
	var obj goga.ObjFunc_t
	switch opt.Strategy {

	// argmin_x{ β(y(x)) | lsf(x) ≤ 0 }
	//  f ← sqrt(y dot y)
	//  g ← -lsf(x) ≥ 0
	//  h ← out-of-range in case Transform fails
	case 0:
		ng, nh = 1, 1
		fcn = func(f, g, h, x []float64, ξ []int, cpu int) {

			// original and normalised variables
			h[0] = 0
			y, invalid := vars.Transform(x)
			if invalid {
				h[0] = 1
				return
			}

			// objective value
			f[0] = math.Sqrt(la.VecDot(y, y)) // β

			// inequality constraint
			lsf, failed := lsft(x, cpu)
			g[0] = -lsf
			h[0] = failed
		}

	// argmin_x{ β(y(x)) | lsf(x) = 0 }
	//  f  ← sqrt(y dot y)
	//  h0 ← lsf(x)
	//  h1 ← out-of-range in case Transform fails
	case 1:
		ng, nh = 0, 2
		fcn = func(f, g, h, x []float64, ξ []int, cpu int) {

			// original and normalised variables
			h[0], h[1] = 0, 0
			y, invalid := vars.Transform(x)
			if invalid {
				h[0], h[1] = 1, 1
				return
			}

			// objective value
			f[0] = math.Sqrt(la.VecDot(y, y)) // β

			// equality constraint
			lsf, failed := lsft(x, cpu)
			h[0] = lsf
			h[1] = failed

			// induce minmisation of h0
			//f[0] += math.Abs(lsf)
		}

	case 2:
		opt.Nova = 1
		opt.Noor = 2
		obj = func(sol *goga.Solution, cpu int) {

			// clear out-of-range values
			sol.Oor[0] = 0 // invalid transformation or FEM failed
			sol.Oor[1] = 0 // g(x) ≤ 0 was violated

			// original and normalised variables
			x := sol.Flt
			y, invalid := vars.Transform(x)
			if invalid {
				sol.Oor[0] = goga.INF
				sol.Oor[1] = goga.INF
				return
			}

			// objective value
			sol.Ova[0] = math.Sqrt(la.VecDot(y, y)) // β

			// inequality constraint
			lsf, failed := lsft(x, cpu)
			sol.Oor[0] = failed
			sol.Oor[1] = fun.Ramp(lsf)
		}

	default:
		chk.Panic("strategy %d is not available", opt.Strategy)
	}

	// initialise optimiser
	opt.Init(goga.GenTrialSolutions, obj, fcn, nf, ng, nh)

	// solve
	io.Pf(". . . . . . . .  running  . . . . . . . .\n")
	opt.RunMany("", "")
	goga.StatF(opt, 0, true)
	io.Pfblue2("Tsys = %v\n", opt.SysTimeAve)

	// check
	goga.CheckFront0(opt, true)

	// results
	sols := goga.GetFeasible(opt.Solutions)
	if len(sols) > 0 {
		goga.SortByOva(sols, 0)
		best := sols[0]
		io.Pforan("x    = %.6f\n", best.Flt)
		io.Pforan("xref = %.6f\n", opt.RptXref)
		io.Pforan("β = %v  (%v)\n", best.Ova[0], opt.RptFref[0])
	}
	return
}
示例#3
0
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)
}