|
| 1 | +/* ***************************************************************************** |
| 2 | + * Copyright (c) 2015-2019 Skymind, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | + * License for the specific language governing permissions and limitations |
| 12 | + * under the License. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: Apache-2.0 |
| 15 | + ******************************************************************************/ |
| 16 | + |
| 17 | +import org.deeplearning4j.rl4j.learning.sync.qlearning.QLearning; |
| 18 | +import org.deeplearning4j.rl4j.learning.sync.qlearning.discrete.QLearningDiscreteDense; |
| 19 | +import org.deeplearning4j.rl4j.mdp.gym.GymEnv; |
| 20 | +import org.deeplearning4j.rl4j.network.dqn.DQNFactoryStdDense; |
| 21 | +import org.deeplearning4j.rl4j.policy.DQNPolicy; |
| 22 | +import org.deeplearning4j.rl4j.space.Box; |
| 23 | +import org.nd4j.linalg.learning.config.Adam; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.logging.Logger; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author rubenfiszel ([email protected]) on 8/11/16. |
| 30 | + * |
| 31 | + * Main example for Cartpole DQN |
| 32 | + */ |
| 33 | +public class Cartpole |
| 34 | +{ |
| 35 | + private static QLearning.QLConfiguration CARTPOLE_QL = |
| 36 | + new QLearning.QLConfiguration( |
| 37 | + 123, //Random seed |
| 38 | + 200, //Max step By epoch |
| 39 | + 150000, //Max step |
| 40 | + 150000, //Max size of experience replay |
| 41 | + 32, //size of batches |
| 42 | + 500, //target update (hard) |
| 43 | + 10, //num step noop warmup |
| 44 | + 0.01, //reward scaling |
| 45 | + 0.99, //gamma |
| 46 | + 1.0, //td-error clipping |
| 47 | + 0.1f, //min epsilon |
| 48 | + 1000, //num step for eps greedy anneal |
| 49 | + true //double DQN |
| 50 | + ); |
| 51 | + |
| 52 | + private static DQNFactoryStdDense.Configuration CARTPOLE_NET = |
| 53 | + DQNFactoryStdDense.Configuration.builder() |
| 54 | + .l2(0.001).updater(new Adam(0.0005)).numHiddenNodes(16).numLayer(3).build(); |
| 55 | + |
| 56 | + public static void main(String[] args) throws IOException { |
| 57 | + DQNPolicy<Box> pol = cartPole(); |
| 58 | + loadCartpole(pol); |
| 59 | + } |
| 60 | + |
| 61 | + private static DQNPolicy<Box> cartPole() throws IOException { |
| 62 | + //define the mdp from gym (name, render) |
| 63 | + GymEnv<Box, Integer, org.deeplearning4j.rl4j.space.DiscreteSpace> mdp = new GymEnv<Box, Integer, org.deeplearning4j.rl4j.space.DiscreteSpace>("CartPole-v0", false, false); |
| 64 | + QLearningDiscreteDense<Box> dql = new QLearningDiscreteDense<Box>(mdp, CARTPOLE_NET, CARTPOLE_QL); |
| 65 | + |
| 66 | + dql.train(); |
| 67 | + mdp.close(); |
| 68 | + |
| 69 | + return dql.getPolicy(); //get the final policy |
| 70 | + } |
| 71 | + |
| 72 | + private static void loadCartpole(DQNPolicy<Box> pol) throws IOException { |
| 73 | + //use the trained agent on a new similar mdp (but render it this time) |
| 74 | + |
| 75 | + //define the mdp from gym (name, render) |
| 76 | + GymEnv<Box, Integer, org.deeplearning4j.rl4j.space.ActionSpace<Integer>> mdp2 = new GymEnv<Box, Integer, org.deeplearning4j.rl4j.space.ActionSpace<Integer>>("CartPole-v0", true, false); |
| 77 | + |
| 78 | + //evaluate the agent |
| 79 | + double rewards = 0; |
| 80 | + for (int i = 0; i < 1000; i++) { |
| 81 | + mdp2.reset(); |
| 82 | + double reward = pol.play(mdp2); |
| 83 | + rewards += reward; |
| 84 | + Logger.getAnonymousLogger().info("Reward: " + reward); |
| 85 | + } |
| 86 | + |
| 87 | + Logger.getAnonymousLogger().info("average: " + rewards/1000); |
| 88 | + } |
| 89 | +} |
0 commit comments