Skip to content

Commit

Permalink
Added updated base_script fixed on private repo
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsenErik committed Sep 4, 2023
1 parent a4f1686 commit d0b6723
Show file tree
Hide file tree
Showing 11,155 changed files with 1,765,663 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file added src/base_scripts/.script.sh.swp
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
539 changes: 539 additions & 0 deletions src/base_scripts/dts4marl/algorithms/grammatical_evolution.py

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions src/base_scripts/dts4marl/battlefield.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"grammar": {
"root": ["condition", "leaf"],
"input_index": {
"start": 0,
"stop": 34,
"step": 1,
"dtype": "int"
},
"float": {
"start": 0.1,
"stop": 1,
"step": 0.1,
"dtype": "float"
}
},
"conditions": {
"type": "orthogonal"
},
"leaves": {
"params": {
"n_actions": 21,
"learning_rate": null
},
"decorators": [
[
"RandomInit",
{
"low": -1,
"high": 1
}

],
[
"EpsilonGreedy",
{
"epsilon": 1,
"decay": 0.99,
"min_epsilon": 0.05
}
],
[
"NoBuffers",
{}
]
]
},
"environment": {
"map_size": 80,
"minimap_mode": true,
"step_reward": -0.005,
"dead_penalty": -0.1,
"attack_penalty": -0.1,
"attack_opponent_reward": 0.9,
"max_cycles": 500,
"extra_features": false
},
"training": {
"gamma": 0.9,
"episodes": 400,
"jobs": 12,
"generations": 40
},
"statistics": {
"agent": {
"type": "quantile",
"params": {"q": 0.7, "method": "midpoint"}
},
"set": {
"type": "mean",
"params": {}
}
},
"sets": "random",
"team_to_optimize": "blue",
"observation": "34_new",
"manual_policy": null,
"ge": {
"pop_size": 60,
"agents": 12,
"sets": 1,
"no_mig": true,
"individual_genes_injected": [0,22,3,0,23,3,0,5,4,0,24,3,0,7,4,0,25,3,0,8,4,1,0,10,4,0,30,5,1,0,20,2,1,1,1,0,32,5,1,0,21,2,1,1,1,0,29,5,1,0,18,2,1,1,1,0,27,5,1,0,17,2,1,1,1],
"injection_rate": 0.05,
"mutation": {
"type": "UniformMutator",
"params": {
"gene_probability": 0.05,
"max_value": 10000
}
},
"crossover": {
"type": "OnePointCrossover",
"params": {}
},
"selection": {
"type": "TournamentSelection",
"params": {
"tournament_size": 3
}
},
"replacement": {
"type": "ReplaceWithOldIfWorse",
"params": {}
},
"mut_prob": 0.8,
"cx_prob": 0.4,
"genotype_length": 500,
"max_int": 10000
}
}
2 changes: 2 additions & 0 deletions src/base_scripts/dts4marl/decisiontreelibrary/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
*.swp
8 changes: 8 additions & 0 deletions src/base_scripts/dts4marl/decisiontreelibrary/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
run tests:
image: python:3
script:
- pip install numpy
- pip install pytest pytest-cov
- pytest --cov=. tests
- coverage xml
coverage: '/TOTAL.*\s+(\d+%)$/'
8 changes: 8 additions & 0 deletions src/base_scripts/dts4marl/decisiontreelibrary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# DecisionTreeLibrary
![coverage](https://gitlab.com/leocus/decisiontreelibrary/badges/master/coverage.svg)
This library aims to be a general library for building/using decision trees.
In fact, with this library we do not aim to manage only the cases in which the DT is used for classification or regression purposes, but we make it work also for more recent applications, e.g. Reinforcement Learning.
The library has 3 main entities:
- Leaves: These classes learn a subset -> action mapping (e.g. subset -> class in classification, subset -> value in regression, state -> action in RL)
- Conditions: These classes split their input space into two subsets. The type of conditions is not bound to any type (e.g. orthogonal, oblique, etc), in fact, a class extending Condition can implement its own splitting criterion.
- Trees: These classes are "wrappers" that contain a root (either a leaf or a condition) and implement methods to manage the tree.
9 changes: 9 additions & 0 deletions src/base_scripts/dts4marl/decisiontreelibrary/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python
from .leaves import QLearningLeafFactory
from .nodes import *
from .conditions import ConditionFactory
from .trees import DecisionTree, RLDecisionTree

# MIA AGGIUNTA
from .conditions import Condition
from .leaves import Leaf
255 changes: 255 additions & 0 deletions src/base_scripts/dts4marl/decisiontreelibrary/conditions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
src.trees
~~~~~~~~~
This module implements the conditions to be used in the decision trees
:copyright: (c) 2021 by Leonardo Lucio Custode.
:license: MIT, see LICENSE for more details.
"""
import abc
from copy import deepcopy
from .nodes import Node


class Condition(Node):
"""
This is the base class for the conditions.
"""
BRANCH_LEFT = True
BRANCH_RIGHT = False

def __init__(self, left, right):
"""
Initializes an internal node (that checks a condition)
:left: The node in the left branch (i.e. the one taken when the
condition evaluates to True)
:right: The node in the right branch (i.e. the one taken when
the condition evaluates to False)
"""
Node.__init__(self)

self._left = left
self._right = right

def get_left(self):
return self._left

def get_right(self):
return self._right

def set_left(self, value):
self._left = value

def set_right(self, value):
self._right = value

@abc.abstractstaticmethod
def get_trainable_parameters(self):
"""
Returns a list of parameters with their type
(input_index, int or float) as a string.
"""
pass

@abc.abstractmethod
def set_params_from_list(self, params):
"""
Sets its parameters according to the parameters specified by
the input list.
:params: A list of params (int or float)
"""
pass

def get_output(self, input_):
"""
Computes the output associated to its inputs (i.e. computes
the path of the input vector (or vectors) in the tree and returns
the decision associated to it).
:input_: A 1D numpy array
:returns: A 1D numpy array
"""
assert len(input_.shape) == 1, "Only 1D arrays are currently supported"
if self.get_branch(input_) == Condition.BRANCH_LEFT:
return self._left.get_output(input_)
else:
return self._right.get_output(input_)

@abc.abstractmethod
def get_branch(self, inputs):
"""
Computes the branch taken given the inputs
:inputs: 1D numpy array (1 sample) or 2D numpy array (N samples)
:returns: A numpy array where each element is either:
- True: Left branch has been taken
- False: Right branch has been taken
"""
pass

def empty_buffers(self):
self._left.empty_buffers()
self._right.empty_buffers()

def copy_structure(self):
"""
Returns a copy of the structure of itself
"""
new = Condition(self.get_left().copy_structure(), self.get_right().copy_structure())
return new

def deep_copy(self):
"""
Returns a deep copy of itself
"""
return deepcopy(self)

class OrthogonalCondition(Condition):
"""
This class implements orthogonal conditions for the decision tree.
Orthogonal conditions are the ones that generate hyperplanes that are
orthogonal to the chosen axis (i.e. they test only one variable).
"""

def __init__(self, feature_idx, split_value, left=None, right=None):
"""
Initializes the condition.
:feature_idx: the index of the variable (that has to be tested) in
the input vector
:split_value: The value used for the test
:left: The left node. Default: None.
:right: The right node. Default: None.
"""
Condition.__init__(self, left, right)

self._feature_idx = feature_idx
self._split_value = split_value

def get_branch(self, inputs):
"""
Computes the branch taken given the inputs
:inputs: 1D numpy array (1 sample) or 2D numpy array (N samples)
:returns: A numpy array where each element is either:
- True: Left branch has been taken
- False: Right branch has been taken
"""
if len(inputs.shape) == 1:
return inputs[self._feature_idx] < self._split_value
return inputs[:, self._feature_idx] < self._split_value

@staticmethod
def get_trainable_parameters():
"""
Returns a list of parameters with their type
(input_index, int or float) as a string.
"""
return ["input_index", "float"]

@staticmethod
def check_params(params):
"""
Checks whether the parameters are good for this type of node.
If not, it raises an AssertionError
:params: A list of params (int or float)
"""
assert len(params) >= 2, \
"This type of condition requires 2 parameters."

@staticmethod
def create_from_params(params):
"""
Creates a condition from its parameters
:params: A list of params (int or float)
"""
OrthogonalCondition.check_params(params)
return OrthogonalCondition(int(params[0]), float(params[1]))

def set_params_from_list(self, params):
"""
Sets its parameters according to the parameters specified by
the input list.
:params: A list of params (int or float)
"""
OrthogonalCondition.check_params(params)
self._feature_idx = int(params[0])
self._split_value = float(params[1])

def get_feature_idx(self):
return self._feature_idx

def get_split_value(self):
return self._split_value

def set_feature_idx(self, value):
self._feature_idx = value

def set_split_value(self, value):
self._split_value = value

def __str__(self):
return f"x_{self._feature_idx} < {round(self._split_value, 1)}"
#return f"x_{self._feature_idx} < {self._split_value}" # Original

def copy_structure(self):
"""
Returns a copy of the structure of itself
"""
new = OrthogonalCondition(
self.get_feature_idx(),
self.get_split_value(),
self.get_left().copy_structure(),
self.get_right().copy_structure()
)
return new

def deep_copy(self):
"""
Returns a deep copy of itself
"""
return deepcopy(self)


class ConditionFactory:
"""
A factory for conditions
"""
ORTHOGONAL = "orthogonal"

CONDITIONS = {
ORTHOGONAL: OrthogonalCondition,
}

def __init__(self, condition_type="orthogonal"):
"""
Initializes the factory of conditions
:condition_type: strings supported:
- orthogonal
"""
self._condition_type = condition_type

def create(self, params):
"""
Creates a condition
:returns: A Condition
"""
return self.CONDITIONS[self._condition_type].create_from_params(params)

def get_trainable_parameters(self):
"""
Returns a list of parameters with their type (int or float).
"""
return self.CONDITIONS[self._condition_type].get_trainable_parameters()


Loading

0 comments on commit d0b6723

Please sign in to comment.