This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct Log Log; | |
struct Log { | |
float score; | |
float moves; | |
}; | |
typedef struct LogBuffer LogBuffer; | |
struct LogBuffer { | |
Log *logs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct Log Log; | |
struct Log { | |
float score; | |
float moves; | |
}; | |
typedef struct LogBuffer LogBuffer; | |
struct LogBuffer { | |
Log *logs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct Agent Agent; | |
struct Agent { | |
int r; | |
int c; | |
int id; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct CCpr CCpr; | |
struct CCpr { | |
int width; | |
int height; | |
int num_agents; | |
int vision; | |
int vision_window; | |
int obs_size; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct FoodList FoodList; | |
struct FoodList { | |
int *indexes; // Grid flattened index positions | |
int size; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void spawn_foods(CCpr *env) { | |
// After each step, check existing foods and spawns new food in the | |
// neighborhood Iterates over food_list for efficiency instead of the entire | |
// grid. | |
FoodList *foods = env->foods; | |
int original_size = foods->size; | |
for (int i = 0; i < original_size; i++) { | |
int idx = foods->indexes[i]; | |
int offset = idx - env->width - 1; // Food spawn in 1 radius | |
int r = offset / env->width; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void compute_observations(CCpr *env) { | |
for (int i = 0; i < env->num_agents; i++) { | |
Agent *agent = &env->agents[i]; | |
int obs_offset = i * env->obs_size; | |
int r_offset = agent->r - env->vision; | |
int c_offset = agent->c - env->vision; | |
for (int r = 0; r < 2 * env->vision + 1; r++) { | |
for (int c = 0; c < 2 * env->vision + 1; c++) { | |
int grid_idx = (r_offset + r) * env->width + c_offset + c; | |
int obs_idx = obs_offset + r * env->vision_window + c; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void c_reset(CCpr *env) { | |
env->tick = 0; | |
for (int r = 0; r < env->height; r++) { | |
for (int c = 0; c < env->width; c++) { | |
int adr = grid_index(env, r, c); | |
env->grid[adr] = EMPTY; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void c_step(CCpr *env) { | |
memset(env->rewards, 0, env->num_agents * sizeof(float)); | |
memset(env->interactive_food_agent_count, 0, | |
(env->width * env->height + 7) / 8); | |
for (int i = 0; i < env->num_agents; i++) { | |
step_agent(env, i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void step_agent(CCpr *env, int i) { | |
int action = env->actions[i]; | |
int dr = 0; | |
int dc = 0; | |
switch (action) { | |
case 0: | |
dr = -1; |
OlderNewer