TDDC17AICourse/lab5/StateAndReward.java
2021-08-22 13:07:04 +02:00

87 lines
2.8 KiB
Java

public class StateAndReward {
/* State discretization function for the angle controller */
public static String getStateAngle(double angle, double vx, double vy) {
return " " + discretize2(angle, 20, 2*-Math.PI/3, 2*Math.PI/3) + " ";
}
/* Reward function for the angle controller */
public static double getRewardAngle(double angle, double vx, double vy) {
return Math.PI - Math.abs(angle);
}
/* State discretization function for the full hover controller */
public static String getStateHover(double angle, double vx, double vy) {
return discretize2(angle, 10, 2*-Math.PI/3, 2*Math.PI/3)
+ " / " + discretize(vx, 3, -1, 1)
+ " / " + discretize(vy, 11, -1, 1);
}
/* Reward function for the full hover controller */
public static double getRewardHover(double angle, double vx, double vy) {
return (Math.PI - Math.abs(angle))/2 + (10 - Math.abs(Math.max(Math.abs(vx), Math.abs(vy)))) / 5;
}
// ///////////////////////////////////////////////////////////
// discretize() performs a uniform discretization of the
// value parameter.
// It returns an integer between 0 and nrValues-1.
// The min and max parameters are used to specify the interval
// for the discretization.
// If the value is lower than min, 0 is returned
// If the value is higher than min, nrValues-1 is returned
// otherwise a value between 1 and nrValues-2 is returned.
//
// Use discretize2() if you want a discretization method that does
// not handle values lower than min and higher than max.
// ///////////////////////////////////////////////////////////
public static int discretize(double value, int nrValues, double min,
double max) {
if (nrValues < 2) {
return 0;
}
double diff = max - min;
if (value < min) {
return 0;
}
if (value > max) {
return nrValues - 1;
}
double tempValue = value - min;
double ratio = tempValue / diff;
return (int) (ratio * (nrValues - 2)) + 1;
}
// ///////////////////////////////////////////////////////////
// discretize2() performs a uniform discretization of the
// value parameter.
// It returns an integer between 0 and nrValues-1.
// The min and max parameters are used to specify the interval
// for the discretization.
// If the value is lower than min, 0 is returned
// If the value is higher than min, nrValues-1 is returned
// otherwise a value between 0 and nrValues-1 is returned.
// ///////////////////////////////////////////////////////////
public static int discretize2(double value, int nrValues, double min,
double max) {
double diff = max - min;
if (value < min) {
return 0;
}
if (value > max) {
return nrValues - 1;
}
double tempValue = value - min;
double ratio = tempValue / diff;
return (int) (ratio * nrValues);
}
}