From f5cb877531ec89a0a5d7d9ef8aafe6a8fc7e413d Mon Sep 17 00:00:00 2001
From: Mathurn Videau <mathurin.videau@epfedu.fr>
Date: Sun, 26 Sep 2021 21:49:10 +0200
Subject: [PATCH] license + fix MCTS

---
 GPRL/MCTS/MCTS.py                             |  5 +++
 GPRL/UCB.py                                   |  5 +++
 GPRL/algorithms.py                            |  5 +++
 GPRL/containers/grid.py                       |  5 +++
 GPRL/factory.py                               |  5 +++
 GPRL/genetic_programming/linearGP.py          |  5 +++
 GPRL/genetic_programming/team.py              |  5 +++
 GPRL/utils/FEC.py                             |  5 +++
 GPRL/utils/gp_utils.py                        |  5 +++
 GPRL/utils/optim.py                           |  5 +++
 GPRL/utils/utils.py                           |  5 +++
 LICENSE                                       | 21 ++++++++++++
 conf/conf_qdgp-BipedalWalker.yml              |  1 +
 conf/conf_qdgp-Hopper.yml                     |  1 +
 conf/conf_qdlingp-BipedalWalker.yml           |  1 +
 conf/conf_qdlingp-Hopper.yml                  |  1 +
 experiments/ADF.py                            |  5 +++
 experiments/MCTS.py                           | 34 +++++++++++++++----
 experiments/bench.py                          |  5 +++
 experiments/gp.py                             |  7 +++-
 .../imitation_learning/imitation_gp.py        |  5 +++
 .../imitation_learning/imitation_linGP.py     |  4 +++
 .../imitation_learning/imitation_utils.py     |  5 +++
 .../imitation_learning/time_feature.py        |  5 +++
 experiments/linGP.py                          |  5 +++
 experiments/qdgp.py                           |  5 +++
 experiments/qdlinGP.py                        |  5 +++
 27 files changed, 157 insertions(+), 8 deletions(-)
 create mode 100644 LICENSE

diff --git a/GPRL/MCTS/MCTS.py b/GPRL/MCTS/MCTS.py
index 04cbedf..e0b7849 100644
--- a/GPRL/MCTS/MCTS.py
+++ b/GPRL/MCTS/MCTS.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from ..genetic_programming.linearGP import Instruction
 import numpy as np
 
diff --git a/GPRL/UCB.py b/GPRL/UCB.py
index 9aaaab1..0d7f520 100644
--- a/GPRL/UCB.py
+++ b/GPRL/UCB.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from copy import deepcopy
 from functools import partial
 import heapq
diff --git a/GPRL/algorithms.py b/GPRL/algorithms.py
index 2c7799e..020a652 100644
--- a/GPRL/algorithms.py
+++ b/GPRL/algorithms.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from collections import deque
 from deap import tools, algorithms
 import heapq
diff --git a/GPRL/containers/grid.py b/GPRL/containers/grid.py
index 8f5c13e..50d0f4d 100644
--- a/GPRL/containers/grid.py
+++ b/GPRL/containers/grid.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from functools import reduce
 import operator
 import numpy as np
diff --git a/GPRL/factory.py b/GPRL/factory.py
index 4098c6c..cbdaec1 100644
--- a/GPRL/factory.py
+++ b/GPRL/factory.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from abc import ABC, abstractmethod
 
 class EvolveFactory(ABC):#Base class to produce toolbox in different script and keep multiprocessing support (evolve.py script)
diff --git a/GPRL/genetic_programming/linearGP.py b/GPRL/genetic_programming/linearGP.py
index 5c19ea6..6565d98 100644
--- a/GPRL/genetic_programming/linearGP.py
+++ b/GPRL/genetic_programming/linearGP.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import random
 from functools import partial
 import copy
diff --git a/GPRL/genetic_programming/team.py b/GPRL/genetic_programming/team.py
index 273049f..a7e85a0 100644
--- a/GPRL/genetic_programming/team.py
+++ b/GPRL/genetic_programming/team.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import random
 import numpy as np
 from sklearn.linear_model import LinearRegression
diff --git a/GPRL/utils/FEC.py b/GPRL/utils/FEC.py
index d2044e5..549b670 100644
--- a/GPRL/utils/FEC.py
+++ b/GPRL/utils/FEC.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from collections import OrderedDict
 from functools import reduce
 import numpy as np
diff --git a/GPRL/utils/gp_utils.py b/GPRL/utils/gp_utils.py
index 66f9d2c..f61469f 100644
--- a/GPRL/utils/gp_utils.py
+++ b/GPRL/utils/gp_utils.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import numpy as np
 import random
 from deap import gp
diff --git a/GPRL/utils/optim.py b/GPRL/utils/optim.py
index 2c35535..7b203ec 100644
--- a/GPRL/utils/optim.py
+++ b/GPRL/utils/optim.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import numpy as np
 
 from functools import partial
diff --git a/GPRL/utils/utils.py b/GPRL/utils/utils.py
index 7d71367..1ed935c 100644
--- a/GPRL/utils/utils.py
+++ b/GPRL/utils/utils.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from functools import reduce
 from operator import add, itemgetter
 import pickle
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..72cf512
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2021 Mathurin Videau
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
\ No newline at end of file
diff --git a/conf/conf_qdgp-BipedalWalker.yml b/conf/conf_qdgp-BipedalWalker.yml
index 43d11de..ef39b92 100644
--- a/conf/conf_qdgp-BipedalWalker.yml
+++ b/conf/conf_qdgp-BipedalWalker.yml
@@ -1,6 +1,7 @@
 algorithm:
   name: algo.qdLambda
   args:
+    save_every: 10
     batch_size: 100
     lambda_: 500
     ngen: 5000
diff --git a/conf/conf_qdgp-Hopper.yml b/conf/conf_qdgp-Hopper.yml
index 077c0ac..58d632c 100644
--- a/conf/conf_qdgp-Hopper.yml
+++ b/conf/conf_qdgp-Hopper.yml
@@ -1,6 +1,7 @@
 algorithm:
   name: algo.qdLambda
   args:
+    save_every: 10
     batch_size: 100
     lambda_: 500
     ngen: 5000
diff --git a/conf/conf_qdlingp-BipedalWalker.yml b/conf/conf_qdlingp-BipedalWalker.yml
index 9281981..2edc0e8 100644
--- a/conf/conf_qdlingp-BipedalWalker.yml
+++ b/conf/conf_qdlingp-BipedalWalker.yml
@@ -1,6 +1,7 @@
 algorithm:
   name: algo.qdLambda
   args:
+    save_every: 10
     batch_size: 100
     lambda_: 500
     ngen: 5000
diff --git a/conf/conf_qdlingp-Hopper.yml b/conf/conf_qdlingp-Hopper.yml
index b4e8c63..3936eeb 100644
--- a/conf/conf_qdlingp-Hopper.yml
+++ b/conf/conf_qdlingp-Hopper.yml
@@ -1,6 +1,7 @@
 algorithm:
   name: algo.qdLambda
   args:
+    save_every: 10
     batch_size: 100
     lambda_: 500
     ngen: 5000
diff --git a/experiments/ADF.py b/experiments/ADF.py
index fc4f1b1..2a47d43 100644
--- a/experiments/ADF.py
+++ b/experiments/ADF.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from deap import gp, creator, base, tools
 import numpy as np
 
diff --git a/experiments/MCTS.py b/experiments/MCTS.py
index 48e282e..40c24a9 100644
--- a/experiments/MCTS.py
+++ b/experiments/MCTS.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from functools import partial
 import operator
 import numpy as np
@@ -19,14 +24,25 @@ def init(env_name, func_set):
     exp_function = [ (gp_utils.exp, [float], float), (gp_utils.log, [float], float)]
     trig_function = [(np.sin, [float], float)]
     if_function = [ (gp_utils.if_then_else, [bool, float, float], float), (operator.gt, [float, float], bool), (operator.and_, [bool, bool], bool), (operator.or_, [bool, bool], bool) ]
-    classification_func = [(gp_utils.classification, [float, float, float], int)] #(gp_utils.intervales, [float], int)
 
     if func_set  == "small":
         function_set = core_function + if_function
     elif func_set == "extended":
         function_set = core_function + exp_function + trig_function   
+        
+    if bool(ENV.action_space.shape):
+        ret = float
+        OUTPUT = ENV.action_space.shape[0]
+    else:
+        OUTPUT = 1
+        if ENV.action_space.n == 2:
+            ret = bool
+        else:
+            ret = int
+            classification_func = [(gp_utils.classification, [float]*ENV.action_space.n, int)] #(gp_utils.intervales, [float], int)
+            function_set += classification_func
 
-    pset = gp.PrimitiveSetTyped("MAIN", [float]*INPUT, bool)
+    pset = gp.PrimitiveSetTyped("MAIN", [float]*INPUT, ret)
     for primitive in function_set:
         pset.addPrimitive(*primitive)
     pset.addTerminal(0.1, float)
@@ -37,12 +53,16 @@ def init(env_name, func_set):
 
 
 def fitness(individual, n_steps, gamma):
-    agent = gp.compile(gp.PrimitiveTree(individual), pset=pset)
+    if ENV.action_space.shape:
+        func = gp.compile(gp.PrimitiveTree(individual), pset=pset)
+        agent = lambda *s: [func(*s)]
+    else:
+        func = gp.compile(gp.PrimitiveTree(individual), pset=pset)
+        agent = lambda *s: int(func(*s))
     s = 0
     state = ENV.reset()
     for _ in range(n_steps):
-        #state, reward, done, _ = env.step(int(agent(*state)))
-        state, reward, done, _ = ENV.step([agent(*state)])
+        state, reward, done, _ = ENV.step(agent(*state))
         s+= gamma*reward
         if done:
             return s
@@ -69,8 +89,8 @@ if __name__ == "__main__":
 
     args = parser.parse_args()
     
-    init()
-    pool = Pool(args.n_thread, initializer=init)
+    init(args.env, args.function_set)
+    pool = Pool(args.n_thread, initializer=init, initargs=(args.env, args.function_set))
     
     func = partial(fitness, n_steps=args.n_steps, gamma=args.gamma)
     
diff --git a/experiments/bench.py b/experiments/bench.py
index e7f2593..4cb6622 100644
--- a/experiments/bench.py
+++ b/experiments/bench.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from GPRL.UCB import UpdateFitnessHof, selDoubleTournament, UCBFitness
 from functools import partial
 import operator
diff --git a/experiments/gp.py b/experiments/gp.py
index 7f57183..17d6d8b 100644
--- a/experiments/gp.py
+++ b/experiments/gp.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import warnings
 import gym
 try:
@@ -54,7 +59,6 @@ class Factory(EvolveFactory):
         exp_function = [ (gp_utils.exp, [float], float), (gp_utils.log, [float], float)]
         trig_function = [(np.sin, [float], float)]
         if_function = [ (gp_utils.if_then_else, [bool, float, float], float), (operator.gt, [float, float], bool), (operator.and_, [bool, bool], bool), (operator.or_, [bool, bool], bool) ]
-        classification_func = [(gp_utils.classification, [float, float, float], int)] #(gp_utils.intervales, [float], int)
 
 
         if self.conf['function_set'] == "small":
@@ -72,6 +76,7 @@ class Factory(EvolveFactory):
                 ret = bool
             else:
                 ret = int
+                classification_func = [(gp_utils.classification, [float]*ENV.action_space.n, int)] #(gp_utils.intervales, [float], int)
                 function_set += classification_func
 
         
diff --git a/experiments/imitation_learning/imitation_gp.py b/experiments/imitation_learning/imitation_gp.py
index 59fca97..3ae15d1 100644
--- a/experiments/imitation_learning/imitation_gp.py
+++ b/experiments/imitation_learning/imitation_gp.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from deap import gp, creator, base, tools
 import numpy as np
 
diff --git a/experiments/imitation_learning/imitation_linGP.py b/experiments/imitation_learning/imitation_linGP.py
index 2e361f3..07986c1 100644
--- a/experiments/imitation_learning/imitation_linGP.py
+++ b/experiments/imitation_learning/imitation_linGP.py
@@ -1,3 +1,7 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
 
 from functools import partial
 from GPRL.genetic_programming import linearGP as linGP
diff --git a/experiments/imitation_learning/imitation_utils.py b/experiments/imitation_learning/imitation_utils.py
index 3f49902..03ff166 100644
--- a/experiments/imitation_learning/imitation_utils.py
+++ b/experiments/imitation_learning/imitation_utils.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import numpy as np
 
 class RingReplayBuffer(object):
diff --git a/experiments/imitation_learning/time_feature.py b/experiments/imitation_learning/time_feature.py
index 3d04c46..9129ca8 100644
--- a/experiments/imitation_learning/time_feature.py
+++ b/experiments/imitation_learning/time_feature.py
@@ -1,3 +1,8 @@
+#Copyright (c) 2020 Stable-Baselines Team
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of stable-baselines3-contrib.
+
 from typing import Dict, Union
 
 import gym
diff --git a/experiments/linGP.py b/experiments/linGP.py
index fc5ec84..349e83f 100644
--- a/experiments/linGP.py
+++ b/experiments/linGP.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 from functools import partial
 import warnings
 import random
diff --git a/experiments/qdgp.py b/experiments/qdgp.py
index c106dca..b32cbd2 100644
--- a/experiments/qdgp.py
+++ b/experiments/qdgp.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import operator
 import numpy as np
 import pandas as pd
diff --git a/experiments/qdlinGP.py b/experiments/qdlinGP.py
index 216149a..d4fcb62 100644
--- a/experiments/qdlinGP.py
+++ b/experiments/qdlinGP.py
@@ -1,3 +1,8 @@
+# Copyright (c) Mathurin Videau. All Rights Reserved.
+#
+# This source code is licensed under the MIT license found in the
+# LICENSE file in the root directory of this source tree.
+
 import numpy as np
 from GPRL.containers.grid import FixGrid as Grid
 
-- 
GitLab