From 88a6f54ef3e199f6dfef67f7a6d037f29dd47245 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 2 Jan 2025 18:40:21 -0600 Subject: begin implementing metagrammar for ddf --- .gitignore | 2 ++ example.lua | 21 ++++++++++++++++----- grammar.lua | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3819313 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +*.swo diff --git a/example.lua b/example.lua index 2bd87fa..9f4a852 100644 --- a/example.lua +++ b/example.lua @@ -17,10 +17,15 @@ for line in csvFile:lines() do end end -print(#data) -for i, v in ipairs(data[1]) do - print(i, v) -end + +local metaproduction = {} +metaproduction[""] = {{''}} +metaproduction[""] = { {}, {'', ''} } +metaproduction[""] = {{ 'defun(', '', ')\n' }} +metaproduction[""] = { {'0'}, {'1+', ''} } + +local metagrammar = Grammar.createGrammar(metaproduction, {}) + local production = {} production[""]={{''}} @@ -41,7 +46,8 @@ production[""]={ {'0.','','',''} } local grammar = Grammar.createGrammar(production, {}) -math.randomseed(0) +--math.randomseed(0) +math.randomseed(os.time()) function randomGenome() @@ -54,6 +60,11 @@ function randomGenome() end +local g = randomGenome() +print(#g, metagrammar:expand(randomGenome())) +os.exit(0) + + function randomPop() local pop = {} for n=1,1000 do diff --git a/grammar.lua b/grammar.lua index 286723d..d4adf32 100644 --- a/grammar.lua +++ b/grammar.lua @@ -59,10 +59,10 @@ function Grammar.expandSequence(self, sequence, n) end -function Grammar.expand(self, genome, maxLength) +function Grammar.expand(self, genome, geneIndex, maxLoops) + geneIndex = 0 maxLoops = maxLoops or 2 local sequence = { '' } - local geneIndex = 0 local count = 0 local continue = true while continue do -- cgit v1.2.1