-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdqn_try.go
151 lines (131 loc) · 3.34 KB
/
dqn_try.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"math/rand"
)
// type Environment interface {
// Reset() []float64
// Step(action int) ([]float64, float64, bool)
// StateSize() int
// ActionSize() int
// }
// SimpleEnv - A simple environment for testing the agent
type SimpleEnv struct {
stateSize int
actionSize int
currentState []float64
}
// Reset - Resets the environment to a random initial state
func (env *SimpleEnv) Reset() []float64 {
env.currentState = []float64{
rand.Float64(),
rand.Float64(),
rand.Float64(),
rand.Float64(),
}
return env.currentState
}
// Step - Simulates taking an action in the environment
func (env *SimpleEnv) Step(action int) ([]float64, float64, bool) {
nextState := []float64{
rand.Float64(),
rand.Float64(),
rand.Float64(),
rand.Float64(),
}
reward := -1.0
if rand.Float64() < 0.5 {
reward = 1.0
}
done := rand.Float64() < 0.1 // Randomly end episode
return nextState, reward, done
}
func (env *SimpleEnv) StateSize() int {
return env.stateSize
}
func (env *SimpleEnv) ActionSize() int {
return env.actionSize
}
// Gridworld - A grid environment where agent moves on the grid
type Gridworld struct {
gridSize int
stateSize int
actionSize int
grid [][]int
position []int
}
// Reset - Resets the grid environment
func (grid *Gridworld) Reset() []float64 {
grid.grid = make([][]int, grid.gridSize)
for i := range grid.grid {
grid.grid[i] = make([]int, grid.gridSize)
}
grid.grid[3][3] = 1 // Set goal at random position
grid.position = []int{0, 0} //grid.position = []int{rand.Intn(grid.gridSize), rand.Intn(grid.gridSize)} // Start at random position
return grid.GetState()
}
// GetState - Returns the current state as a flattened grid
func (grid *Gridworld) GetState() []float64 {
flatGrid := make([]float64, grid.gridSize*grid.gridSize)
for i, row := range grid.grid {
for j, cell := range row {
flatGrid[i*grid.gridSize+j] = float64(cell)
}
}
// Mark the agent's current position
index := grid.position[0]*grid.gridSize + grid.position[1]
flatGrid[index] = -0.5
return flatGrid
}
// Step - Moves the agent based on the action and returns next state, reward, and done
func (grid *Gridworld) Step(action int) ([]float64, float64, bool) {
x, y := grid.position[0], grid.position[1]
newX, newY := x, y
switch action {
case 0: // Move up
if x > 0 {
newX--
}
case 1: // Move down
if x < grid.gridSize-1 {
newX++
}
case 2: // Move left
if y > 0 {
newY--
}
case 3: // Move right
if y < grid.gridSize-1 {
newY++
}
}
grid.position = []int{newX, newY}
done := newX == grid.gridSize-1 && newY == grid.gridSize-1 // Check if goal is reached
reward := -0.1
if done {
reward = 10
}
return grid.GetState(), reward, done
}
func (grid *Gridworld) StateSize() int {
return grid.stateSize
}
func (grid *Gridworld) ActionSize() int {
return grid.actionSize
}
// TestGridworldAgent - Test DQN agent with the Gridworld environment
func TestGridworldAgent() {
gridEnv := &Gridworld{
gridSize: 5, // 8x8 grid
stateSize: 25,
actionSize: 4, // Up, Down, Left, Right
}
agent := NewDQNAgent(gridEnv) // Create the DQN agent
fmt.Println("Training on Gridworld...")
agent.Train(2000, 32, 300) // Train for 200 episodes
fmt.Println("Evaluating performance...")
agent.Evaluate(100, 300) // Evaluate on 100 episodes
}
// func main() {
// TestGridworldAgent()
// }