Example #1
0
File: neat.go Project: NioTeX/neat
func (e *NEATEval) Evaluate(p neat.Phenome) (r neat.Result) {
	inputs := [][]float64{{0, 0}, {0, 1}, {1, 0}, {1, 1}}
	expected := []float64{0, 1, 1, 0}
	actual := make([]float64, 4)

	// Run experiment
	var err error
	var sum float64
	stop := true
	for i, in := range inputs {
		outputs, err := p.Activate(in)
		if err != nil {
			break
		}
		actual[i] = outputs[0]
		sum += math.Abs(outputs[0] - expected[i])
		if expected[i] == 0 {
			stop = stop && outputs[0] < 0.5
		} else {
			stop = stop && outputs[0] > 0.5
		}
	}

	// Calculate the result
	r = result.New(p.ID(), math.Pow(4.0-sum, 2.0), err, stop)
	return
}
Example #2
0
File: xor.go Project: NioTeX/neat
// Evaluate computes the error for the XOR problem with the phenome
//
// To compute fitness, the distance of the output from the correct answer was summed for all four
// input patterns. The result of this error was subtracted from 4 so that higher fitness would mean
// better networks. The resulting number was squared to give proportionally more fitness the closer
// a network was to a solution. (Stanley, 43)
func (e Evaluator) Evaluate(p neat.Phenome) (r neat.Result) {
	inputs := [][]float64{
		[]float64{0, 0},
		[]float64{0, 1},
		[]float64{1, 0},
		[]float64{1, 1},
	}

	expected := []float64{0, 1, 1, 0}
	actual := make([]float64, 4)

	// Run experiment
	var err error
	var sum float64
	stop := true
	for i, in := range inputs {
		outputs, err := p.Activate(in)
		if err != nil {
			break
		}
		actual[i] = outputs[0]
		sum += math.Abs(outputs[0] - expected[i])
		if expected[i] == 0 {
			stop = stop && outputs[0] < 0.5
		} else {
			stop = stop && outputs[0] > 0.5
		}
	}

	// Display the work
	if e.show {
		b := bytes.NewBufferString("\n")
		if e.useTrial {
			b.WriteString(fmt.Sprintf("Trial %d ", e.trialNum))
		}
		b.WriteString(fmt.Sprintf("XOR Evaluation for genome %d\n", p.ID()))
		b.WriteString(fmt.Sprintf("------------------------------------------\n"))
		b.WriteString(fmt.Sprintf("For {0,0}, expected 0. output was %f\n", actual[0]))
		b.WriteString(fmt.Sprintf("For {0,1}, expected 1. output was %f\n", actual[1]))
		b.WriteString(fmt.Sprintf("For {1,0}, expected 1. output was %f\n", actual[2]))
		b.WriteString(fmt.Sprintf("For {1,1}, expected 0. output was %f\n", actual[3]))
		fmt.Print(b.String())
	}

	// Calculate the result
	if stop {
		fmt.Println("Stopping", p.ID())
	}
	r = result.New(p.ID(), math.Pow(4.0-sum, 2.0), err, stop)
	return
}
Example #3
0
func (e *RTEvaluator) Evaluate(p neat.Phenome) (r neat.Result) {

	// Retrieve the environment
	env := e.History[p.ID()]
	h := &env.Hero

	// Iterate the maze 1 step
	var err error
	paths := make([]Line, 0, *Steps)
	stop := false

	// Note the start of the path
	a := h.Location

	// Update the hero's location
	var outputs []float64
	inputs := generateNeuralInputs(*env)
	if outputs, err = p.Activate(inputs); err == nil {
		interpretOutputs(env, outputs[0], outputs[1])
		update(env)
		b := h.Location
		paths = append(paths, Line{A: a, B: b})

		// Look for solution
		stop = distanceToTarget(env) < 5.0
	}

	f := 300.0 - distanceToTarget(env)          // fitness
	bh := []float64{h.Location.X, h.Location.Y} // behavior
	r = &Result{Classic: result.New(p.ID(), f, err, stop), behavior: bh}

	// Output the maze
	if e.show {
		// Write the file
		var t string
		if e.useTrial {
			t = fmt.Sprintf("-%d", e.trialNum)
		}
		if err := showMaze(path.Join(*WorkPath, fmt.Sprintf("maze%s-%d.svg", t, p.ID())), *env, paths, nil); err != nil {
			log.Println("Could not output maze run:", err)
		}
	}
	return
}
Example #4
0
File: ocr.go Project: NioTeX/neat
func (e Evaluator) Evaluate(p neat.Phenome) (r neat.Result) {

	// Iterate the inputs and ask about
	letters := []uint8{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
	guesses := make([][]uint8, 26)
	values := make([]float64, 26)
	sum := 0.0
	cnt := 0.0
	stop := true
	for i, input := range inputs {
		// Query the network for this letter
		outputs, err := p.Activate(input)
		if err != nil {
			return result.New(p.ID(), 0, err, false)
		}

		// Identify the max
		max, _ := stats.Max(outputs)
		values[i] = max

		// Determine success
		s2 := 0.0
		for j := 0; j < len(outputs); j++ {
			if outputs[j] == max {
				guesses[i] = append(guesses[i], letters[j])
				if j != i {
					stop = false // picked another letter
					s2 += 1.0
				} else {
					s2 += 1.0 - max
				}
			}
			cnt += 1
		}
		sum += s2
	}

	if e.show {
		b := bytes.NewBufferString("\n")
		fmt.Println()
		if e.useTrial {
			b.WriteString(fmt.Sprintf("Trial %d ", e.trialNum))
		}
		b.WriteString(fmt.Sprintf("OCR Evaluation for genome %d. Letter->Guess(confidence)\n", p.ID()))
		b.WriteString(fmt.Sprintf("------------------------------------------\n"))
		for i := 0; i < len(letters); i++ {
			b.WriteString(fmt.Sprintf("%s (%0.2f)", string(letters[i]), values[i]))
			cl := ""
			il := ""
			for j := 0; j < len(guesses[i]); j++ {
				if guesses[i][j] == letters[i] {
					cl = "   correct"
				} else {
					if il != "" {
						il += ", "
					}
					il += string(guesses[i][j])
				}
			}
			if cl == "" {
				cl = " incorrect"
			}
			b.WriteString(cl)
			if il != "" {
				b.WriteString(" but also guessed ")
				b.WriteString(il)
			}
			b.WriteString("\n")
		}
		fmt.Println(b.String())
	}

	return result.New(p.ID(), math.Pow(cnt-sum, 2), nil, stop || !time.Now().Before(e.stopTime))
}
Example #5
0
// Evaluate computes the error for the XOR problem with the phenome
//
// To compute fitness, the distance of the output from the correct answer was summed for all four
// input patterns. The result of this error was subtracted from 4 so that higher fitness would mean
// better networks. The resulting number was squared to give proportionally more fitness the closer
// a network was to a solution. (Stanley, 43)
func (e Evaluator) Evaluate(p neat.Phenome) (r neat.Result) {
	// Run experiment
	var err error
	stop := false
	var worstFitt, sumFitt float64
	worstFitt = 101

	if e.show {
		fmt.Println("Flappy Evaluation for genome ", p.ID(), "\n")
	}

	for tries := 0; tries < 10; tries++ {
		f := Flappy{}
		f.Alive = true
		f.Fitness = 0
		f.screen.x = 10
		f.screen.y = 10
		f.bird.velocity = 1
		f.bird.posX = 4
		f.bird.posY = 2
		f.obs.Y = 12
		f.obs.bottomX = 6
		f.obs.topX = 3

		in := make([]float64, f.screen.x*f.screen.y)

		if e.show {
			fmt.Println("")
			fmt.Println("try #", tries)
		}

		for f.Alive && f.Fitness < 10 {
			in = f.Export()
			outputs, err := p.Activate(in)
			if err != nil {
				break
			}
			f.Next(outputs[0])
			//var asd string
			if outputs[0] >= 0.5 {
				//asd = " Clicked!!"
			} else {
				//asd = ""
			}
			if e.show {
				//fmt.Println("obsY: ", f.obs.Y, "[", f.obs.bottomX, " ", f.bird.posX, " ", f.obs.topX, "]", asd, "Fitness: ", f.Fitness)
			}
		}

		if f.Fitness < worstFitt {
			worstFitt = f.Fitness
		}
		sumFitt += f.Fitness
	}

	// Calculate the result
	if worstFitt > 9 {
		stop = true
	}

	sumFitt /= 10

	sumFitt += worstFitt

	r = result.New(p.ID(), sumFitt, err, stop)
	return
}