当前位置: 首页>>代码示例>>Golang>>正文


Golang Optimiser.Solve方法代码示例

本文整理汇总了Golang中github.com/cpmech/goga.Optimiser.Solve方法的典型用法代码示例。如果您正苦于以下问题:Golang Optimiser.Solve方法的具体用法?Golang Optimiser.Solve怎么用?Golang Optimiser.Solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/cpmech/goga.Optimiser的用法示例。


在下文中一共展示了Optimiser.Solve方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: runone

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
//.........这里部分代码省略.........
开发者ID:cpmech,项目名称:goga,代码行数:101,代码来源:topology.go

示例2: main

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), "")
//.........这里部分代码省略.........
开发者ID:cpmech,项目名称:goga,代码行数:101,代码来源:one-obj-prob9-dbg.go


注:本文中的github.com/cpmech/goga.Optimiser.Solve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。