Organize Agentbox runtime code
Co-authored-by: codegirl007 <codegirl-007@users.noreply.github.com>
This commit is contained in:
co-authored by
codegirl007
parent
f2fee5d26b
commit
4d2fb4a733
@@ -10,75 +10,82 @@ import (
|
||||
)
|
||||
|
||||
type Deterministic struct {
|
||||
next int
|
||||
initialX float64
|
||||
hasInitial bool
|
||||
nextStep int
|
||||
initialSquareX float64
|
||||
hasInitialPosition bool
|
||||
}
|
||||
|
||||
func (a *Deterministic) Name() string {
|
||||
func (deterministicAgent *Deterministic) Name() string {
|
||||
return "deterministic-right"
|
||||
}
|
||||
|
||||
func (a *Deterministic) NextAction(
|
||||
func (deterministicAgent *Deterministic) NextAction(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ []Step,
|
||||
observation Observation,
|
||||
) (Decision, error) {
|
||||
var decision Decision
|
||||
switch a.next {
|
||||
switch deterministicAgent.nextStep {
|
||||
case 0:
|
||||
x, err := greenCentroidX(observation.Screenshot)
|
||||
initialSquareX, err := greenCentroidX(observation.Screenshot)
|
||||
if err != nil {
|
||||
return Decision{}, fmt.Errorf("inspect initial screenshot: %w", err)
|
||||
}
|
||||
a.initialX = x
|
||||
a.hasInitial = true
|
||||
deterministicAgent.initialSquareX = initialSquareX
|
||||
deterministicAgent.hasInitialPosition = true
|
||||
action := environment.InputAction{Type: environment.KeyDown, Key: "RIGHT"}
|
||||
decision = Decision{Reason: "Press RIGHT to start moving.", Action: &action}
|
||||
case 1:
|
||||
action := environment.InputAction{Type: environment.Wait, DurationMS: 1000}
|
||||
decision = Decision{Reason: "Keep RIGHT held for one second.", Action: &action}
|
||||
case 2:
|
||||
x, err := greenCentroidX(observation.Screenshot)
|
||||
currentSquareX, err := greenCentroidX(observation.Screenshot)
|
||||
if err != nil {
|
||||
return Decision{}, fmt.Errorf("inspect moved screenshot: %w", err)
|
||||
}
|
||||
if !a.hasInitial || x-a.initialX < 100 {
|
||||
return Decision{}, fmt.Errorf("visual verification failed: square moved %.1f pixels right, want at least 100", x-a.initialX)
|
||||
distanceMoved := currentSquareX - deterministicAgent.initialSquareX
|
||||
if !deterministicAgent.hasInitialPosition || distanceMoved < 100 {
|
||||
return Decision{}, fmt.Errorf(
|
||||
"visual verification failed: square moved %.1f pixels right, want at least 100",
|
||||
distanceMoved,
|
||||
)
|
||||
}
|
||||
action := environment.InputAction{Type: environment.KeyUp, Key: "RIGHT"}
|
||||
decision = Decision{
|
||||
Reason: fmt.Sprintf("The square moved %.1f pixels right; release RIGHT.", x-a.initialX),
|
||||
Reason: fmt.Sprintf(
|
||||
"The square moved %.1f pixels right; release RIGHT.",
|
||||
distanceMoved,
|
||||
),
|
||||
Action: &action,
|
||||
}
|
||||
default:
|
||||
decision = Decision{Reason: "The movement sequence is complete.", Done: true}
|
||||
}
|
||||
a.next++
|
||||
deterministicAgent.nextStep++
|
||||
return decision, nil
|
||||
}
|
||||
|
||||
func greenCentroidX(screenshot []byte) (float64, error) {
|
||||
image, err := png.Decode(bytes.NewReader(screenshot))
|
||||
renderedImage, err := png.Decode(bytes.NewReader(screenshot))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var sumX, count uint64
|
||||
bounds := image.Bounds()
|
||||
var xCoordinateSum, greenPixelCount uint64
|
||||
bounds := renderedImage.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
red, green, blue, _ := image.At(x, y).RGBA()
|
||||
red, green, blue, _ := renderedImage.At(x, y).RGBA()
|
||||
if green > 0xc000 && red < 0x4000 && blue < 0x8000 {
|
||||
sumX += uint64(x)
|
||||
count++
|
||||
xCoordinateSum += uint64(x)
|
||||
greenPixelCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
if count < 1000 {
|
||||
return 0, fmt.Errorf("found only %d green square pixels", count)
|
||||
if greenPixelCount < 1000 {
|
||||
return 0, fmt.Errorf("found only %d green square pixels", greenPixelCount)
|
||||
}
|
||||
return float64(sumX) / float64(count), nil
|
||||
return float64(xCoordinateSum) / float64(greenPixelCount), nil
|
||||
}
|
||||
|
||||
var _ Agent = (*Deterministic)(nil)
|
||||
|
||||
Reference in New Issue
Block a user