diff --git a/Reinforcement_Learning/Function.py b/Reinforcement_Learning/Function.py new file mode 100644 index 0000000000000000000000000000000000000000..62f0eb3f9ad043f3386b5b36510b37a1dd6caf9b --- /dev/null +++ b/Reinforcement_Learning/Function.py @@ -0,0 +1,117 @@ +# Trying again + + +class Expression: + def __add__(self, other): + if isinstance(other, int) or isinstance(other, float): + other = Constant(other) + return Addition(self, other) + + +class Constant(Expression): + def __init__(self, val): + if isinstance(val, int) or isinstance(val, float): + self.val = val + else: + raise TypeError + + def __str__(self): + return f"{self.val}" + + def __eq__(self, other): + if isinstance(other, Constant): + return self.val == other.val + return False + + def evaluate(self, env): + return self.val + + def derivative(self, name): + return Constant(0) + + def simplify(self): + return self + + def decompose(self): + return [] + + +class Variable(Expression): + def __init__(self, name): + if name[0].isalpha() and name[0].isascii(): + self.name = name + else: + raise TypeError + + def __str__(self): + return f"{self.name}" + + def __eq__(self, other): + if isinstance(other, Variable): + return self.name == other.name + return False + + # def __neg__(self): + # pass + + def evaluate(self, env): + return Constant(env[self.name]).evaluate(env) + + def derivative(self, name): + return Constant(1 if self.name == name else 0) + + def decompose(self): + return [self.name] + + +class UnaryOp(Expression): + pass + + +class BinaryOp(Expression): + def __init__(self, left, right): + self.left = left + self.right = right + self.name = "" + + def __str__(self): + return f"({self.left} {self.name} {self.right})" + + def evaluate(self, env): + return self.operate(self.left.evaluate(env), self.right.evaluate(env)) + + def decompose(self): + return self.left.vs() + self.right.vs() + + +class Addition(BinaryOp): + def __init__(self, left, right): + super().__init__(left, right) + self.name = "+" + + def operate(self, x, y): + return x + y + + def derivative(self, name): + return self.left.derivative(name) + self.right.derivative(name) + + def simplify(self): + left = self.left.simplify() + right = self.right.simplify() + if left.decompose() == []: + left = left.evaluate({}) + if right.decompose() == []: + right = right.evaluate({}) + + if isinstance(left, Constant) and isinstance(right, Constant): + return Constant(left.val + right.val) + + if left == Constant(0): + return right + + if right == Constant(0): + return left + + return left + right + + diff --git a/Reinforcement_Learning/Perceptrons.py b/Reinforcement_Learning/Perceptrons.py index 7028ba4caf03b6ed02c6d0f762e36e6bcb1c4420..7dd939e8a3d7b58a4fc2a0e5107a9c69da7c5775 100644 --- a/Reinforcement_Learning/Perceptrons.py +++ b/Reinforcement_Learning/Perceptrons.py @@ -3,7 +3,7 @@ import Training_data import matplotlib.pyplot as plt rng = np.random.default_rng(123) -TEACHDATA = 99999 +TEACHDATA = 9999 TESTDATA = 999 T_NUMBER = 6 # Number to be detected 0-6 @@ -62,7 +62,7 @@ class ThresholdPerceptron(Neuron): delta = ETA * err * ix self.weights = self.weights + delta self.errors.append(abs(err)) - # if np.linalg.norm(old_weights - self.weights) == 0.00: + # if np.linalg.norm(old_weights - self.weights) == 0.00 and i % 57 == 0: # return return