Sharing of my labs carried out during the TDDC17 course at Linköping University.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StateAndReward.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. public class StateAndReward {
  2. /* State discretization function for the angle controller */
  3. public static String getStateAngle(double angle, double vx, double vy) {
  4. return " " + discretize2(angle, 20, 2*-Math.PI/3, 2*Math.PI/3) + " ";
  5. }
  6. /* Reward function for the angle controller */
  7. public static double getRewardAngle(double angle, double vx, double vy) {
  8. return Math.PI - Math.abs(angle);
  9. }
  10. /* State discretization function for the full hover controller */
  11. public static String getStateHover(double angle, double vx, double vy) {
  12. return discretize2(angle, 10, 2*-Math.PI/3, 2*Math.PI/3)
  13. + " / " + discretize(vx, 3, -1, 1)
  14. + " / " + discretize(vy, 11, -1, 1);
  15. }
  16. /* Reward function for the full hover controller */
  17. public static double getRewardHover(double angle, double vx, double vy) {
  18. return (Math.PI - Math.abs(angle))/2 + (10 - Math.abs(Math.max(Math.abs(vx), Math.abs(vy)))) / 5;
  19. }
  20. // ///////////////////////////////////////////////////////////
  21. // discretize() performs a uniform discretization of the
  22. // value parameter.
  23. // It returns an integer between 0 and nrValues-1.
  24. // The min and max parameters are used to specify the interval
  25. // for the discretization.
  26. // If the value is lower than min, 0 is returned
  27. // If the value is higher than min, nrValues-1 is returned
  28. // otherwise a value between 1 and nrValues-2 is returned.
  29. //
  30. // Use discretize2() if you want a discretization method that does
  31. // not handle values lower than min and higher than max.
  32. // ///////////////////////////////////////////////////////////
  33. public static int discretize(double value, int nrValues, double min,
  34. double max) {
  35. if (nrValues < 2) {
  36. return 0;
  37. }
  38. double diff = max - min;
  39. if (value < min) {
  40. return 0;
  41. }
  42. if (value > max) {
  43. return nrValues - 1;
  44. }
  45. double tempValue = value - min;
  46. double ratio = tempValue / diff;
  47. return (int) (ratio * (nrValues - 2)) + 1;
  48. }
  49. // ///////////////////////////////////////////////////////////
  50. // discretize2() performs a uniform discretization of the
  51. // value parameter.
  52. // It returns an integer between 0 and nrValues-1.
  53. // The min and max parameters are used to specify the interval
  54. // for the discretization.
  55. // If the value is lower than min, 0 is returned
  56. // If the value is higher than min, nrValues-1 is returned
  57. // otherwise a value between 0 and nrValues-1 is returned.
  58. // ///////////////////////////////////////////////////////////
  59. public static int discretize2(double value, int nrValues, double min,
  60. double max) {
  61. double diff = max - min;
  62. if (value < min) {
  63. return 0;
  64. }
  65. if (value > max) {
  66. return nrValues - 1;
  67. }
  68. double tempValue = value - min;
  69. double ratio = tempValue / diff;
  70. return (int) (ratio * nrValues);
  71. }
  72. }