RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.critterbot.examples; 00002 00003 import java.awt.Color; 00004 00005 import rlpark.plugin.critterbot.data.CritterbotLabels; 00006 import rlpark.plugin.critterbot.environment.CritterbotEnvironment; 00007 import rlpark.plugin.critterbot.environment.CritterbotRobot; 00008 00009 import zephyr.plugin.core.api.synchronization.Clock; 00010 00011 public class ColoredAgent implements Runnable { 00012 final Color[] colors = new Color[CritterbotLabels.NbLeds]; 00013 static int ledOffset = 0; 00014 static double currentCount = 0.0; 00015 static int time = 0; 00016 private final CritterbotEnvironment environment; 00017 private final Clock clock = new Clock("Agent"); 00018 00019 public ColoredAgent(CritterbotEnvironment environment) { 00020 this.environment = environment; 00021 } 00022 00023 private Color[] computeLeds() { 00024 return computeLeds(colors); 00025 } 00026 00027 static public Color[] computeLeds(Color[] colors) { 00028 currentCount += 0.01; 00029 time++; 00030 if (time % 8 == 0) 00031 ledOffset += 1; 00032 Color[] colorsSuite = { Color.BLUE, Color.RED, Color.GREEN }; 00033 for (int i = 0; i < colors.length; i++) { 00034 int ledIndex = (i + ledOffset) % colors.length; 00035 colors[ledIndex] = computeColor(colorsSuite, currentCount + i); 00036 } 00037 return colors; 00038 } 00039 00040 static private Color computeColor(Color[] colorsSuite, double countColor) { 00041 double floor = Math.floor(countColor); 00042 double ceil = Math.ceil(countColor); 00043 double ratio = countColor - floor; 00044 Color floorColor = colorsSuite[(int) floor % colorsSuite.length]; 00045 Color ceilColor = colorsSuite[(int) ceil % colorsSuite.length]; 00046 return new Color((int) (floorColor.getRed() * (1 - ratio) + ceilColor.getRed() * ratio), 00047 (int) (floorColor.getGreen() * (1 - ratio) + ceilColor.getGreen() * ratio), 00048 (int) (floorColor.getBlue() * (1 - ratio) + ceilColor.getBlue() * ratio)); 00049 } 00050 00051 @Override 00052 public void run() { 00053 while (clock.tick() && !environment.isClosed()) { 00054 environment.setLed(computeLeds()); 00055 } 00056 } 00057 00058 public static void main(String[] args) { 00059 CritterbotEnvironment environment = new CritterbotRobot(); 00060 new ColoredAgent(environment).run(); 00061 } 00062 }