示例#1
0
func buildIter(lb, ub []float64) optim.Method {
	mask := make([]bool, len(ub))
	for i := range mask {
		mask[i] = lb[i] < ub[i]
	}

	n := 30 + 1*len(lb)
	if *npar != 0 {
		n = *npar
	} else if n < 30 {
		n = 30
	}

	fmt.Printf("swarming with %v particles\n", n)

	ev := optim.ParallelEvaler{}
	if *addr == "" {
		ev.NConcurrent = *ncpu
	}

	pop := swarm.NewPopulationRand(n, lb, ub)
	swarm := swarm.New(
		pop,
		swarm.Evaler(ev),
		swarm.VmaxBounds(lb, ub),
		swarm.DB(db),
	)

	if *swarmonly {
		return swarm
	} else {
		return pattern.New(pop[0].Point,
			pattern.ResetStep(.01, 1.0),
			pattern.NsuccessGrow(4),
			pattern.Evaler(ev),
			pattern.PollRandNMask(n, mask),
			pattern.SearchMethod(swarm, pattern.Share),
			pattern.DB(db),
		)
	}
}
示例#2
0
func loadIter(lb, ub []float64, iter int) (md optim.Method, initstep float64) {

	_, err := db.Exec("CREATE INDEX IF NOT EXISTS points_posid ON points (posid ASC);")
	check(err)

	query := "SELECT pt.dim,pt.val,pi.val FROM points AS pt JOIN patterninfo AS pi ON pi.posid=pt.posid WHERE pi.iter=?;"
	initPoint := loadPoint(query, iter)

	row := db.QueryRow("SELECT step FROM patterninfo WHERE iter=?;", iter)
	err = row.Scan(&initstep)
	check(err)

	mask := make([]bool, len(ub))
	for i := range mask {
		mask[i] = lb[i] < ub[i]
	}

	row = db.QueryRow("SELECT COUNT(*) FROM swarmparticles WHERE iter=?;", iter)
	var npar int
	err = row.Scan(&npar)
	check(err)

	pop := make(swarm.Population, npar)
	for i := 0; i < npar; i++ {
		query := "SELECT pt.dim,pt.val,s.val FROM points AS pt JOIN swarmparticles AS s ON s.posid=pt.posid WHERE s.iter=? AND s.particle=?;"
		pt := loadPoint(query, iter, i)
		query = "SELECT pt.dim,pt.val,s.best FROM points AS pt JOIN swarmparticlesbest AS s ON s.posid=pt.posid WHERE s.iter=? AND s.particle=?;"
		best := loadPoint(query, iter, i)
		query = "SELECT pt.dim,pt.val,0 FROM points AS pt JOIN swarmparticles AS s ON s.velid=pt.posid WHERE s.iter=? AND s.particle=?;"
		vel := loadPoint(query, iter, i)
		par := &swarm.Particle{
			Id:    i,
			Point: pt,
			Best:  best,
			Vel:   vel.Pos,
		}
		pop[i] = par
		//fmt.Printf("DEBUG par %v: pos[10]=%v obj=%v bestpos[10]=%v bestobj=%v\n", i, par.Pos[10], par.Val, par.Best.Pos[10], par.Best.Val)
	}

	fmt.Printf("swarming with %v particles\n", len(pop))

	ev := optim.ParallelEvaler{}
	if *addr == "" {
		ev.NConcurrent = runtime.NumCPU()
	}

	swarm := swarm.New(
		pop,
		swarm.Evaler(ev),
		swarm.VmaxBounds(lb, ub),
		swarm.DB(db),
		swarm.InitIter(iter+1),
	)
	return pattern.New(initPoint,
		pattern.ResetStep(.01, 1.0),
		pattern.NsuccessGrow(4),
		pattern.Evaler(ev),
		pattern.PollRandNMask(npar, mask),
		pattern.SearchMethod(swarm, pattern.Share),
		pattern.DB(db),
	), initstep
}