how to understand a table of tables in lua? - lua

> polyline = {color = "blue", thickness = 2, npoints = 4, {x=0,y=0}, {x=-10,y=0}, {x=-10,y=1}, {x=0,y=1}}
> print(polyline[2])
table: 0x55ad5c0f3f90
> print(polyline[2].x)
-10
Why does print(polyline[2]) give out -10 ?

If you do not provide a key explicitly, table elements are assigned to numeric keys within the table constructor.
polyline = {color = "blue", thickness = 2, npoints = 4, {x=0,y=0}, {x=-10,y=0}, {x=-10,y=1}, {x=0,y=1}}
is equivalent to
do
polyline = {}
polyline.color = "blue"
polyline.thickness = 2
polyline.npoints = 4
do
polyline[1] = {}
polyline[1].x = 0
polyline[1].y = 0
end
do
polyline[2] = {}
polyline[2].x = -10
polyline[2].y = 0
end
do
polyline[3] = {}
polyline[3].x = -10
polyline[3].y = 1
end
do
polyline[4] = {}
polyline[4].x = 0
polyline[4].y = 1
end
end
Refer to
https://www.lua.org/manual/5.3/manual.html#3.4.9

Related

What is the problem with my Gradient Descent Algorithm or how its applied?

I've been trying figure out what I have done wrong for many hours, but just can't figure out. I've even looked at other basic Neural Network libraries to make sure that my gradient descent algorithms were correct, but it still isn't working.
I'm trying to teach it XOR but it outputs -
input (0 0) | 0.011441891321516094
input (1 0) | 0.6558508610135193
input (0 1) | 0.6558003273099053
input (1 1) | 0.6563021185296245
after 1000 trainings, so clearly there's something wrong.
The code is written in lua and I created the Neural Network from raw data so you can easily understand how the data is formatted.
- Training code -
math.randomseed(os.time())
local nn = require("NeuralNetwork")
local network = nn.newFromRawData({
["activationFunction"] = "sigmoid",
["learningRate"] = 0.3,
["net"] = {
[1] = {
[1] = {
["value"] = 0
},
[2] = {
["value"] = 0
}
},
[2] = {
[1] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
},
[2] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
},
[3] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
},
[4] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
}
},
[3] = {
[1] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1,
[3] = 1,
[4] = 1
}
}
}
}
})
attempts = 1000
for i = 1,attempts do
network:backPropagate({0,0},{0})
network:backPropagate({1,0},{1})
network:backPropagate({0,1},{1})
network:backPropagate({1,1},{0})
end
print("Results:")
print("input (0 0) | "..network:feedForward({0,0})[1])
print("input (1 0) | "..network:feedForward({1,0})[1])
print("input (0 1) | "..network:feedForward({0,1})[1])
print("input (1 1) | "..network:feedForward({1,1})[1])
- Library -
local nn = {}
nn.__index = nn
nn.ActivationFunctions = {
sigmoid = function(x) return 1/(1+math.exp(-x/1)) end,
ReLu = function(x) return math.max(0, x) end,
}
nn.Derivatives = {
sigmoid = function(x) return x * (1 - x) end,
ReLu = function(x) return x > 0 and 1 or 0 end,
}
nn.CostFunctions = {
MSE = function(outputs, expected)
local sum = 0
for i = 1, #outputs do
sum += 1/2*(expected[i] - outputs[i])^2
end
return sum/#outputs
end,
}
function nn.new(inputs, outputs, hiddenLayers, neurons, learningRate, activationFunction)
local self = setmetatable({}, nn)
self.learningRate = learningRate or .3
self.activationFunction = activationFunction or "ReLu"
self.net = {}
local net = self.net
local layers = hiddenLayers+2
for i = 1, layers do
net[i] = {}
end
for i = 1, inputs do
net[1][i] = {value = 0}
end
for i = 2, layers-1 do
for x = 1, neurons do
net[i][x] = {netInput = 0, value = 0, bias = math.random()*2-1, weights = {}}
for z = 1, #net[i-1] do
net[i][x].weights[z] = math.random()*2-1
end
end
end
for i = 1, outputs do
net[layers][i] = {netInput = 0, value = 0, bias = math.random()*2-1, weights = {}}
for z = 1, #net[layers-1] do
net[layers][i].weights[z] = math.random()*2-1
end
end
return self
end
function nn.newFromRawData(data)
return setmetatable(data, nn)
end
function nn:feedForward(inputs)
local net = self.net
local activation = self.activationFunction
local layers = #net
local inputLayer = net[1]
local outputLayer = net[layers]
for i = 1, #inputLayer do
inputLayer[i].value = inputs[i]
end
for i = 2, layers do
local layer = net[i]
for x = 1, #layer do
local sum = layer[x].bias
for z = 1, #net[i-1] do
sum += net[i-1][z].value * layer[x].weights[z]
end
layer[x].netInput = sum
layer[x].value = nn.ActivationFunctions[activation](sum)
end
end
local outputs = {}
for i = 1, #outputLayer do
table.insert(outputs, outputLayer[i].value)
end
return outputs
end
function nn:backPropagate(inputs, expected)
local outputs = self:feedForward(inputs)
local net = self.net
local activation = self.activationFunction
local layers = #net
local lr = self.learningRate
local inputLayer = net[1]
local outputLayer = net[layers]
for i = 1, #outputLayer do
local delta = -(expected[i] - outputs[i]) * nn.Derivatives[activation](net[layers][i].value)
outputLayer[i].delta = delta
end
for i = layers-1, 2, -1 do
local layer = net[i]
local nextLayer = net[i+1]
for x = 1, #layer do
local delta = 0
for z = 1, #nextLayer do
delta += nextLayer[z].delta * nextLayer[z].weights[x]
end
layer[x].delta = delta * nn.Derivatives[activation](layer[x].value)
end
end
for i = 2, layers do
local lastLayer = net[i-1]
for x = 1, #net[i] do
net[i][x].bias -= lr * net[i][x].delta
for z = 1, #lastLayer do
net[i][x].weights[z] -= lr * net[i][x].delta * lastLayer[z].value
end
end
end
end
return nn
Any help would be highly appreciated, thanks!
All initial weights must be DIFFERENT numbers, otherwise backpropagation will not work. For example, you can replace 1 with math.random()
Increase number of attempts to 10000
With these modifications, your code works fine:
Results:
input (0 0) | 0.028138230938126
input (1 0) | 0.97809448578087
input (0 1) | 0.97785000216126
input (1 1) | 0.023128477689456

Xonix Game Optimization Problems

I'm doing a Xonix game on corona sdk. As a playing field, I decided to use a grid of squares. Width 44, Height 71. As a result, I received a row consisting of 3124(44*71) squares along which the player moves. But I have problems with optimization. The engine can not cope with such a number of squares on the screen at the same time. Because of this, FPS on a smartphone is not more than 12. Help me find the best solution. All that concerns polygons and meshes, I am not strong in geometry ((((
Reducing the number of squares is a bad idea. Then the appearance is lost, and the field is captured too large pieces.
Thank you in advance!
-- Creating a playing field
M.create_pole = function()
-- init
local image = display.newImage
local rect = display.newRect
local rrect = display.newRoundedRect
local floor = math.floor
M.GR_ZONE = display.newGroup()
M.POLE = {}
-- bg
M.POLE.bg = rrect(0,0,150,150,5)
M.POLE.bg.width = M.width_pole
M.POLE.bg.height = M.height_pole
M.POLE.bg.x = _W/2
M.POLE.bg.y = _H/2
M.POLE.bg.xScale = _H*.00395
M.POLE.bg.yScale = _H*.00395
M.POLE.bg:setFillColor(API.hexToCmyk(M.color.land))
M.GR:insert(M.POLE.bg)
M.POLE.img = image(IMAGES..'picture/test.jpg')
M.POLE.img.width = M.width_pole-M.size_xonix
M.POLE.img.height = M.height_pole-M.size_xonix
M.POLE.img.x = _W/2
M.POLE.img.y = _H/2
M.POLE.img.xScale = _H*.00395
M.POLE.img.yScale = _H*.00395
M.POLE.img.strokeWidth = 1.3
M.POLE.img:setStrokeColor(API.hexToCmyk(M.color.img))
M.GR:insert(M.POLE.img)
-- control player
M.POLE.bg:addEventListener( 'touch', M.swipe )
M.POLE.bg:addEventListener( 'tap', function(e)
M.POLE.player.state = 0
end )
-- arr field
M.arr_pole = {} -- newRect
local jj = 0
for i=1,M.delta_height do -- 71 point
M.arr_pole[i] = {}
M.GUI.border[i] = {}
for k=1,M.delta_width do -- 44 point
jj = jj+1
M.arr_pole[i][k] = {nm = jj}
M.GUI.border[i][k] = {}
local xx = k*M.size_xonix
local yy = i*M.size_xonix
local _x = floor(xx-M.size_xonix/2)
local _y = floor(yy-M.size_xonix/2)
M.arr_pole[i][k][1] = image(IMAGES..'pole/land.jpg')
M.arr_pole[i][k][1] .x = _x
M.arr_pole[i][k][1] .y = _y
M.GR_ZONE:insert(M.arr_pole[i][k][1])
-- water at the edges
-- the rest is land
if (i==1 or i==M.delta_height)
or (k==1 or k==M.delta_width) then
M.arr_pole[i][k].type = 0
else
M.arr_pole[i][k].type = 2
M.arr_pole[i][k][1].isVisible = true
end
end
end
M.GR_ZONE.width = M.POLE.bg.width*M.POLE.bg.xScale
M.GR_ZONE.height = M.POLE.bg.height*M.POLE.bg.yScale
M.GR_ZONE.x = M.POLE.bg.x-M.POLE.bg.width*M.POLE.bg.xScale/2
M.GR_ZONE.y = M.POLE.bg.y-M.POLE.bg.height*M.POLE.bg.yScale/2
-- player
local _x = M.arr_pole[1][1][1].x
local _y = M.arr_pole[1][1][1].y
M.POLE.player = image(IMAGES..'ui/player.png')
M.POLE.player.x = _x
M.POLE.player.y = _y
M.POLE.player.width = M.size_xonix*1.5
M.POLE.player.height = M.size_xonix*1.5
M.POLE.player.state = 0
M.GR_ZONE:insert(M.POLE.player)
M.POLE.player:toFront()
end
-- get all free cells
- land
M.get_free = function(_i,_j,_start)
local _par = pairs
local _fill = M.filling_arr
local _arr = M.arr_pole
local _index = {
{-1,-1}, {0,-1}, {1,-1},
{-1, 0}, {1, 0},
{-1, 1}, {0, 1}, {1, 1},
}
-- mark as verified
_arr[_i][_j].is_check = true
_fill[#_fill+1] = {_i,_j}
for k,v in _par(_index) do
local i = _i+v[1]
local j = _j+v[2]
if i>1 and i<M.delta_height
and j>1 and j<M.delta_width then
if not _arr[i][j].is_check then
if _arr[i][j].type==2 then
M.get_free(i,j)
end
end
end
end
if _start then
return _fill
end
end
-- fill(capture)
M.filling = function()
-- init
local par = pairs
local tb_rem = table.remove
local arr = M.arr_pole
M.island_arr = {}
-- check indicator
for i,ii in par(arr) do
for j,jj in par(ii) do
if jj.type==2 then
jj.is_check = false
end
end
end
-- we divide land into islands
for i,ii in par(arr) do
for j,jj in par(ii) do
if jj.type==2 and not jj.is_check then
M.filling_arr = {}
M.island_arr[#M.island_arr+1] = M.get_free(i,j,true)
end
end
end
- find a larger island and delete it
local sel_max = {dir = '', count = 1}
for k,v in par(M.island_arr) do
if #v>=sel_max.count then
sel_max.dir = k
sel_max.count = #v
end
end
if #M.island_arr>0 then
tb_rem(M.island_arr,sel_max.dir)
end
-- fill
for k,v in par(M.island_arr) do
for kk,vv in par(v) do
local obj = arr[vv[1]][vv[2]]
obj.type = 0
obj[1].isVisible = false
end
end
-- turning the edges into water
for k,v in par(M.bread_crumbs) do
local arr = arr[v.arr_y][v.arr_x]
arr.type = 0
arr[1].isVisible = false
end
M.clear_history()
end

Why is this lua dijkstra's algorithm not working in some cases?

Im using Dijkstra algorythm code from this site: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Lua
Unfortunately It doesnt work for current edges table.
I have determined that the problem disappears when I delete the connection from 35 -> 36, but It doesnt solve the problem.
-- Graph definition
local edges = {
[34] = {[35] = 1,[37] = 1,},
[35] = {[34] = 1,[36] = 1,[46] = 1,},
[36] = {[35] = 1,[37] = 1,},
[37] = {[34] = 1,[36] = 1,},
[38] = {[46] = 1,},
[46] = {[35] = 1,[38] = 1,},
}
-- Fill in paths in the opposite direction to the stated edges
function complete (graph)
for node, edges in pairs(graph) do
for edge, distance in pairs(edges) do
if not graph[edge] then graph[edge] = {} end
graph[edge][node] = distance
end
end
end
-- Create path string from table of previous nodes
function follow (trail, destination)
local path, nextStep = destination, trail[destination]
while nextStep do
path = nextStep .. " " .. path
nextStep = trail[nextStep]
end
return path
end
-- Find the shortest path between the current and destination nodes
function dijkstra (graph, current, destination, directed)
if not directed then complete(graph) end
local unvisited, distanceTo, trail = {}, {}, {}
local nearest, nextNode, tentative
for node, edgeDists in pairs(graph) do
if node == current then
distanceTo[node] = 0
trail[current] = false
else
distanceTo[node] = math.huge
unvisited[node] = true
end
end
repeat
nearest = math.huge
for neighbour, pathDist in pairs(graph[current]) do
if unvisited[neighbour] then
tentative = distanceTo[current] + pathDist
if tentative < distanceTo[neighbour] then
distanceTo[neighbour] = tentative
trail[neighbour] = current
end
if tentative < nearest then
nearest = tentative
nextNode = neighbour
end
end
end
unvisited[current] = false
current = nextNode
until unvisited[destination] == false or nearest == math.huge
return distanceTo[destination], follow(trail, destination)
end
-- Main procedure
print("Directed:", dijkstra(edges, 34, 38, true))
print("Undirected:", dijkstra(edges, 34, 38, false))
I recieve the output of inf, 38 with current egdes table content but when I delete the connection between 35 -> 36 it gives an good output - 3, 34 35 46 38
For easier understand im uploading the graphic representation of edges table: https://i.imgur.com/FFF22C1.png
As you can see the route is corrent when we start from 34 -> 35 -> 46 -> 38 but as I sad It works only when connection from 35 to 36 is not existing.
Why it is not working in the case showed in my code?
This is an example of Dijkstra algorithm implementation
-- Graph definition
local edges = {
[34] = {[35] = 1,[37] = 1,},
[35] = {[34] = 1,[36] = 1,[46] = 1,},
[36] = {[35] = 1,[37] = 1,},
[37] = {[34] = 1,[36] = 1,},
[38] = {[46] = 1,},
[46] = {[35] = 1,[38] = 1,},
}
local starting_vertex, destination_vertex = 34, 38
local function create_dijkstra(starting_vertex)
local shortest_paths = {[starting_vertex] = {full_distance = 0}}
local vertex, distance, heap_size, heap = starting_vertex, 0, 0, {}
return
function (adjacent_vertex, edge_length)
if adjacent_vertex then
-- receiving the information about adjacent vertex
local new_distance = distance + edge_length
local adjacent_vertex_info = shortest_paths[adjacent_vertex]
local pos
if adjacent_vertex_info then
if new_distance < adjacent_vertex_info.full_distance then
adjacent_vertex_info.full_distance = new_distance
adjacent_vertex_info.previous_vertex = vertex
pos = adjacent_vertex_info.index
else
return
end
else
adjacent_vertex_info = {full_distance = new_distance, previous_vertex = vertex, index = 0}
shortest_paths[adjacent_vertex] = adjacent_vertex_info
heap_size = heap_size + 1
pos = heap_size
end
while pos > 1 do
local parent_pos = (pos - pos % 2) / 2
local parent = heap[parent_pos]
local parent_info = shortest_paths[parent]
if new_distance < parent_info.full_distance then
heap[pos] = parent
parent_info.index = pos
pos = parent_pos
else
break
end
end
heap[pos] = adjacent_vertex
adjacent_vertex_info.index = pos
elseif heap_size > 0 then
-- which vertex neighborhood to ask for?
vertex = heap[1]
local parent = heap[heap_size]
heap[heap_size] = nil
heap_size = heap_size - 1
if heap_size > 0 then
local pos = 1
local last_node_pos = heap_size / 2
local parent_info = shortest_paths[parent]
local parent_distance = parent_info.full_distance
while pos <= last_node_pos do
local child_pos = pos + pos
local child = heap[child_pos]
local child_info = shortest_paths[child]
local child_distance = child_info.full_distance
if child_pos < heap_size then
local child_pos2 = child_pos + 1
local child2 = heap[child_pos2]
local child2_info = shortest_paths[child2]
local child2_distance = child2_info.full_distance
if child2_distance < child_distance then
child_pos = child_pos2
child = child2
child_info = child2_info
child_distance = child2_distance
end
end
if child_distance < parent_distance then
heap[pos] = child
child_info.index = pos
pos = child_pos
else
break
end
end
heap[pos] = parent
parent_info.index = pos
end
local vertex_info = shortest_paths[vertex]
vertex_info.index = nil
distance = vertex_info.full_distance
return vertex
end
end,
shortest_paths
end
local vertex, dijkstra, shortest_paths = starting_vertex, create_dijkstra(starting_vertex)
while vertex and vertex ~= destination_vertex do
-- send information about all adjacent vertexes of "vertex"
for adjacent_vertex, edge_length in pairs(edges[vertex]) do
dijkstra(adjacent_vertex, edge_length)
end
vertex = dijkstra() -- now dijkstra is asking you about the neighborhood of another vertex
end
if vertex then
local full_distance = shortest_paths[vertex].full_distance
local path = vertex
while vertex do
vertex = shortest_paths[vertex].previous_vertex
if vertex then
path = vertex.." "..path
end
end
print(full_distance, path)
else
print"Path not found"
end

Most optimized way to get value from table

I need to check if first value is >= 'from' and second value is <= 'to', if true then my function retun number. It's working but I don't know if this is the best and most optimized way to get value(number from table).
local table = {
{from = -1, to = 12483, number = 0},
{from = 12484, to = 31211, number = 1},
{from = 31212, to = 53057, number = 2},
{from = 53058, to = 90200, number = 3},
{from = 90201, to = 153341, number = 4},
{from = 153342, to = 443162, number = 5},
{from = 443163, to = 753380, number = 6},
{from = 753381, to = 1280747, number = 7},
{from = 1280748, to = 2689570, number = 8},
{from = 2689571, to = 6723927, number = 9},
{from = 6723928, to = 6723928, number = 10}
}
local exampleFromValue = 31244
local exampleToValue = 42057
local function getNumber()
local number = 0
for k, v in pairs(table) do
if (v.from and exampleFromValue >= v.from) and (v.to and exampleToValue <= v.to) then
number = v.number
break
end
end
return number
end
print(getNumber())
With this small amount of data, such function doesn't seem like a performace issue. However, you can compress the data a bit:
local t = {
12484, 31212, 53058, 90201, 153342, 443163, 753381, 1280748, 2689571, 6723928
}
local exampleFromValue = 31244
local exampleToValue = 42057
local function getNumber()
local last = -1
for i, v in ipairs(t) do
if exampleFromValue >= last and exampleToValue < v then
return i - 1
end
last = v
end
return 0
end

Draw table of multiple types

I have two zombie objects, one is Grunt and one is Runner.
My Grunt.lua file:
function InitGrunt()
grunt = {}
grunt.x = 0
grunt.y = 0
grunt.speed = 120
grunt.hitBox = (sprites.grunt:getHeight() + sprites.grunt:getWidth())/2
grunt.hit = false
gruntDefCD = 2
gruntCD = gruntDefCD
gruntMinCD = 0.4
gruntTimer = gruntCD
gruntTimerDecr = 0.8
end
function SpawnGrunt()
local side = math.random(1, 4)
--randomize spawn position
table.insert( zombies, grunt )
end
and my Runner.lua:
function InitRunner()
runner = {}
runner.x = 0
runner.y = 0
runner.speed = 240
runner.hitBox = (sprites.runner:getWidth() + sprites.runner:getHeight())/2
runner.hit = false
runnerDefCD = 4
runnerCD = runnerDefCD
runnerMinCD = 2
runnerTimer = runnerCD
runnerTimerDecr = 0.95
end
function SpawnRunner()
local side = math.random(1, 4)
--randomize spawn position
table.insert( zombies, runner )
end
So zombie table will have grunts and runners. How do I print both of them in Draw()?
I can draw one with:
function DrawGrunts()
for i, z in ipairs(zombies) do
love.graphics.draw(sprites.grunt, z.x, z.y, PlayerZombieAngle(z), nil, nil, sprites.grunt:getWidth()/2, sprites.grunt:getHeight()/2 )
end
end
But how do I draw both of them in one function, ideally?
Give the different entities a pointer to their sprite object:
runner = { }
runner.x = 0
runner.y = 0
runner.sprite = sprites.runner
-- Rest of runner definition
grunt = { }
grunt.x = 0
grunt.y = 0
grunt.sprite = sprites.grunt
-- Rest of grunt definition
And your draw function becomes:
for i, z in ipairs(zombies) do
love.graphics.draw(z.sprite, z.x, z.y, ...)
end

Resources