In: Computer Science
public static int punishOrMercy(char direction, int
reward)
Output: int which will be the value of zero or the initial
reward.
Functionality: After the result of the reward function is stored,
this function should be called. The goal of this function is to
help the robot in case it faced a lot of damage in the current
step. However, this function will help the robot only if the
robot’s reward was negative and the direction that the user
inputted was up. This is common practice in Artificial Intelligence
(AI) to reward more or penalize less to create incentive to move
towards a certain direction, in our case, up.
What does it do (a more detailed explanation): It will first check
the sign of the reward value, if it was negative, then it will
check if the direction is equal to ‘u’, if that is the case, then
it will flip a coin and for zero (tail) it will return zero, and
for one (head) it will return the initial reward (the same reward
that the function received as an input).
When a coin is generated, this function also prints one of the
following messages:
“Coin: tail | Mercy, the negative reward is removed.”
“Coin: head | No mercy, the negative rewarded is
applied.”
(in java)
*note (do just complete this specific method as this is just one of the methods apart of this.)
In case of any query do comment. Please rate answer as well. Thanks
You can use below code for your method. You have to import to toss a coin to get either 0 or 1 for tail and head simulation.
import java.util.Random;
Code:
public static int punishOrMercy(char direction, int reward){
if (reward < 0 && direction == 'u'){
//flip a coin here
Random rnd = new Random();
int coin = rnd.nextInt(2);
if (coin == 0) {
//if tail, print the message and return 0
System.out.println("Coin: tail | Mercy, the negative reward is removed.");
return 0;
}
else{
//if head then return the same reward
System.out.println("Coin: head | No mercy, the negative rewarded is applied.");
return reward;
}
}
//if your direction is not u or reward is not negative then return the same reward
return reward;
}
Output:
Full Sample Code for above output:
import java.util.Random;
public class Reward
{
public static void main(String[] args) {
System.out.println(punishOrMercy('u',-1));
System.out.println(punishOrMercy('u',-1));
System.out.println(punishOrMercy('u',2));
System.out.println(punishOrMercy('d',-1));
}
public static int punishOrMercy(char direction, int reward){
if (reward < 0 && direction == 'u'){
//flip a coin here
Random rnd = new Random();
int coin = rnd.nextInt(2);
if (coin == 0) {
//if tail, print the message and return 0
System.out.println("Coin: tail | Mercy, the negative reward is removed.");
return 0;
}
else{
//if head then return the same reward
System.out.println("Coin: head | No mercy, the negative rewarded is applied.");
return reward;
}
}
//if your direction is not u or reward is not negative then return the same reward
return reward;
}
}