Skip to content

Commit 0cf509f

Browse files
committed
update
1 parent 018772d commit 0cf509f

File tree

7 files changed

+69
-51
lines changed

7 files changed

+69
-51
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
.pio
22

3-
.idea
3+
.idea
4+
include/README
5+
6+
lib/README
7+
8+
test/README

include/ball.h

+14-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const PROGMEM float velocity = 1.5f;
1414

1515
class Ball : public GameObject, Collision {
1616
public:
17-
Ball(Environment *env) : GameObject(Size(0, 1)) {
17+
Ball(Environment *env) : GameObject(Size(1, 1)) {
1818
this->Init();
1919
this->env = env;
2020
}
@@ -57,31 +57,25 @@ class Ball : public GameObject, Collision {
5757
double alpha;
5858

5959
void onCollision(GameObject *other) override {
60-
this->alpha = (this->position.x - (other->getX() + other->getWidth() / 2)) * (-M_PI_4);
61-
if (reinterpret_cast<Player *>(other)->getSide() == PlayerSide::LEFT)
60+
this->alpha = constrain(this->position.x - (other->getX() + (other->getWidth() >> 1)), -1, 1) * (-M_PI_4);
61+
if (reinterpret_cast<Player *>(other)->getSide() == PlayerSide::RIGHT)
6262
this->alpha += M_PI_2;
6363
else
6464
this->alpha += (M_PI + M_PI_2); // 270 degrees
6565

66-
// Serial.println("Collision");
66+
// Serial.println("Collision");
6767
//
68-
// Serial.print("X: ");
69-
// Serial.print(this->position.x);
70-
// Serial.print(" Y: ");
71-
// Serial.println(this->position.y);
72-
// Serial.print("Width: ");
73-
// Serial.print(this->size.width);
74-
// Serial.print(" Height: ");
75-
// Serial.println(this->size.height);
68+
// Serial.print("Ball - X: ");
69+
// Serial.print(this->position.x);
70+
// Serial.print(" Y: ");
71+
// Serial.print(this->position.y);
72+
// Serial.print(" Alpha: ");
73+
// Serial.println(this->alpha);
7674
//
77-
// Serial.print("X: ");
78-
// Serial.print(other->getX());
79-
// Serial.print(" Y: ");
80-
// Serial.println(other->getY());
81-
// Serial.print("Width: ");
82-
// Serial.print(other->getWidth());
83-
// Serial.print(" Height: ");
84-
// Serial.println(other->getHeight());
75+
// Serial.print("Player - X: ");
76+
// Serial.print(other->getX());
77+
// Serial.print(" Y: ");
78+
// Serial.println(other->getY());
8579
}
8680

8781
void playerCollider();

include/environment.h

+27-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void display_freeram() {
2929

3030

3131
class Environment {
32-
public:
32+
public: // layerA(this, A5, PlayerSide::LEFT)
3333
Environment() : playerA(this, PlayerSide::LEFT), playerB(this, PlayerSide::RIGHT) {
3434
// Init LED Matrix 8x8
3535
mx.begin();
@@ -68,35 +68,48 @@ class Environment {
6868
obj->update(s);
6969
}
7070

71-
void playerEnd(Player &p) {
71+
void playerEnd(Player *p) {
7272
Serial.print("Player ");
73-
Serial.print(p.getSide() == PlayerSide::LEFT ? "Left: " : "Right: ");
74-
Serial.println(p.getScore());
73+
Serial.print(p->getSide() == PlayerSide::LEFT ? F("Left: ") : F("Right: "));
74+
Serial.println(p->getScore());
7575

7676
Serial.print(F("Success rate of Player "));
77-
Serial.print(p.getSide() == PlayerSide::LEFT ? F("Left: ") : F("Right: "));
78-
Serial.print(p.getScore() * 100.0f / rounds);
77+
Serial.print(p->getSide() == PlayerSide::LEFT ? F("Left: ") : F("Right: "));
78+
Serial.print(p->getScore() * 100.0f / rounds);
7979
Serial.println(F("%"));
80-
p.Init();
80+
81+
if ((p->getScore() * 100.0f / rounds) > 85) {
82+
for (char i = 0; i < 8; i++)
83+
for (char j = 0; j < 8; j++)
84+
for (char k = 0; k < 6; k++)
85+
for (char l = 0; l < 3; l++) {
86+
Serial.print((int)reinterpret_cast<QPlayer*>(p)->Q_table[i][j][k][l]);
87+
Serial.print(';');
88+
}
89+
Serial.println();
90+
}
91+
p->Init();
8192

8293
Serial.print("Rounds: ");
8394
Serial.println(rounds);
8495

85-
display_freeram();
96+
//display_freeram();
8697
}
8798

8899
void handleEnd() {
89-
this->playerEnd(playerA);
90-
this->playerEnd(playerB);
100+
this->playerEnd(&playerA);
101+
this->playerEnd(&playerB);
91102
ball->Init();
92-
// this->rounds = 0;
103+
if (this->rounds > 30000) this->rounds = 0;
104+
//this->rounds = 0;
93105
}
94106

95107
void handleRound() {
96108
this->rounds++;
97109
}
98110

99111
RandomPlayer playerA;
112+
//HumanPlayer playerA;
100113
QPlayer playerB;
101114

102115
private:
@@ -106,6 +119,7 @@ class Environment {
106119
};
107120

108121
inline void Player::Goal() {
122+
if (this->score > 30000) this->score = 0;
109123
this->score += 1;
110124
this->env->handleRound();
111125
if ((this->score % 21) == 0) {
@@ -120,7 +134,7 @@ inline void Ball::playerCollider() {
120134

121135
// check if the goal has been scored
122136
if (this->env->playerA.getY() == this->position.y &&
123-
(this->position.x < this->env->playerA.getX() || this->position.x > (this->env->playerA.getX() + 2))) {
137+
(this->position.x < this->env->playerA.getX() || this->position.x > (this->env->playerA.getX() + this->env->playerA.getWidth() - 1))) {
124138
this->env->playerA.setReward(-50);
125139
this->env->playerB.setReward(50);
126140
this->env->playerB.Goal();
@@ -129,7 +143,7 @@ inline void Ball::playerCollider() {
129143
}
130144

131145
if (this->env->playerB.getY() == this->position.y &&
132-
(this->position.x < this->env->playerB.getX() || this->position.x > (this->env->playerB.getX() + 2))) {
146+
(this->position.x < this->env->playerB.getX() || this->position.x > (this->env->playerB.getX() + this->env->playerB.getWidth() - 1))) {
133147
this->env->playerA.Goal();
134148
this->env->playerA.setReward(50);
135149
this->env->playerB.setReward(-50);

include/human_player.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ class Environment;
1111

1212
class HumanPlayer : public Player {
1313
public:
14-
HumanPlayer(Environment *env, const int pin, const PlayerSide side) : Player(env, side), pin(pin) {
14+
HumanPlayer(Environment *env, const char pin, const PlayerSide side) : Player(env, side), pin(pin) {
1515
}
1616

1717
void update(const State &s) override {
1818
this->position.x = readJoystick();
1919
}
2020

2121
private:
22-
int readJoystick() const {
23-
int buff[6] = {};
22+
char readJoystick() const {
23+
char buff[6] = {};
2424

25-
for (int i = 0; i < 10; i++) {
25+
for (char i = 0; i < 10; i++) {
2626
buff[map(analogRead(this->pin), 0, 1023, 0, 5)]++;
2727
// auto y = map(analogRead(A5), 0, 1023, 0, 7);
2828
}
2929

3030
// find mode
31-
int max = 0, maxIdx = 0;
32-
for (int i = 0; i < 6; i++) {
31+
char max = 0, maxIdx = 0;
32+
for (char i = 0; i < 6; i++) {
3333
if (buff[i] > max) {
3434
max = buff[i];
3535
maxIdx = i;
@@ -39,7 +39,7 @@ class HumanPlayer : public Player {
3939
return maxIdx;
4040
}
4141

42-
int pin;
42+
char pin;
4343
};
4444

4545
#endif //HUMAN_H

include/player.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Player : public GameObject {
3030
void Init() override {
3131
this->position = Point{
3232
.x = static_cast<int>(random(0, 6)),
33-
.y = this->side == PlayerSide::LEFT ? 0 : 7
33+
.y = this->side == PlayerSide::LEFT ? 7 : 0
3434
};
35-
// this->score = 0;
35+
//this->score = 0;
3636
this->reward = 0;
3737
}
3838

include/q_player.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ const PROGMEM int EPSILON = 20;*/
2020
const PROGMEM float GAMMA = 0.98f;
2121
const PROGMEM int EPSILON = 30;*/
2222

23+
/*const PROGMEM float LEARNING_RATE = 0.40f;
24+
const PROGMEM float GAMMA = 0.90f;
25+
const PROGMEM int EPSILON = 5;*/
26+
2327
const PROGMEM float LEARNING_RATE = 0.40f;
2428
const PROGMEM float GAMMA = 0.90f;
25-
const PROGMEM int EPSILON = 5;
29+
const PROGMEM int EPSILON = 20;
2630

2731
/*const PROGMEM float LEARNING_RATE = 0.30f;
2832
const PROGMEM float GAMMA = 0.97f;
@@ -127,15 +131,17 @@ class QPlayer : public Player {
127131
}*/
128132
}
129133

134+
char Q_table[8][8][6][3] = {}; // init with zeros
135+
130136
private:
131-
int getPolicy(const State &s) {
132-
int max = 0;
137+
char getPolicy(const State &s) {
138+
char max = 0;
133139

134140
// Epsilon-greedy policy
135141
if (random(0, 100) < EPSILON) {
136-
max = static_cast<int>(random(0, 3));
142+
max = static_cast<char>(random(0, 3));
137143
} else {
138-
for (int a = 1; a < 3; a++) {
144+
for (char a = 1; a < 3; a++) {
139145
if (Q_table[s.ball_x][s.ball_y][s.player_x][a] > Q_table[s.ball_x][s.ball_y][s.player_x][max]) {
140146
max = a;
141147
}
@@ -145,8 +151,7 @@ class QPlayer : public Player {
145151
return max;
146152
}
147153

148-
char Q_table[8][8][6][3] = {}; // init with zeros
149-
int action;
154+
char action;
150155
};
151156

152157
#endif //Q_PLAYER_H

src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ void setup() {
1010

1111
void loop() {
1212
env.run();
13-
delay(10);
13+
//delay(100);
1414
}

0 commit comments

Comments
 (0)