Пример #1
0
// Initialises continues initialisation by generating individuals
//  Optional:  obj  XOR  fcn, nf, ng, nh
func (o *Optimiser) Init(gen Generator_t, obj ObjFunc_t, fcn MinProb_t, nf, ng, nh int) {

	// generic or minimisation problem
	if obj != nil {
		o.ObjFunc = obj
	} else {
		if fcn == nil {
			chk.Panic("either ObjFunc or MinProb must be provided")
		}
		o.Nf, o.Ng, o.Nh, o.MinProb = nf, ng, nh, fcn
		o.ObjFunc = func(sol *Solution, cpu int) {
			o.MinProb(o.F[cpu], o.G[cpu], o.H[cpu], sol.Flt, sol.Int, cpu)
			for i, f := range o.F[cpu] {
				sol.Ova[i] = f
			}
			for i, g := range o.G[cpu] {
				sol.Oor[i] = utl.GtePenalty(g, 0.0, 1) // g[i] ≥ 0
			}
			for i, h := range o.H[cpu] {
				h = math.Abs(h)
				sol.Ova[0] += h
				sol.Oor[o.Ng+i] = utl.GtePenalty(o.EpsH, h, 1) // ϵ ≥ |h[i]|
			}
		}
		o.F = la.MatAlloc(o.Ncpu, o.Nf)
		o.G = la.MatAlloc(o.Ncpu, o.Ng)
		o.H = la.MatAlloc(o.Ncpu, o.Nh)
		o.Nova = o.Nf
		o.Noor = o.Ng + o.Nh
	}

	// calc derived parameters
	o.Generator = gen
	o.CalcDerived()

	// allocate solutions
	o.Solutions = NewSolutions(o.Nsol, &o.Parameters)
	o.Groups = make([]*Group, o.Ncpu)
	for cpu := 0; cpu < o.Ncpu; cpu++ {
		o.Groups[cpu] = new(Group)
		o.Groups[cpu].Init(cpu, o.Ncpu, o.Solutions, &o.Parameters)
	}

	// metrics
	o.Metrics = new(Metrics)
	o.Metrics.Init(o.Nsol, &o.Parameters)

	// auxiliary
	o.tmp = NewSolution(0, 0, &o.Parameters)
	o.cpupairs = utl.IntsAlloc(o.Ncpu/2, 2)
	o.iova0 = -1
	o.ova0 = make([]float64, o.Tf)

	// generate trial solutions
	o.generate_solutions(0)
}
Пример #2
0
// PrintConstraints prints violated or not constraints
func (o System) PrintConstraints(P []float64, Pdemand float64, full bool) {
	sumP := 0.0
	for i, g := range o.G {
		if full {
			io.Pfyel("P%d range error = %v\n", i, utl.GtePenalty(P[i], g.Pmin, 1)+utl.GtePenalty(g.Pmax, P[i], 1))
		}
		sumP += P[i]
	}
	Ploss := 0.0
	io.Pf("balance error = %v\n", math.Abs(sumP-Pdemand-Ploss))
}
Пример #3
0
// Init initialises simple flot problem structure
func NewSimpleFltProb(fcn SimpleFltFcn_t, nf, ng, nh int, C *ConfParams) (o *SimpleFltProb) {

	// data
	o = new(SimpleFltProb)
	o.Fcn = fcn
	o.C = C
	o.C.Nova = nf
	o.C.Noor = ng + nh

	// sandbox
	o.nf, o.ng, o.nh = nf, ng, nh
	o.ff = utl.DblsAlloc(o.C.Nisl, o.nf)
	o.gg = utl.DblsAlloc(o.C.Nisl, o.ng)
	o.hh = utl.DblsAlloc(o.C.Nisl, o.nh)

	// objective function
	o.C.OvaOor = func(ind *Individual, isl, time int, report *bytes.Buffer) {
		x := ind.GetFloats()
		o.Fcn(o.ff[isl], o.gg[isl], o.hh[isl], x)
		for i, f := range o.ff[isl] {
			ind.Ovas[i] = f
		}
		for i, g := range o.gg[isl] {
			ind.Oors[i] = utl.GtePenalty(g, 0.0, 1) // g[i] ≥ 0
		}
		for i, h := range o.hh[isl] {
			h = math.Abs(h)
			ind.Ovas[0] += h
			ind.Oors[ng+i] = utl.GtePenalty(o.C.Eps1, h, 1) // ϵ ≥ |h[i]|
		}
	}

	// evolver
	o.Evo = NewEvolver(o.C)

	// auxiliary
	o.NumfmtX = "%8.5f"
	o.NumfmtF = "%8.5f"

	// results and stat
	nx := len(o.C.RangeFlt)
	o.Xbest = utl.DblsAlloc(o.C.Ntrials, nx)

	// plotting
	if o.C.DoPlot {
		o.PopsIni = o.Evo.GetPopulations()
		o.PltDirout = "/tmp/goga"
		o.PltNpts = 41
		o.PltLwg = 1.5
		o.PltLwh = 1.5
	}
	return
}
Пример #4
0
func Test_island01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("island01")

	genes := [][]float64{
		{11, 21, 31},
		{12, 22, 32},
		{13, 23, 33},
		{14, 24, 34},
		{15, 25, 35},
		{16, 26, 36},
	}

	// parameters
	C := NewConfParams()
	C.Ninds = len(genes)
	C.Nova = 2
	C.Noor = 3
	C.Nbases = 1
	C.Rnk = false
	C.RangeFlt = [][]float64{
		{10, 20},
		{20, 30},
		{30, 40},
	}
	C.CalcDerived()

	// generator
	C.PopFltGen = func(id int, cc *ConfParams) Population {
		o := make([]*Individual, cc.Ninds)
		for i := 0; i < cc.Ninds; i++ {
			o[i] = NewIndividual(cc.Nova, cc.Noor, cc.Nbases, genes[i])
		}
		return o
	}

	// objective function
	// the best will have the largest genes (x,y,z);
	// but with the first gene (x) smaller than or equal to 13
	C.OvaOor = func(ind *Individual, idIsland, time int, report *bytes.Buffer) {
		x, y, z := ind.GetFloat(0), ind.GetFloat(1), ind.GetFloat(2)
		ind.Ovas[0] = -(x + y + z)
		ind.Ovas[1] = z
		ind.Oors[0] = utl.GtePenalty(13, x, 1)
		ind.Oors[1] = utl.GtePenalty(24, y, 1)
		ind.Oors[2] = utl.GtePenalty(z, 35, 1)
		return
	}

	sorted_genes := [][]float64{
		{13, 23, 33},
		{14, 24, 34},
		{12, 22, 32},
		{11, 21, 31},
		{15, 25, 35},
		{16, 26, 36},
	}

	// island
	id := 0
	isl := NewIsland(id, C)
	io.Pf("\n%v", isl.Pop.Output(C))
	io.Pf("best = %v\n", isl.Pop[0].Output(nil, false))
	vals := make([]float64, 3)
	for i := 0; i < 3; i++ {
		vals[i] = isl.Pop[0].GetFloat(i)
	}
	chk.Vector(tst, "best", 1e-14, vals, []float64{13, 23, 33})
	for i, ind := range isl.Pop {
		for j := 0; j < 3; j++ {
			vals[j] = ind.GetFloat(j)
		}
		chk.Vector(tst, io.Sf("ind%d", i), 1e-14, vals, sorted_genes[i])
	}
	/*
		io.Pforan("sovas0 = %.4f\n", isl.sovas[0])
		io.Pforan("sovas1 = %.4f\n", isl.sovas[1])
		io.Pfcyan("soors0 = %.4f\n", isl.soors[0])
		io.Pfcyan("soors1 = %.4f\n", isl.soors[1])
		io.Pfcyan("soors2 = %.4f\n", isl.soors[2])
	*/

	// stat
	minrho, averho, maxrho, devrho := isl.FltStat()
	io.Pforan("\nallbases[0] = %.6f\n", isl.allbases[0])
	io.Pforan("allbases[1] = %.6f\n", isl.allbases[1])
	io.Pforan("allbases[2] = %.6f\n", isl.allbases[2])
	if C.Nbases > 1 {
		io.Pforan("allbases[3] = %.6f\n", isl.allbases[3])
		io.Pforan("allbases[4] = %.6f\n", isl.allbases[4])
		io.Pforan("allbases[5] = %.6f\n", isl.allbases[5])
	}
	io.Pfcyan("devbases[0] = %.6f\n", isl.devbases[0])
	io.Pfcyan("devbases[1] = %.6f\n", isl.devbases[1])
	io.Pfcyan("devbases[2] = %.6f\n", isl.devbases[2])
	io.Pforan("minrho = %v\n", minrho)
	io.Pforan("averho = %v\n", averho)
	io.Pforan("maxrho = %v\n", maxrho)
	io.Pforan("devrho = %v\n", devrho)

	isl.Run(0, false, false)
	io.Pf("\n%v\n", isl.Pop.Output(C))

	isl.Run(1, false, false)
	io.Pforan("%v\n", isl.Pop.Output(C))

	isl.Run(2, false, false)
	io.Pf("%v\n", isl.Pop.Output(C))

	// TODO: more tests required
}
Пример #5
0
func Test_flt04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("flt04. two-bar truss. Pareto-optimal")

	// configuration
	C := NewConfParams()
	C.Nisl = 4
	C.Ninds = 24
	C.GAtype = "crowd"
	C.DiffEvol = true
	C.CrowdSize = 3
	C.ParetoPhi = 0.05
	C.Tf = 100
	C.Dtmig = 25
	C.RangeFlt = [][]float64{{0.1, 2.25}, {0.5, 2.5}}
	C.PopFltGen = PopFltGen
	C.CalcDerived()
	rnd.Init(C.Seed)

	// data
	// from Coelho (2007) page 19
	ρ := 0.283 // lb/in³
	H := 100.0 // in
	P := 1e4   // lb
	E := 3e7   // lb/in²
	σ0 := 2e4  // lb/in²

	// functions
	TSQ2 := 2.0 * math.Sqrt2
	fcn := func(f, g, h []float64, x []float64) {
		f[0] = 2.0 * ρ * H * x[1] * math.Sqrt(1.0+x[0]*x[0])
		f[1] = P * H * math.Pow(1.0+x[0]*x[0], 1.5) * math.Sqrt(1.0+math.Pow(x[0], 4.0)) / (TSQ2 * E * x[0] * x[0] * x[1])
		g[0] = σ0 - P*(1.0+x[0])*math.Sqrt(1.0+x[0]*x[0])/(TSQ2*x[0]*x[1])
		g[1] = σ0 - P*(1.0-x[0])*math.Sqrt(1.0+x[0]*x[0])/(TSQ2*x[0]*x[1])
	}

	// objective value function
	C.OvaOor = func(ind *Individual, idIsland, t int, report *bytes.Buffer) {
		x := ind.GetFloats()
		f := make([]float64, 2)
		g := make([]float64, 2)
		fcn(f, g, nil, x)
		ind.Ovas[0] = f[0]
		ind.Ovas[1] = f[1]
		ind.Oors[0] = utl.GtePenalty(g[0], 0, 1)
		ind.Oors[1] = utl.GtePenalty(g[1], 0, 1)
	}

	// simple problem
	sim := NewSimpleFltProb(fcn, 2, 2, 0, C)
	sim.Run(chk.Verbose)

	// results
	if chk.Verbose {

		// reference data
		_, dat, _ := io.ReadTable("data/coelho-fig1.6.dat")

		// Pareto-front
		feasible := sim.Evo.GetFeasible()
		ovas, _ := sim.Evo.GetResults(feasible)
		ovafront, _ := sim.Evo.GetParetoFront(feasible, ovas, nil)
		xova, yova := sim.Evo.GetFrontOvas(0, 1, ovafront)

		// plot
		plt.SetForEps(0.75, 355)
		plt.Plot(dat["f1"], dat["f2"], "'k+',ms=3")
		x := utl.DblsGetColumn(0, ovas)
		y := utl.DblsGetColumn(1, ovas)
		plt.Plot(x, y, "'r.'")
		plt.Plot(xova, yova, "'ko',markerfacecolor='none',ms=6")
		plt.Gll("$f_1$", "$f_2$", "")
		plt.SaveD("/tmp/goga", "test_flt04.eps")
	}
}