Icon representing a recipe

Recipe: AFK3.1(BounceWiggle)

created by neon_fuzz

Profile


Name
AFK3.1(BounceWiggle)
ID
103196
Shared with
Public
Parent
AFK3(BounceWiggle)
Children
Created on
November 30, 2019 at 17:24 PM UTC
Updated on
November 30, 2019 at 17:24 PM UTC
Description

Random Clashing Importance Wiggles/Shakes. Useful when you don't feel like CI wiggling by hand.

Updates: Better CI=1 adjustment at beginning, only one loop when mutating all, random number generators tweaked, integer sliders are now... integers, spelling improvements

Best for


Code


--------------- -- Variables -- --------------- AFK = {} AFK.SaveSlot = 98 AFK.StartScore = current.GetEnergyScore() AFK.StartCI = behavior.GetClashImportance() AFK.IsMutable = false AFK.BounceWiggle = {} AFK.Init = {} AFK.Helper = {} AFK.BounceWiggle.DoShake = false AFK.BounceWiggle.DoMutate = false AFK.BounceWiggle.Iterations = 50 AFK.BounceWiggle.MinGain = 0.1 AFK.BounceWiggle.SkipCIMaximization = true ---------------------- -- Helper Functions -- ---------------------- AFK.Helper.IsMutable = function() for n=1, structure.GetCount() do if (structure.IsMutable(n)) then AFK.IsMutable = true end end end AFK.Helper.PrintStart = function(choice) if (choice > 2) then mode = "Mutate" AFK.BounceWiggle.DoMutate = true elseif (choice > 1) then mode = "Shake" AFK.BounceWiggle.DoShake = true else mode = "NoShake" end print("AFK3(BounceWiggle"..mode..") started. ".. AFK.BounceWiggle.Iterations.. " Failed iterations before ending.") end AFK.Helper.PrintEnd = function(gain) if (gain > 0) then print ("script complete: + ".. (current.GetEnergyScore() - AFK.StartScore).. " -- "..current.GetEnergyScore()) else print("script complete:"..current.GetEnergyScore()) end end AFK.Helper.SetCI = function(ci) ci = ci or math.random(50, 900) / 1000 behavior.SetClashImportance(ci) end AFK.Helper.SelectRandom = function() local shakeSelectionCount = math.random(1, structure.GetCount()) for n=1, shakeSelectionCount do local selectSegment = math.random(1, structure.GetCount()) selection.Select(selectSegment) end end AFK.Helper.PerformFunction = function(func, iters) local currentScore = current.GetEnergyScore() local newScore = currentScore func(iters) recentbest.Restore() newScore = current.GetEnergyScore() if (newScore > (currentScore + AFK.BounceWiggle.MinGain)) then currentScore = newScore save.Quicksave(AFK.SaveSlot) else save.Quickload(AFK.SaveSlot) end end AFK.Helper.UpdateIterations = function(gain) -- TODO: option to print, even on failure. (so we know Iterations) if gain > 0 then print (AFK.BounceWiggle.Iterations..": + ".. gain.." -- "..current.GetEnergyScore()) elseif (AFK.BounceWiggle.Iterations > 0) then AFK.BounceWiggle.Iterations = AFK.BounceWiggle.Iterations - 1 end end ----------------------- -- Create Dialog Box -- ----------------------- AFK.CreateBounceWiggleDialog = function() local currentDialog = dialog.CreateDialog("BounceWiggle") currentDialog.IterationsLabel = dialog.AddLabel( "Failed Iterations before ending") currentDialog.IterationsSlider = dialog.AddSlider( "Failure Iterations", AFK.BounceWiggle.Iterations, -1, 1000, 0) currentDialog.BlankLabel1 = dialog.AddLabel("") currentDialog.DiscardLabel = dialog.AddLabel( "(Sketchbook) Discard gains less than") currentDialog.DiscardSlider = dialog.AddSlider( "Discard <", AFK.BounceWiggle.MinGain, 0, 10, 1) currentDialog.BlankLabel2 = dialog.AddLabel("") currentDialog.SkipMaximization = dialog.AddCheckbox( "Skip CI=1 Maximization", AFK.BounceWiggle.SkipCIMaximization) currentDialog.NoShakeButton = dialog.AddButton("Wiggle Only", 1) currentDialog.ShakeButton = dialog.AddButton("Shake", 2) if (AFK.IsMutable) then currentDialog.MutateButton = dialog.AddButton("Mutate", 3) end currentDialog.CancelButton = dialog.AddButton("Cancel", 0) local choice = dialog.Show(currentDialog) AFK.BounceWiggle.Iterations = currentDialog.IterationsSlider.value AFK.BounceWiggle.MinGain = currentDialog.DiscardSlider.value AFK.BounceWiggle.SkipCIMaximization = currentDialog.SkipMaximization.value if (AFK.BounceWiggle.Iterations < 1) then AFK.BounceWiggle.Iterations = -1 end if (choice > 0) then AFK.Helper.PrintStart(choice) end return choice end ------------------- -- The main dish -- ------------------- AFK.BounceWiggle.Main = function() AFK.Helper.IsMutable() local startScore = AFK.StartScore local choice = AFK.CreateBounceWiggleDialog() if (choice < 1) then print("Dialog cancelled.") return end save.Quicksave(AFK.SaveSlot) recentbest.Save() if (AFK.BounceWiggle.SkipCIMaximization == false) then AFK.Helper.PerformFunction(AFK.BounceWiggle.CIMaximization) startScore = current.GetEnergyScore() end print("Script started: "..startScore) while (AFK.BounceWiggle.Iterations ~= 0) do startScore = current.GetEnergyScore() AFK.Helper.PerformFunction(AFK.BounceWiggle.BounceWiggle) AFK.Helper.UpdateIterations(current.GetEnergyScore() - startScore) end AFK.Helper.PrintEnd(current.GetEnergyScore() - startScore) AFK.Cleanup() end ------------------------- -- The BounceWiggliest -- ------------------------- AFK.BounceWiggle.CIMaximization = function() local currentScore = AFK.StartScore local newScore = currentScore + AFK.BounceWiggle.MinGain + 0.01 print("Maximizing Wiggle Score at Clashing Importance = 1: "..currentScore) AFK.Helper.SetCI(1) while (currentScore + AFK.BounceWiggle.MinGain < newScore) do structure.WiggleAll(25) structure.LocalWiggleAll(25) recentbest.Restore() currentScore = newScore newScore = current.GetEnergyScore() end if (newScore > AFK.StartScore) then print ("CI Maximization: + "..(currentScore - AFK.StartScore).. " -- "..currentScore) end end AFK.BounceWiggle.WiggleAll = function(ci, wiggleIterations) AFK.Helper.SetCI(ci) wiggleIterations = wiggleIterations or math.random(1, 3) local wiggleType = math.random(1, 2) if (wiggleType > 1) then structure.WiggleAll(wiggleIterations) else structure.LocalWiggleAll(wiggleIterations) end end AFK.BounceWiggle.ShakeityShake = function() local shakeType = math.random(1, 3) local shakeIterations = math.random(1, 3) AFK.Helper.SetCI() -- 1/3 chance of whole protein if (shakeType > 2) then if (AFK.BounceWiggle.DoMutate == true) then -- When mutating all, we only do one iteration. -- This is to increase BounceWiggle speed, to explore more -- configurations faster. structure.MutateSidechainsAll(1) else structure.ShakeSidechainsAll(shakeIterations) end -- 2/3 chance of random selection else AFK.Helper.SelectRandom() if (AFK.BounceWiggle.DoMutate == true) then structure.MutateSidechainsSelected(shakeIterations) else structure.ShakeSidechainsSelected(shakeIterations) end selection.DeselectAll() end end AFK.BounceWiggle.BounceWiggle = function() AFK.BounceWiggle.WiggleAll() if (AFK.BounceWiggle.DoShake == true or AFK.BounceWiggle.DoMutate == true) then AFK.BounceWiggle.ShakeityShake() end AFK.BounceWiggle.WiggleAll(1, 25) end ------------- -- The end -- ------------- function AFK.Cleanup(errorMessage) behavior.SetClashImportance(AFK.StartCI) recentbest.Restore() selection.DeselectAll() end xpcall(AFK.BounceWiggle.Main, AFK.Cleanup)

Comments