Skip to content

Instantly share code, notes, and snippets.

@Rav3nPL
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rav3nPL/562ea3960494e9c02e7d to your computer and use it in GitHub Desktop.
Save Rav3nPL/562ea3960494e9c02e7d to your computer and use it in GitHub Desktop.
Foldit Mutate Combo v2.1
--[[
Mutate Combo v2 by rav3n_pl
2 ways of mutate:
random:
- randomly choose sphere around mutable segment and mutate in random CI
brute force:
- check all possible aas for every mutable sgmnt
]]--
-- script options there VVVVVVVV
normal=true --set false for exploration puzzles if want to explore
autoLooops=3
bruteLoops=10 --maximum brute force loops, it breaks if no change on full loop
--auto loops are first, then brute. put 0 in proper place if want only auto or brute
mutateSphere={4,7} --how big sphere around segment should be mutated: random{min,max}
shakeoutSphere=5 --how much further shake after mutate
worst=true --start mutating from worst scoring segment. if false it randomize order
chains={"G";"A";"V";"C";"P";"T";"S";"I";"L";"N";"D";"M";"H";"Q";"E";"F";"K";"Y";"R";"W"}--all of them
--chains={"G";"A";"V";"C";"P";"T";"S";"I";"L";"N";"M";"Q";"F";"Y";"W"} --desired for TNT: no ASP, GLU, ARG, LYS, HIS
maxCI=1 --maximum Clash Importance, safe from 0.1 to 1.0, under 0.1 universe might implode!
CapCI=false --when false= always use maxci as multiplier, true=just cap it to maxci when higher
filters=true --if true filters are enabled only for scoring
-- end of options ^^^^^^^^^^
p = print --a short
segCnt = structure.GetCount() --always the same
while structure.GetSecondaryStructure(segCnt)=="M" do segCnt=segCnt-1 end --ignore ligands
function CI(c)
if CapCI==true then
if c>maxCI then
c=maxCI
end
else
c=c*maxCI
end
behavior.SetClashImportance(c)
end
function Score()
if filters then
behavior.SetSlowFiltersDisabled(false)
end
local s=0
if normal then
s=current.GetEnergyScore()
else
s=current.GetScore()
end
if filters then
behavior.SetSlowFiltersDisabled(true)
end
return s
end
function down(x)--cut all after comma
return x-x%1
end
function round(x)--cut all afer 3-rd place
return x-x%0.001
end
--local seed=recipe.GetRandomSeed() --NOT WORKING on windowz!!!
--calculate REALLY good seed from current score
seed=os.time()
seed=1/seed
while seed<10000000 do seed=seed*10 end
seed=seed-seed%1
p("Seed is: "..seed)
math.randomseed(seed)
mutable={} --table to store mutable sgmnts
function RandomInt(high) -- A random int between [1, high]
return math.random(high)
end
function Random(tab) -- random from range
return math.random(tab[1],tab[2])
end
function ShuffleTable(tab) --randomize order of elements
local cnt=#tab
for i=1,cnt do
local r=RandomInt(cnt)
tab[i],tab[r]=tab[r],tab[i]
end
return tab
end
function Wiggle(how, iters, minppi) --score conditioned recursive wiggle/shake
if how==nil then how="wa" end
if iters==nil then iters=3 end
if minppi==nil then minppi=0.1 end
if iters>0 then
iters=iters-1
local sp=Score()
if how == "s" then structure.ShakeSidechainsSelected(1)
elseif how == "wb" then structure.WiggleAll(1, true,false)
elseif how == "ws" then structure.WiggleAll(1,false,true)
elseif how == "wa" then structure.WiggleAll(1)
end
local ep = Score()
local ig=ep-sp
if ig > minppi then return Wiggle(how, iters, minppi) end --to learn recursion you have to learn recursion ;]
end
end
function mut(ci)
local done=false
CI(ci)
local i=1
repeat
local s=Score()
structure.MutateSidechainsSelected(i)
if Score()~=s then done=true
else i=i+1 end
if i>2 then done=true end
until done
CI(1)
end
function Aftermut(sg)
CI(0.1)
Wiggle("s",1)
selection.SelectAll()
CI(1)
Wiggle()
if sg~=nil then
SelectSphere(sg,12)
end
Wiggle("s",1)
selection.SelectAll()
Wiggle()
selection.DeselectAll()
end
function FindMutable()
p("Finding Mutable Segments...")
local mutable={}
for i=1,segCnt do
if structure.IsMutable(i) then
mutable[#mutable + 1] = i
end
end
p(#mutable.." mutables found")
return mutable
end
function SelectSphere(sg, radius)
selection.DeselectAll()
for i=1, segCnt do
if structure.GetDistance(sg,i)<radius then selection.Select(i) end
end
end
bestScore=Score()
function SaveBest()
local g=Score()-bestScore
if g>0.01 then
p("Gained another "..round(g).." pts.")
bestScore=Score()
save.Quicksave(3)
end
end
function mutate(i,small,large,ci)
sc=Score()
SelectSphere(i, small)
mut(ci)
if math.abs(Score()-sc)>1 then
SelectSphere(i, large)
Aftermut(i)
else
selection.SelectAll()
Wiggle()
end
local g=Score()-sc
if g>0 then
SaveBest()
elseif g<0 then
save.Quickload(3)
end
end
function sortMutable(tab)
t={}
for i=1,#tab do
t[#t+1]={tab[i],current.GetSegmentEnergyScore(tab[i])}
end
for i=1,#t do
for j=i+1,#t do
if t[i][2]>t[j][2] then
t[i],t[j]=t[j],t[i]
end
end
end
tab={}
for i=1,#t do
tab[#tab+1]=t[i][1]
end
return tab
end
function MutateRandom(loops)
local ss=Score()
selection.DeselectAll()
recentbest.Save()
save.Quicksave(3)
for i=1,loops do
local ls=Score()
if worst==true then
mutable=sortMutable(mutable) --worst first
else
mutable=ShuffleTable(mutable) --random
end
local cnt=#mutable
for s=1,cnt do
sg=mutable[s]--segment
ci=RandomInt(101)--clash importance
ci=ci/100
if ci>1 then ci=1 end --not over 1
sm=Random(mutateSphere) --small sphere for mutate
sb=sm+shakeoutSphere --big sphere for shakeout
p("Loop "..i.." of "..loops.."; sg "..s.." of "..cnt.."; score: "..round(Score()))
p("In segment "..sg.." spheres "..sm.."-"..sb.." on CI: "..ci)
mutate(sg,sm,sb,ci)
end
p("Loop gain: "..round(Score()-ls))
end
p("Total random mutate gain: "..round(Score()-ss))
end
function Brute(sg)
local sco=Score()
selection.DeselectAll()
p("Mutating segment "..sg)
save.Quicksave(4)
local aa1=structure.GetAminoAcid(sg)
for i=1,#chains do
local c1=Score()
selection.DeselectAll()
selection.Select(sg)
structure.SetAminoAcidSelected(chains[i])
SelectSphere(sg,9)
Aftermut(sg)
if Score()<c1 then
save.Quickload(4)
else
save.Quicksave(4)
end
end
save.Quickload(4)
local aa2=structure.GetAminoAcid(sg)
local m=false
if aa1==aa2 then
p("Not changed.")
else
p("Segment mutated.")
m=true
end
SaveBest()
return m
end
function MutateBrute(loops)
CI(1)
local ss=Score()
for l=1, loops do
local ls=Score()
p("Mutate loop started at score: "..round(Score()))
if worst==true then
mutable=sortMutable(mutable) --worst first
else
mutable=ShuffleTable(mutable) --random
end
local muted=false
for i=1,#mutable do
local m=Brute(mutable[i])
if m==true then muted=true end
p("Loop "..l.." "..#mutable-i.." left. Current score: "..round(Score()))
end
p("Loop gain: "..round(Score()-ls))
if muted==false then
p("Not muted any single one! Breaking!")
break
end
end
p("Brute force mutate completed. Gain: "..round(Score()-ss))
end
-- main call
if filters then behavior.SetSlowFiltersDisabled(true) end --disable filters
mutable=FindMutable() --find mutable segments
MutateRandom(autoLooops) --all auto
MutateBrute(bruteLoops) --all brute force
if filters then behavior.SetSlowFiltersDisabled(false) end --enable filters
--end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment