Icon representing a recipe

Recipe: Optimizer v0.5

created by cjddig

Profile


Name
Optimizer v0.5
ID
104481
Shared with
Public
Parent
None
Children
Created on
February 17, 2021 at 07:44 AM UTC
Updated on
February 17, 2021 at 07:44 AM UTC
Description

Select 3(first)/5(second) residues, rebuild, remix, local wiggle, cut/idealize, local wiggle, and global wiggle.
My first recipe, so it may have many bug. Need feedback.
Only useful for some design puzzles that have few objectives and allow Rebuild tool.

Best for


Code


function main() local segmentList = {} WP = behavior.GetWigglePower() local ask = dialog.CreateDialog("Test Recipe") ask.DisableFilters = dialog.AddCheckbox("Disable Filters", true) ask.WorstFirst = dialog.AddCheckbox("Worst First", true) ask.OK = dialog.AddButton("OK", 1) ask.Cancel = dialog.AddButton("Cancel", 0) if (behavior.HighPowerAllowed()) then behavior.SetWigglePower("h") else behavior.SetWigglePower("m") end for i = 1, structure.GetCount() do if (not structure.IsLocked(i)) then table.insert(segmentList, {['segment'] = i, ['score'] = current.GetSegmentEnergyScore(i)}) end end creditbest.Restore() filter.EnableAll() save.Quicksave(100) FirstScore = current.GetScore() CurrentScore = current.GetScore() print("Starting Score: " .. FirstScore) function Idealize(num) for i = 1, structure.GetCount() do order = i save.Quickload(100) if (ask.DisableFilters.value == true) then filter.DisableAll() end startSegment = segmentList[i]['segment'] - num endSegment = segmentList[i]['segment'] + num if (startSegment < 1) then startSegment = 1 end if (endSegment >= structure.GetCount()) then endSegment = structure.GetCount() end print("Working on Segment " .. startSegment .. " - " .. endSegment) selection.SelectRange(startSegment, endSegment) structure.RebuildSelected(1) val = structure.RemixSelected(1, 99) selection.DeselectAll() for i = 1, val do if (i % 10 == 1 and i ~= 11) then print("Loading " .. i .. "st Remix") elseif (i % 10 == 2 and i ~= 12) then print("Loading " .. i .. "nd Remix") elseif (i % 10 == 3 and i ~= 13) then print("Loading " .. i .. "rd Remix") else print("Loading " .. i .. "th Remix") end if (ask.DisableFilters.value == true) then filter.DisableAll() end save.Quickload(i) structure.WiggleAll(13) selection.DeselectAll() filter.EnableAll() print("Primary score: " .. current.GetScore()) if current.GetScore() > CurrentScore then print("Gained " .. current.GetScore() - CurrentScore .. "point(s)") CurrentScore = current.GetScore() end if (segmentList[order]['segment'] - 1 > 0) then structure.InsertCut(segmentList[order]['segment'] - 1) end if (segmentList[order]['segment'] + 1 < structure.GetCount()) then structure.InsertCut(segmentList[order]['segment'] + 1) end selection.Select(segmentList[order]['segment']) structure.IdealizeSelected() selection.Deselect(segmentList[order]['segment']) if (segmentList[order]['segment'] - 1 > 0) then structure.DeleteCut(segmentList[order]['segment'] - 1) end if (segmentList[order]['segment'] + 1 < structure.GetCount()) then structure.DeleteCut(segmentList[order]['segment'] + 1) end for j = 1, structure.GetCount() do if (structure.GetDistance(segmentList[order]['segment'], j) < 10) then selection.Select(j) end end structure.WiggleSelected(13) selection.DeselectAll() structure.WiggleAll(13) filter.EnableAll() print("Secondary score: " .. current.GetScore()) if current.GetScore() > CurrentScore then print("Gained " .. current.GetScore() - CurrentScore .. " point(s)") CurrentScore = current.GetScore() save.Quicksave(100) end end end end if (dialog.Show(ask) > 0) then if (ask.WorstFirst.value == true) then table.sort(segmentList, function(a, b) return a.score < b.score end) end Idealize(1) Idealize(2) save.Quickload(100) cleanup() else print("Canceled") cleanup() end end function cleanup(error) local reason local start, stop, line, msg if error == nil then reason = "complete" else start, stop, line, msg = error:find(":(%d+):%s()") if msg ~= nil then error = error:sub(msg, #error) end if error:find("Cancelled") ~= nil then reason = "cancelled" else reason = "error" end end if reason == "cancelled" then print("Recipe cancelled") elseif reason == "error" then print("Unexpected error detected") print("Error line: " .. line) print("Error: \"" .. error .. "\"") print("Please copy this message and connect cjddig.") print("(or you can modify this recipe.)") end behavior.SetClashImportance(1) selection.SelectAll() if error ~= nil then save.Quickload(100) end filter.EnableAll() structure.SetSecondaryStructureSelected("A") selection.DeselectAll() behavior.SetWigglePower(WP) print("Restored to best score") print("Final Score: " .. current.GetScore()) end xpcall(main, cleanup)

Comments


TurtleByte Lv 1

Hey cjddig, just a quick couple of suggestions I have after running this for the first time.

1) If you're going to change the wiggle power, give the user the option of what to change it to. I was working on low wiggle power and was caught off guard when the script changed it to high without warning.

2) Also have an option for the user to select whether or not they want to work with their current pose or credit best. Due to number 1 my credit best pose was not what I wanted to work with, so even after deleting the wiggle power code and re-running, I ended up working with the incorrect pose again.

3) I'd recommend saving the original pose in one slot and the best scoring pose in a different slot. Then never overwrite the original pose's slot, I may want to go back to compare my original pose to the ending pose.

4) Add some dialog options for wiggle iterations.

5) More of a personal preference but I would take a look at breaking your code up into smaller functions. Refactor out distinct blocks of code that do 1 thing. For example, the dialog or when you compare the score or when you're printing, those could be a separate functions. This also makes it easier to read and understand your code.

Cheers,
TurtleByte

cjddig Lv 1

I'm currently making v0.7, and I'll apply your suggestions. Thanks again for the suggestions!