Created
October 24, 2016 02:46
-
-
Save liammcinroy/42cf093491015b76a412cbfa5ed96bcd to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
int width = 0; | |
int height = 0; | |
bool Send = true; | |
// Read in RAM and RL data. | |
bool readData(FILE* alePipe) { | |
char buffer[65535]; | |
char* err = fgets(buffer, sizeof(buffer), alePipe); | |
if (err == NULL) | |
{ | |
Send = false; | |
return false; | |
} | |
// Find the first colon, corresponding to the end of the RAM data | |
char* end = strchr(buffer, ':'); | |
if (end == NULL) //DIE sent | |
return true; | |
// Now parse the terminal bit | |
bool terminal = (end[1] == '1'); | |
// Also output reward whenever nonzero | |
int reward = strtol(&end[3], NULL, 10); | |
if (reward != 0) | |
std::cout << "Reward: " << reward << std::endl; | |
//now read screen data | |
char* current = buffer; | |
//while (current != end) | |
//{ | |
// //convert string to data.... | |
// current++; | |
//} | |
return terminal; | |
} | |
void agent_main(FILE* out_pipe, FILE* in_pipe) | |
{ | |
char buffer[1024]; | |
fgets(buffer, sizeof(buffer), in_pipe); | |
char ws[3] = { buffer[0], buffer[1], buffer[2] }; | |
char hs[3] = { buffer[4], buffer[5], buffer[6] }; | |
width = atoi(ws); | |
height = atoi(hs); | |
// Request screen & RL data from ALE | |
while (true) | |
{ | |
bool terminal = readData(in_pipe); | |
if (terminal) | |
break; | |
if (Send) | |
{ | |
int a = 1 + (rand() % 17); | |
fprintf(out_pipe, "%d,%d\n", a, 18); | |
fflush(out_pipe); | |
} | |
Send = true; | |
} | |
fprintf(out_pipe, "%d,%d\n", 0, 18); | |
fflush(out_pipe); | |
} | |
int main() | |
{ | |
std::string cmd("start ale\\ALE.exe"); | |
cmd += " -frame_skip 3 -disable_color_averaging false -display_screen true -game_controller fifo_named"; | |
cmd += " C:\\RL\\Arcade-Learning-Environment-0.5.1\\roms\\alien.BIN"; | |
std::cout << cmd.c_str() << std::endl; | |
FILE* out_pipe = fopen("ale_fifo_in", "w"); | |
if (out_pipe == NULL) | |
perror("error in:"); | |
fputs("1,0,0,1\n", out_pipe); | |
fflush(out_pipe); | |
//system(cmd.c_str()); | |
FILE* proc = _popen(cmd.c_str(), "r"); | |
FILE* in_pipe = fopen("ale_fifo_out", "r"); | |
if (in_pipe == NULL) | |
perror("error out:"); | |
agent_main(out_pipe, in_pipe); | |
//close pipe | |
fclose(in_pipe); | |
fclose(out_pipe); | |
_pclose(proc); | |
cout << "SIMULATION OVER" << endl; | |
cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment