Code
--Total LWS V2.0
--Based on Total LWS 1.0.4. by Rav3n_pl
--Bruno Kestemont 6 Feb 2013
--adapted to NC + minppi diminishes with loops 01/02/2014
---------------------------------------------------------------------------------------
--Generic parameters and functions
p=print --"quicker" print
segCnt=structure.GetCount()
--runnum=1
--minlen=1
--maxlen=7
--minppi=0.01
--NotAllLoops=true
timeLeft=os.difftime(puzzle.GetExpirationTime(),os.time())/86400
startTime=os.time()
PROBABLEFILTER=false
OriginalFilterSetting = behavior.GetSlowFiltersDisabled() -- new BK 8/4/2013
function Score()
return current.GetEnergyScore()
end
startingscore= Score()
function down1(x)--cut all afer 1-rd place
return x-x%0.1
end
function down(x)
return x-x%1
end
function detectfilter()
local descrTxt=puzzle.GetDescription()
if #descrTxt>0 and (descrTxt:find("filter") or descrTxt:find("filters")) then
PROBABLEFILTER=true
end
return
end
detectfilter()
function AllLoop()
local ok=false
-- First save structure !
for i=1, segCnt do
local ss=structure.GetSecondaryStructure(i)
if ss~="L" then
save.SaveSecondaryStructure()
ok=true
break
end
end
if ok then
selection.SelectAll()
structure.SetSecondaryStructureSelected("L")
end
end
function lw(minppi)
local gain=true
while gain do
local ss=Score()
if PROBABLEFILTER then behavior.SetSlowFiltersDisabled(true) end-- new BK 8/4/2013, always disable filter here
structure.LocalWiggleSelected(2) --structure.WiggleSelected
if PROBABLEFILTER then behavior.SetSlowFiltersDisabled(OriginalFilterSetting) end -- new BK 8/4/2013, always back to user settings
local g=Score()-ss
if g<minppi then gain=false end
if g<0 then recentbest.Restore() end
end
selection.DeselectAll() -- new
freeze.UnfreezeAll() -- new
end
--Total lws
function totalLws(minlen,maxlen,minppi)
local function Freeze(start, len)
freeze.UnfreezeAll()
selection.DeselectAll()
for f=start, segCnt, len+1 do
if f<= segCnt then selection.Select(f) end
end
freeze.FreezeSelected(true, false)
end
local function wiggle(start,len,minppi)
if start>1 then
selection.DeselectAll()
selection.SelectRange(1,start-1)
lw(minppi)
end
for i=start, segCnt, len+1 do
selection.DeselectAll()
local ss = i+1
local es=i+len
if ss >= segCnt then ss=segCnt end
if es >= segCnt then es=segCnt end
selection.SelectRange(ss,es)
lw(minppi)
end
end
freeze.UnfreezeAll()
selection.DeselectAll()
behavior.SetClashImportance(1)
save.SaveSecondaryStructure()
if NotAllLoops==true then
AllLoop()
end
local ssc=Score()
p("Starting Total LWS: "..ssc)
p("Lenght: "..minlen.." to "..maxlen.." ;minimum ppi: "..minppi)
for l=minlen, maxlen do
for s=1, l+1 do
p("Len: "..l.."..start point: "..s)
Freeze(s,l)
local sp=Score()
recentbest.Save()
wiggle(s,l,minppi)
local g=Score()-sp
p("Gained another: "..g)
end
end
p("Total LWS gain: "..Score()-ssc)
save.LoadSecondaryStructure()
end
function AskOptions()
local ask=dialog.CreateDialog("TotalLWS options")
--repeat
if timeLeft<2 then
ask.minlen=dialog.AddSlider("min lenght",1,1,3,0)
ask.maxlen=dialog.AddSlider("max lenght",7,3,15,0)
ask.minppi=dialog.AddSlider("Min points per iteration",0.01,0.001,0.3,3)
ask.runnum=dialog.AddSlider("Number runs",10,1,20,0)
elseif Score()<5000 then--]]
ask.minlen=dialog.AddSlider("min lenght",1,1,5,0)
ask.maxlen=dialog.AddSlider("max lenght",7,5,12,0)
ask.minppi=dialog.AddSlider("Min points per iteration",300,100,500,-1)
ask.runnum=dialog.AddSlider("Number runs",1,1,5,0)
else
ask.minlen=dialog.AddSlider("min lenght",1,1,6,0)
ask.maxlen=dialog.AddSlider("max lenght",7,6,12,0)
ask.minppi=dialog.AddSlider("Min points per iteration",0.1,0.01,3,2)
ask.runnum=dialog.AddSlider("Number runs",1,1,5,0)
end
ask.NotAllLoops = dialog.AddCheckbox("Keep structure",true)
ask.OK = dialog.AddButton("OK",1) ask.Cancel = dialog.AddButton("Cancel",0)
askresult=dialog.Show(ask)
--until askresult<2
--return askresult > 0
--end
if askresult > 0 then
minlen=ask.minlen.value -- minimum lenggh of sgmnts - if you have done lws by 1 and 2 you may want set it to 3
maxlen=ask.maxlen.value --segCntmum lenght of sgments - more than 7 looks useless
minppi=ask.minppi.value --minimum gain per local wiggle iter
runnum=ask.runnum.value
NotAllLoops=ask.NotAllLoops.value
print("Options:minlen =",minlen,", maxlen =",maxlen,", minppi =",minppi, ", runnum =",runnum, ", NotAllLoops =",NotAllLoops)
return true
end
return false
end
if AskOptions()==false then
return
end
--M A I N -----
--AskOptions()
while(true) do
for i=1, runnum do
print("run "..i.." / "..runnum)
totalLws(minlen,maxlen,minppi)
minppi=minppi*(1-(1/runnum)) -- new 01/02/2014
end
end
--recipe.ReportStatus()
duration=(os.time()-startTime)/60 -- in minutes
score=Score()
Recipegain=score-startingscore
RecipeReturn=Recipegain/duration*60 -- in pts/hour
print("Duration = "..down(duration).." min. Return= "..down1(RecipeReturn).." pts/hour.")