Advertisement
Guest User

KOS hillclimbing attempt

a guest
May 27th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. global TX_lib_hillclimb_main is lexicon(
  2.   "BoundryFinder", BoundryFinder@,
  3.   "MinGoldSection", MinGoldSection@,
  4.   "GoldenSearch", GoldenSearch@,
  5.   "Squared", Squared@
  6. ).
  7. local TXStopper is "[]".
  8.  
  9. Function BoundryFinder {
  10.   parameter Data.
  11.   parameter ScoreSystem.
  12.   parameter StepDirection is (-1).
  13.   parameter StepSize is 0.1.
  14.  
  15.   clearscreen.
  16.  
  17.   local ScoreList is list().
  18.   local DataList is list().
  19.   ScoreList:add(Data).
  20.  
  21.   local OrginialStepSize is StepSize.
  22.   local CurrentData is 0.
  23.   local CurrentX is 0.
  24.   local NewScore is 0.
  25.  
  26.   until ScoreList:length = 3 {
  27.     set CurrentData to ScoreList[ScoreList:length -1]. //newest entry
  28.     set CurrentX to CurrentData[0].
  29.     set CurrentX to CurrentX + StepDirection * StepSize.
  30.     set StepSize to StepSize * 2.
  31.  
  32.     local DataList is list().
  33.     set NewScore to ScoreSystem(CurrentX).
  34.     DataList:add(CurrentX).
  35.     DataList:add(NewScore).
  36.     ScoreList:add(DataList).
  37.  
  38.     // newest entry's x
  39.     // x + direction * step
  40.     // step = step *2
  41.   }
  42.  
  43.   // when is the middle score the best
  44.   local PrintCounter is 0.
  45.   local BreakAttempt is 0.
  46.   until ScoreList[1][1] <= ScoreList[0][1] and ScoreList[1][1] <= ScoreList[2][1] {
  47.     local TempList is list().
  48.     for ListData in ScoreList {
  49.       TempList:add(ListData).
  50.     }
  51.     TempList:remove(0). // removed oldest entry
  52.  
  53.     local OldScoreList is list(). // makes a list of the old scorelist to compare with the new scorelist
  54.     for ListData in ScoreList {
  55.       OldScoreList:add(ListData).
  56.     }
  57.  
  58.     set CurrentData to TempList[1]. // newest entry
  59.     set CurrentX to CurrentData[0]. // x value of newest entry
  60.     set CurrentX to CurrentX + StepDirection * StepSize.
  61.     set StepSize to StepSize * 2.
  62.  
  63.     local DataList is list().
  64.     set NewScore to ScoreSystem(CurrentX).
  65.     DataList:add(CurrentX).
  66.     DataList:add(NewScore).
  67.     TempList:add(DataList).
  68.     set ScoreList to TempList.
  69.  
  70.     //set PrintCounter to PrintCounter + 1.
  71.     if PrintCounter = 5 {
  72.       print "0: new old".
  73.       print ScoreList[0][1].
  74.       print OldScoreList[0][1].
  75.       print "1: new old".
  76.       print ScoreList[1][1].
  77.       print OldScoreList[1][1].
  78.       print "2: new old".
  79.       print ScoreList[2][1].
  80.       print OldScoreList[2][1].
  81.       set PrintCounter to 0.
  82.       wait 3.
  83.     }
  84.  
  85.     if (ScoreList[0][1] > OldScoreList[0][1] and ScoreList[1][1] > OldScoreList[1][1] and ScoreList[2][1] > OldScoreList[2][1]) or (ScoreList[0][1]=ScoreList[1][1]=ScoreList[2][1]) {
  86.       if BreakAttempt = 1 {
  87.         HUDTEXT("no improvement in either way breaking", 5, 2, 30, red, false).
  88.         break.
  89.       }
  90.       set BreakAttempt to 1.
  91.       HUDTEXT("no improvement made! switching sign and resetting StepSize.", 5, 2, 30, red, false).
  92.       print "no improvement made! switching sign and resetting StepSize.".
  93.       set StepDirection to StepDirection*-1.
  94.       set StepSize to OrginialStepSize/10.
  95.       set OrginialStepSize to StepSize.
  96.     }
  97.  
  98.   }
  99.  
  100.   print "found best decent guess".
  101.   return ScoreList.
  102.  
  103. }
  104.  
  105. Function MinGoldSection {
  106.   parameter fn. // scoring function
  107.   parameter a. // lower boundry
  108.   parameter b. // upper boundry
  109.   parameter xtol. // stops when dx is this size
  110.   parameter ftol. // stops when dscore is this size
  111.  
  112.   local golden is 2 / (1 + sqrt(5)).
  113.   local cgolden is 1 - golden.
  114.   local c is golden * a + cgolden * b.
  115.   local d is cgolden * a + golden * b.
  116.  
  117.   // a---c--d---b
  118.   local fa is fn(a).
  119.   local fb is fn(b).
  120.   local fc is fn(c).
  121.   local fd is fn(d).
  122.   local dx is 0.
  123.   local df is 0.
  124.   local next is 0. // 1 if next update is C, 2 if D
  125.  
  126.   if fc < fa and fc < fd {
  127.     set dx to d - a.
  128.     set df to min(abs(fd - fc), abs(fa - fc)).
  129.     // relabel D as B, C as D
  130.     set b to d.
  131.     set fb to fd.
  132.     set d to c.
  133.     set fd to fc.
  134.     set next to 1.
  135.   }
  136.   else {
  137.     set dx to b - c.
  138.     set df to min(abs(fc - fd), abs(fb - fd)).
  139.     // relabel C as A, D as C
  140.     set a to c.
  141.     set fa to fc.
  142.     set c to d.
  143.     set fc to fd.
  144.     set next to 2.
  145.   }
  146.  
  147.   until abs(dx) < xtol or abs(df) < ftol {
  148.     if next = 1 {
  149.       set c to golden * a + cgolden * b.
  150.       set fc to fn(c).
  151.     }
  152.     else {
  153.       set d to cgolden * a + golden * b.
  154.       set fd to fn(d).
  155.     }
  156.     if fc < fa and fc < fd {
  157.       set dx to d - a.
  158.       set df to min(abs(fd - fc), abs(fa - fc)).
  159.       // relabel D as B, C as D
  160.       set b to d.
  161.       set fb to fd.
  162.       set d to c.
  163.       set fd to fc.
  164.       set next to 1.
  165.     }
  166.     else {
  167.       set dx to b - c.
  168.       set df to min(abs(fc - fd), abs(fb - fd)).
  169.       // relabel C as A, D as C
  170.       set a to c.
  171.       set fa to fc.
  172.       set c to d.
  173.       set fc to fd.
  174.       set next to 2.
  175.     }
  176.   }
  177.   // return the argument of the minimal value found so far
  178.   if next = 1 { return d. }
  179.   else { return c. }
  180. }
  181.  
  182. Function GoldenSearch {
  183.   parameter Data.
  184.   parameter ScoreSystem.
  185.   parameter StepDirection is (-1).
  186.   parameter StepSize is 0.001.
  187.   parameter xtol is 1e-10.
  188.   parameter ftol is 1e-10.
  189.  
  190.   local DecentScoreList is BoundryFinder(Data, ScoreSystem, StepDirection, StepSize).
  191.   local GoodValue is MinGoldSection(ScoreSystem, DecentScoreList[0][0], DecentScoreList[2][0], xtol, ftol).
  192.   return GoodValue.
  193. }
  194.  
  195. // test function, can be any scoring function that favors a low score
  196. Function Squared {
  197.   parameter input.
  198.   return sqrt(abs(input)).
  199. }
  200.  
  201. //
  202.  
  203. Function Score {
  204.   Parameter NodeList.
  205.   Parameter ScoreType.
  206.   Parameter ScoreList.
  207.   Parameter DeltaVCap.
  208.  
  209.   local ScoreManeuver is node(NodeList[0], NodeList[1], NodeList[2], NodeList[3]).
  210.   add   ScoreManeuver.
  211.   wait until hasnode = true.
  212.   local Result is 20^63.
  213.   local ScoreTypesAvailable is list("Circularize", "Inclination", "Apoapsis", "Periapsis", "ApoapsisMatch", "PerApoMatch", "PerPerMatch", "MoonTransfer", "Interplanetary", "FinalCorrection", "Error").
  214.   local i is 0.
  215.   local Override is false.
  216.  
  217.   until Result <> 20^63 or Override = true {
  218.     set Result to ScoreExecuter(ScoreType, ScoreTypesAvailable[i], ScoreList).
  219.     set i to i + 1.
  220.     if ScoreTypesAvailable[i] = "Error" {
  221.       set Override to true.
  222.     }
  223.   }
  224.  
  225.  
  226.  
  227.   if ScoreManeuver:deltav:mag > DeltaVCap {
  228.     set Result to 2^64.
  229.     print "surpased delta v cap              " at (1, 26).
  230.   }
  231.  
  232.   if ScoreManeuver:eta < 0 {
  233.     set Result to -1*((2^64)/ScoreManeuver:eta).
  234.     print "eta too close                     " at (1, 26).
  235.     local TimeCopy is BestCandidate[0].
  236.     BestCandidate:remove(0).
  237.     if time:seconds < TimeCopy {
  238.       BestCandidate:insert(0, TimeCopy+30).
  239.     } else {
  240.       BestCandidate:insert(0, time:seconds+30).
  241.     }
  242.  
  243.   }
  244.  
  245.   // if sub_orbital dont penalize below atmosphere
  246.   if ship:status <> "sub_orbital" {
  247.     if ship:body:atm:exists = true {
  248.       if ScoreManeuver:orbit:periapsis < ship:body:atm:height {
  249.         set Result to 2^64.
  250.         print "periapsis under atm            " at(1,26).
  251.       }
  252.     }
  253.  
  254.     if ScoreManeuver:orbit:periapsis < 10000 {
  255.       set Result to 2^64.
  256.       print "periapsis too low (highest point of planet is >0 m)     " at(1,26).
  257.     }
  258.  
  259.     if Result < 0 {
  260.       set Result to 2^64.
  261.       print "result under 0                            " at(1,26).
  262.     }
  263.   }
  264.  
  265.   remove ScoreManeuver.
  266.   return Result.
  267. }
  268.  
  269. Function Improve {
  270.   parameter NodeList.     // (time:seconds + 30, 0, 0, 23)
  271.   parameter ScoreList.    // (kerbin, kerbin)
  272.   parameter ScoreType.    // circularization
  273.   parameter Restriction.  // "realnormal_prograde"
  274.  
  275.   // give nodelist ie. (time:seconds + eta:apoapsis, 0, 0, 10)
  276.   // edit per entry
  277.   local RestrictionTypes is list(
  278.     "timeplus", "timemin", "radialin", "radialout", "realnormal", "antinormal", "prograde", "retrograde"
  279.   ).
  280.  
  281.   local NonRestrictedList is list().
  282.   local RestrictionStepper is 0.
  283.  
  284.   until RestrictionStepper = 7 {
  285.  
  286.     if not Restriction:contains(RestrictionTypes[RestrictionStepper]) {
  287.       NonRestrictedList:add(RestrictionTypes[RestrictionStepper]).
  288.     }
  289.     set RestrictionStepper to RestrictionStepper + 1.
  290.   }
  291.   // all wanted variables are now in NonRestrictedList
  292.  
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement