]> git.lizzy.rs Git - lua-star.git/blob - tests/performance.lua
3D Support
[lua-star.git] / tests / performance.lua
1 --[[
2     Copyright 2018 Wesley Werner <wesley.werner@gmail.com>
3
4     Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
6     The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
8     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 ]]--
10
11 local luastar = require("lua-star")
12 local map = { }
13 local mapsize = 20
14 local numberOfTests = 1
15 local mapDensity = 0.1
16
17 local seed = os.time()
18 math.randomseed(seed)
19 print (string.format("Running with seed %d", seed))
20
21 print (string.format("Building a map of %dx%dx%d...", mapsize, mapsize, mapsize))
22 for x=1, mapsize do
23     map[x] = {}
24     for y=1, mapsize do
25                 map[x][y] = {}
26                 for z=1, mapsize do
27                         map[x][y][z] = math.random()
28                 end
29     end
30 end
31
32 -- precalculate a bunch of start and goal positions
33 -- doubled up for each start/goal pair
34
35 print (string.format("Precalculating %d random start/goal positions...", numberOfTests * 2))
36 local testPoints = { }
37 for i = 1, numberOfTests * 2 do
38     table.insert (testPoints, { x = math.random(1, mapsize), y = math.random(1, mapsize), z = math.random(1, mapsize)})
39 end
40
41 print (string.format("Finding %d paths...", numberOfTests))
42 function positionIsOpenFunc(x, y, z)
43     return map[x][y][z] > mapDensity
44 end
45 local testStart = os.clock()
46 for testNumber = 1, numberOfTests do
47     luastar:find(
48         mapsize, mapsize, mapsize, -- map size
49         table.remove (testPoints), -- start
50         table.remove (testPoints), -- goal
51         positionIsOpenFunc)
52 end
53 local testEnd = os.clock()
54 local totalSec = testEnd - testStart
55 local pathSec = totalSec / numberOfTests
56
57 print (string.format([[
58     Done in %.2f seconds.
59     That is %.4f seconds, or %d milliseconds, per path.
60     The map has %.1f million locations, with about %d%% open space.]],
61     totalSec, -- total seconds
62     pathSec, -- seconds per path
63     pathSec*1000, -- milliseconds per path
64     (mapsize*mapsize*mapsize)/1000000, -- number of locations
65     mapDensity*100 -- % open space on the map
66 ))