Joined: 6/4/2020
Posts: 42
I am having a strange issue. I am making a Lua script for reinforcement learning in Super Mario Bros. The script automatically loads a ROM and plays a movie until it is finished, then tries to make inputs that maximizes Mario's X coordinate. The details don't matter. My problem is that for some reason, FCEUX completely crashes as soon as I run the script. Other scripts work fine. I deliberately put a print command at the start to see if at least that gets executed, but nothing gets printed. Nothing I tried has worked.(By the way, I don't really care about how awful my coding is, I just want the thing to run at all.) As far as I know, the file size is probably not the issue since I have made longer scripts and they work flawlessly. I'm using the 64-bit interim build because emu.loadrom(~) wasn't supported on FCEUX 2.2.3 and I'm too stupid to know how to build FCEUX from the WIN32 binary. Any help would be appreciated. Thank you in advance. Also, I'm not available on Discord anymore, for at least a week.
------------------Preparation--------------
print(0)  --check if the thing even runs at all
screenX = 6
screenY = 6
NetworkLearnRate = 0.0000001
LearnRate = 0.6
ROMlocation = "SMB(NTSC).zip"
primaryMovieLocation = "starter.fm2"  --the movie it first plays
secondaryMovieLocation = "BOTT.fm2"   --the movie it outputs
DiscoutFactor = 0.99
RNGfalloff = 0.99
randomness = 0.5
episodecount = 10
timestep = 600
triesPerEpisode = 150
NetDimensions = {screenX * screenY, 16}
main = savestate.object(5)
buffer = savestate.object(6)
print(1)
function average(list)
  local a = 0
  for x = 1,#list do
    a = a + list[x]
  end
  return a / #list
end
function sigmoid(x)
  return 1 / (1 + (2.71828 ^ (-x)))
end
function leakrelu(x)
  if x <0> list[big[1]] then
      big = {x}
    elseif list[x] == list[big[1]] and big[1] ~= x then
      table.insert(big,x)
    end
  end
  return big[math.random(1,#big)]
end
function smallest(list)
  local big = {1}
  for x = 2, #list do
    if list[x] <list>= 2 ^ (bitcount - x) then
      table.insert(array, 1)
      accumulator = accumulator - 2 ^ (bitcount - x)
    else
      table.insert(array, 0)
    end
  end
  return array
end
function utility()
  return memory.readbyte(109) * 256 + memory.readbyte(134)
end
function memoryize(table1, table2, ratio)
  local dummyarray = table1
  for x = 1, #table1 do
    dummyarray[x] = (table1[x] + ratio * table2[x]) / (ratio + 1)
  end
  return dummyarray
end
function bellman(table1, maxq, dicountfactor, learningrate)
  local dummyarray = table1
  for x = 1, #table1 do
    dummyarray[x] = ((1 - learningrate) * table1[x] + learningrate * dicountfactor * maxq)
  end
  return dummyarray
end
weights = {}
biases = {}
nodes = {{}}
q = 0
futuremaxq = 0
input = {}
screen = {}
fitnessvalue = 0
bestfitness = 0
out = {}
RNG = randomness
dummyarray = {}
for x = 1, screenX * screenY do
  table.insert(screen, 0)
end
print(2)
----------Load ROM, play movie until finished, save state----------
(continues in next reply)
Post subject: Strange Issue Part 2
Joined: 6/4/2020
Posts: 42
init()
emu.loadrom(ROMlocation)
movie.play(primaryMovieLocation, true)
while movie.mode() ~= "finished" do
table.insert(dummyarray, joypad.get(1))
  end
movie.stop()movie.record(secondaryMovieLocation)
movie.rerecordcounting(true)
for x = 1, #dummyarray do
  joypad.set(1, dummyarray[x])
  emu.frameadvance()
end
savestate.save(main)
print(3)
--------------------------------AI---------------------------------
for step = 1, episodecount do
  RNG = randomness
  for attempt = 1, triesPerEpisode do
    savestate.load(main)
    for x = 1, screenX * screenY do
      screen[x] = 0
    end
    for frame = 1, timestep do
      screen = memoryize(screen, getscreen(screenX, screenY))
      predict(screen)
      out = nodes[#nodes]
      if math.random() <RNG> bestfitness then
       bestfitness = fitness
       savestate.save(buffer)
    end
  end
  savestate.load(buffer)
  savestate.save(main)
end
HHS
Active player (286)
Joined: 10/8/2006
Posts: 356
If it hangs, you will not see the value that gets printed, since the window does not update until the script blocks. This is the problem:
while movie.mode() ~= "finished" do
table.insert(dummyarray, joypad.get(1))
  end
You forgot to call emu.frameadvance().
Joined: 6/4/2020
Posts: 42
Thank you! I mistakenly thought this post was deleted, so I couldn't check sooner. I'll implement the fix.
Post subject: Delete This Topic
Joined: 6/4/2020
Posts: 42
This topic is no longer of any significance and I would personally like it if someone removed it.