Profile
- Name
- LuckyLWS V3
- ID
- 103556
- Shared with
- Public
- Parent
- None
- Children
- Created on
- June 04, 2020 at 23:32 PM UTC
- Updated on
- June 04, 2020 at 23:32 PM UTC
- Description
DO NOT USE: Local Wiggling in lua scripting V2 is not the same as in lua V1. I don't know why. Use LuckyLWSV2.5 as it will work better and get you more points
LuckyLWSV2.5 re-written to use foldit ScriptV2
Changes from the previous version:
* Disables filters to improve performance
* Local wiggles now last 25 iterations
Best for
Code
-- Rewrite of LuckLWS v2.5 using foldit API V2
segCnt = structure.GetCount()
function round(x) --round off to three digits
return x-x%0.001
end
function getPoints()
t={}
print("Searching best scoring segments.")
local ss = current.GetEnergyScore() -- Get the current puzzle score
freeze.UnfreezeAll() -- Unfreeze all segments
for i=startS, endS do
-- Skip over locked segments by giving them a net score of 0
if not structure.IsLocked(i) then
save.Quicksave(4)
selection.DeselectAll() -- Deselect all segments
selection.Select(i) -- Select segment
local scs = current.GetSegmentEnergyScore(i) -- Get the current segment score
structure.WiggleSelected(1) -- local wiggle the segment for 1 iteration
local g = current.GetSegmentEnergyScore(i) - scs -- Find difference before and after wiggling
t[#t+1]={i,g} -- Store the difference score in an array
print("Segment ", i, " score: ", g)
if g<keepTest then save.Quickload(4) end
else
local g = 0 -- Find difference before and after wiggling
t[#t+1]={i,g} -- Store the difference score in an array
print("Segment ", i, " score: ", g)
end
end
print("Test Score Gain: ", round(current.GetEnergyScore() - ss)) -- Print total score gain
return t
end
function wig(minGain)
repeat
local ss =current.GetEnergyScore()
structure.LocalWiggleSelected(2) -- local wiggle the segment for 25 iterations
local wg=current.GetEnergyScore()-ss
if wg<0 then
recentbest.Restore() -- Restore recent best if we did not gain points
end
until wg<mingain --Stop running the loop if the gain does not exceed the minimium gain
selection.DeselectAll()
end
function wiggle(s, mingain, buddies)
print("Wiggling segment ",s)
selection.DeselectAll()
local sgs=current.GetEnergyScore()
selection.Select(s)
wig(mingain)
if buddies > 0 then --select buddies
for b=1, buddies do
selection.DeselectAll()
if s+b>endS then selection.SelectRange(s,endS)
else selection.SelectRange(s,s+b)end
wig(mingain)
if s-b<startS then selection.SelectRange(startS,s)
else selection.SelectRange(s-b,s)end
wig(mingain)
if s+b>endS then selection.SelectRange(s,endS)
else selection.SelectRange(s,s+b)end
if s-b<startS then selection.SelectRange(startS,s)
else selection.SelectRange(s-b,s)end
wig(mingain)
end
end
print("Segment gain: ",round(current.GetEnergyScore() -sgs))
end
function AllLoop() --turning entire structure to loops
local ok=false
for i=1, segCnt do
local s=structure.GetSecondaryStructure(i)
if s~="L" then
save.SaveSecondaryStructure()
ok=true
break
end
end
if ok then
selection.SelectAll()
structure.SetSecondaryStructureSelected("L")
end
end
function Sort(tab,items) -- bubble sorting - lowest on top, only needed items
if items>#tab then items=#tab end
for x=1,items do --items do
for y=x+1,#tab do
if tab[x][2]<tab[y][2] then
tab[x],tab[y]=tab[y],tab[x]
end
end
end
return tab
end
function LuckyLWS()
behavior.SetSlowFiltersDisabled(true)
if endS==nil then endS=segCnt end
behavior.SetClashImportance(1)
freeze.UnfreezeAll()
AllLoop()
recentbest.Save()
sscore=current.GetEnergyScore()
besttable=getPoints()
besttable=Sort(besttable,howmany)
if howmany>#besttable then howmany=#besttable end
for i=1, howmany do
if besttable[i][2]>=LWSonly then
local seg=besttable[i][1]
recentbest.Save()
wiggle(seg, mingain, buddies)
end
end
print("Total gain: ", round(current.GetEnergyScore() -sscore))
save.LoadSecondaryStructure ()
behavior.SetSlowFiltersDisabled(false)
end
keepTest = 0.1 --keep test points only when gain is more than
LWSonly = 0.05 --do lws only if test more than
howmany = 5 --how many best
mingain = 0.1 --minimum gain per wiggle iterations
buddies = 4 -- how many segments aside should be wiggled too
startS=1
endS=nil
--for i=1,3 do --run 3 times
LuckyLWS()
--end