コード例 #1
0
ファイル: singpole.go プロジェクト: soniah/neat
func (eval singPoleEval) Evaluate(org *neat.Organism) (err error) {

	if org.Phenome == nil {
		err = errors.New("Cannot evaluate an org without a Phenome")
		org.Fitness = []float64{0} // Minimal fitness
		return
	}

	twelve_degrees := float64(0.2094384) // radians
	num_steps := int(math.Pow(10, 5))

	// initial conditions (as used by Stanley)
	//rng := rand.New(rand.NewSource(time.Now().UnixNano()))
	//x := float64(rng.Intn(4800))/1000.0 - 2.4
	//x_dot := float64(rng.Intn(2000))/1000.0 - 1.0
	//theta := float64(rng.Intn(400))/1000.0 - 0.2
	//theta_dot := float64(rng.Intn(3000))/1000.0 - 1.5
	var x, x_dot, theta, theta_dot float64

	fitness := float64(0)

	for trials := 0; trials < num_steps; trials++ {

		// maps into [0,1]
		inputs := []float64{(x + 2.4) / 4.8,
			(x_dot + 0.75) / 1.5,
			(theta + twelve_degrees) / 0.41,
			(theta_dot + 1.0) / 2.0}

		// a normalizacao so acontece para estas condicoes iniciais
		// nada garante que a evolucao do sistema leve a outros
		// valores de x, x_dot e etc...

		action, err2 := org.Analyze(inputs)
		if err2 != nil {
			err = err2
			org.Fitness = []float64{0}
			return
		}
		// Apply action to the simulated cart-pole
		x, x_dot, theta, theta_dot = cartPole(action[0], x, x_dot, theta, theta_dot)

		// Check for failure.  If so, return steps
		// the number of steps indicates the fitness: higher = better
		fitness += 1
		//if fitness > 100 {
		//	fmt.Println("WTF?")
		//}
		if math.Abs(x) >= 2.4 || (math.Abs(theta) >= twelve_degrees) {
			break
		}
	}
	org.Fitness = []float64{fitness}
	return
}
コード例 #2
0
ファイル: xor.go プロジェクト: soniah/neat
func (eval xorEval) Evaluate(org *neat.Organism) (err error) {

	if org.Phenome == nil {
		err = errors.New("Cannot evaluate an org without a Phenome")
		org.Fitness = []float64{0} // Minimal fitness
		return
	}

	e := float64(0)
	for i, inputs := range INPUTS {
		output, err2 := org.Analyze(inputs)
		if err2 != nil {
			err = err2
			org.Fitness = []float64{0}
			return
		}
		e += (output[0] - OUTPUTS[i]) * (output[0] - OUTPUTS[i])
	}
	org.Fitness = []float64{float64(1) - math.Sqrt(e/float64(len(OUTPUTS)))}

	return
}