Skip to content

Commit 551f91b

Browse files
rootdavidhpark
authored andcommitted
Clean up
1 parent 9a33d82 commit 551f91b

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

pufferlib/ocean/bitflip/bitflip.h

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,9 @@ void add_log(BitFlip *env) {
4747
}
4848

4949
void c_reset(BitFlip *env) {
50-
// Clear observations
5150
memset(env->observations, OFF, env->size * 3 * sizeof(char));
52-
53-
// Clear n_correct
5451
env->n_correct = 0;
55-
56-
// Always make the first bit 1 to avoid "free" rounds just by chance
5752
env->observations[0] = ON;
58-
59-
// Initialize target pattern
6053
for (int i = 1; i < env->size; i++) {
6154
env->observations[i] = (rand() % 2 == 1) ? ON : OFF;
6255

@@ -65,32 +58,30 @@ void c_reset(BitFlip *env) {
6558
env->n_correct++;
6659
}
6760
}
68-
69-
// Initialize starting position in the middle
7061
env->pos = 2 * env->size + (env->size - 1) / 2;
7162
env->observations[env->pos] = CURSOR;
72-
73-
// Clear number of steps
7463
env->tick = 0;
7564
}
7665

7766
void c_step(BitFlip *env) {
78-
env->tick++;
67+
env->tick += 1;
7968

80-
env->observations[env->pos] = EMPTY;
69+
int action = env->actions[0];
70+
env->terminals[0] = 0;
71+
env->rewards[0] = 0.0;
8172

82-
if (env->actions[0] == LEFT) {
83-
env->pos--;
84-
}
73+
env->observations[env->pos] = EMPTY;
8574

86-
if (env->actions[0] == RIGHT) {
87-
env->pos++;
75+
if (action == LEFT) {
76+
env->pos -= 1;
77+
} else if (action == RIGHT) {
78+
env->pos += 1;
8879
}
8980

9081
if (env->tick == 12 * env->size || env->pos < 2 * env->size ||
9182
env->pos >= env->size * 3) {
92-
env->rewards[0] = -1.0;
9383
env->terminals[0] = 1;
84+
env->rewards[0] = -1.0;
9485
add_log(env);
9586
c_reset(env);
9687
return;
@@ -101,14 +92,13 @@ void c_step(BitFlip *env) {
10192
int state_idx = env->pos - env->size;
10293
int target_idx = env->pos - 2 * env->size;
10394

104-
// Flip bit
105-
if (env->actions[0] == FLIP) {
95+
if (action == FLIP) {
10696
env->observations[state_idx] ^= 1;
10797

10898
if (env->observations[state_idx] == env->observations[target_idx]) {
109-
env->n_correct++;
99+
env->n_correct += 1;
110100
} else {
111-
env->n_correct--;
101+
env->n_correct -= 1;
112102
}
113103
}
114104

@@ -119,9 +109,6 @@ void c_step(BitFlip *env) {
119109
c_reset(env);
120110
return;
121111
}
122-
123-
env->rewards[0] = 0.0;
124-
env->terminals[0] = 0;
125112
}
126113

127114
void c_render(BitFlip *env) {

0 commit comments

Comments
 (0)