Example #1
0
class DatasetGenerator(object):

    def __init__(self,pendulum_mass, cart_mass,pendulum_length):
        self.pendulum_mass = pendulum_mass
        self.cart_mass=cart_mass
        self.pendulum_length = pendulum_length
        self.model = InvertedPendulumModel(self.pendulum_length,self.pendulum_mass,self.cart_mass)
        mass = cart_mass + pendulum_mass
        self.force = mass*10

    def generateRandomDataset(self,tests_number):
        result_dataset = Dataset()
        for i in range(tests_number):
            situation = self._get_random_situation()
            conteract = self._evaluate_model_counteraction(situation)
            result_dataset.add_case(list(situation),conteract)
        return result_dataset

    def _get_random_situation(self):
        angular_position = random.uniform(*self._get_angle_range())
        angular_velocity = random.uniform(*self._get_angular_velocity_range())
        cart_position = random.uniform(*self._get_position_range())
        cart_velocity = random.uniform(*self._get_velocity_range())
        return angular_position,angular_velocity,cart_position,cart_velocity

    def _evaluate_model_counteraction(self, situation):
        dt = 0.01
        self.model.set_state(*situation)
        self.model.apply(0, dt)
        new_state = self.model.get_state()
        if self.good_positioning(new_state[0]):
            if self.moving_too_fast(new_state[3]):
                return -self.sign(new_state[3])*self.force
            else:
                return self.sign(new_state[2])*self.force
        elif new_state[0] > 0:
            return self.force
        else:
            return -self.force

    def _get_velocity_range(self):
        return [-0.5,0.5]

    def _get_position_range(self):
        return [-20,20]

    def _get_angle_range(self):
        return [-0.785,0.785] #pi/4

    def _get_angular_velocity_range(self):
        return [-3.14,3.14]

    def sign(self,x):
        return 1 if x >= 0 else -1

    def good_positioning(self,angular_position):
        return math.fabs(angular_position)<3*0.0174532925

    def moving_too_fast(self,cart_velocity):
        return math.fabs(cart_velocity) > self.force / 10
Example #2
0
class ConsoleRunner(object):
    def __init__(self, debug, max_steps, name, angular_position):
        self.set_pendulum_model(angular_position)
        self.simulation = Simulation(self.pendulumModel,
                                     self.__create_controller(name),
                                     sleeping_mode=False)
        self.observer = ExecutionMonitor(debug, max_steps, self.simulation)
        self.pendulumModel.register_observer(self.observer)

    def learn(self, iterations):
        self.simulation.controller.learn(iterations)

    def run(self, speed_multiplier, dt):
        self.simulation.run(speed_multiplier, dt)

    def set_pendulum_model(self, angular_position):
        self.pendulumModel = InvertedPendulumModel(
            pendulum_length=PENDULUM_LENGTH,
            pendulum_mass=PENDULUM_MASS,
            cart_mass=CART_MASS)
        self.pendulumModel.set_state(angular_position=angular_position,
                                     angular_velocity=ANGULAR_VELOCITY,
                                     cart_position=CART_POSITION,
                                     cart_velocity=CART_VELOCITY)

    def __create_controller(self, name):
        return ControllersUtil.get_controller(name)(PENDULUM_LENGTH,
                                                    PENDULUM_MASS, CART_MASS)

    def results(self):
        return self.observer.results()
Example #3
0
class ConsoleRunner(object):
    def __init__(self, debug, max_steps, name, angular_position):
        self.set_pendulum_model(angular_position)
        self.simulation = Simulation(self.pendulumModel, self.__create_controller(name), sleeping_mode=False)
        self.observer = ExecutionMonitor(debug, max_steps, self.simulation)
        self.pendulumModel.register_observer(self.observer)

    def learn(self, iterations):
        self.simulation.controller.learn(iterations)

    def run(self, speed_multiplier, dt):
        self.simulation.run(speed_multiplier, dt)

    def set_pendulum_model(self, angular_position):
        self.pendulumModel = InvertedPendulumModel(
            pendulum_length=PENDULUM_LENGTH, pendulum_mass=PENDULUM_MASS, cart_mass=CART_MASS
        )
        self.pendulumModel.set_state(
            angular_position=angular_position,
            angular_velocity=ANGULAR_VELOCITY,
            cart_position=CART_POSITION,
            cart_velocity=CART_VELOCITY,
        )

    def __create_controller(self, name):
        return ControllersUtil.get_controller(name)(PENDULUM_LENGTH, PENDULUM_MASS, CART_MASS)

    def results(self):
        return self.observer.results()
Example #4
0
class DatasetGenerator(object):
    def __init__(self, pendulum_mass, cart_mass, pendulum_length):
        self.pendulum_mass = pendulum_mass
        self.cart_mass = cart_mass
        self.pendulum_length = pendulum_length
        self.model = InvertedPendulumModel(self.pendulum_length,
                                           self.pendulum_mass, self.cart_mass)
        mass = cart_mass + pendulum_mass
        self.force = mass * 10

    def generateRandomDataset(self, tests_number):
        result_dataset = Dataset()
        for i in range(tests_number):
            situation = self._get_random_situation()
            conteract = self._evaluate_model_counteraction(situation)
            result_dataset.add_case(list(situation), conteract)
        return result_dataset

    def _get_random_situation(self):
        angular_position = random.uniform(*self._get_angle_range())
        angular_velocity = random.uniform(*self._get_angular_velocity_range())
        cart_position = random.uniform(*self._get_position_range())
        cart_velocity = random.uniform(*self._get_velocity_range())
        return angular_position, angular_velocity, cart_position, cart_velocity

    def _evaluate_model_counteraction(self, situation):
        dt = 0.01
        self.model.set_state(*situation)
        self.model.apply(0, dt)
        new_state = self.model.get_state()
        if self.good_positioning(new_state[0]):
            if self.moving_too_fast(new_state[3]):
                return -self.sign(new_state[3]) * self.force
            else:
                return self.sign(new_state[2]) * self.force
        elif new_state[0] > 0:
            return self.force
        else:
            return -self.force

    def _get_velocity_range(self):
        return [-0.5, 0.5]

    def _get_position_range(self):
        return [-20, 20]

    def _get_angle_range(self):
        return [-0.785, 0.785]  #pi/4

    def _get_angular_velocity_range(self):
        return [-3.14, 3.14]

    def sign(self, x):
        return 1 if x >= 0 else -1

    def good_positioning(self, angular_position):
        return math.fabs(angular_position) < 3 * 0.0174532925

    def moving_too_fast(self, cart_velocity):
        return math.fabs(cart_velocity) > self.force / 10
 def __init__(self,pendulum_mass, cart_mass,pendulum_length):
     self.pendulum_mass = pendulum_mass
     self.cart_mass=cart_mass
     self.pendulum_length = pendulum_length
     self.model = InvertedPendulumModel(self.pendulum_length,self.pendulum_mass,self.cart_mass)
     mass = cart_mass + pendulum_mass
     self.force = mass*10
     self.radian = 0.0174532925
Example #6
0
 def set_pendulum_model(self, angular_position):
     self.pendulumModel = InvertedPendulumModel(
         pendulum_length=PENDULUM_LENGTH,
         pendulum_mass=PENDULUM_MASS,
         cart_mass=CART_MASS)
     self.pendulumModel.set_state(angular_position=angular_position,
                                  angular_velocity=ANGULAR_VELOCITY,
                                  cart_position=CART_POSITION,
                                  cart_velocity=CART_VELOCITY)
Example #7
0
 def __create_scene(self):
     self.pendulumView = PendulumView(
         self.width(),
         self.height(),
         visible_meters=float(self.ui.visibility.text()),
         length=float(self.ui.pendulumLength.text()),
         mass=float(self.ui.pendulumMass.text()))
     self.ui.view.setScene(self.pendulumView)
     self.pendulumModel = InvertedPendulumModel(
         pendulum_length=float(self.ui.pendulumLength.text()),
         pendulum_mass=float(self.ui.pendulumMass.text()),
         cart_mass=float(self.ui.cartMass.text()))
     self.pendulumModel.register_observer(self.pendulumView)
     self.set_pendulum_model()
Example #8
0
 def __init__(self,pendulum_mass, cart_mass,pendulum_length):
     self.pendulum_mass = pendulum_mass
     self.cart_mass=cart_mass
     self.pendulum_length = pendulum_length
     self.model = InvertedPendulumModel(self.pendulum_length,self.pendulum_mass,self.cart_mass)
     mass = cart_mass + pendulum_mass
     self.force = mass*10
Example #9
0
 def __init__(self, pendulum_length, pendulum_mass, cart_mass):
     super(ReinforcedController, self).__init__(pendulum_length,
                                                pendulum_mass, cart_mass)
     self.ranges = self.get_ranges()
     self.model = InvertedPendulumModel(self.pendulum_length,
                                        self.pendulum_mass, self.cart_mass)
     self.force_granularity = 2
     self.environment = CartEnvironment(
         self.model, *self.ranges, force_granularity=self.force_granularity)
Example #10
0
 def set_pendulum_model(self, angular_position):
     self.pendulumModel = InvertedPendulumModel(
         pendulum_length=PENDULUM_LENGTH, pendulum_mass=PENDULUM_MASS, cart_mass=CART_MASS
     )
     self.pendulumModel.set_state(
         angular_position=angular_position,
         angular_velocity=ANGULAR_VELOCITY,
         cart_position=CART_POSITION,
         cart_velocity=CART_VELOCITY,
     )
Example #11
0
 def __create_scene(self):
     self.pendulumView = PendulumView(
         self.width(),
         self.height(),
         visible_meters=float(self.ui.visibility.text()),
         length=float(self.ui.pendulumLength.text()),
         mass=float(self.ui.pendulumMass.text()))
     self.ui.view.setScene(self.pendulumView)
     self.pendulumModel = InvertedPendulumModel(
         pendulum_length=float(self.ui.pendulumLength.text()),
         pendulum_mass=float(self.ui.pendulumMass.text()),
         cart_mass=float(self.ui.cartMass.text()))
     self.pendulumModel.register_observer(self.pendulumView)
     self.set_pendulum_model()
Example #12
0
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.view.setRenderHint(QPainter.Antialiasing)
        self.__setup_ui()
        self.__create_scene()

    @pyqtSlot()
    def on_run_clicked(self):
        self.__set_widget_state(running=True)
        self.set_pendulum_model()
        self.simulation.run(int(self.ui.speedMultiplier.text()),float(self.ui.dt.text()))

    @pyqtSlot()
    def on_learn_clicked(self):
        self.ui.run.setEnabled(True)
        self.__create_scene()
        self.simulation = Simulation(self.pendulumModel, self.__create_controller())
        self.simulation.controller.learn(int(self.ui.iterations.text()))

    @pyqtSlot()
    def on_stop_clicked(self):
        self.simulation.stop()
        self.__set_widget_state(running=False)

    @pyqtSlot()
    def on_visibility_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_pendulumLength_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_pendulumMass_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_pendulumAngle_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_cartPosition_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_updateButton_clicked(self):
        self.__create_scene()

    def set_pendulum_model(self):
        self.pendulumModel.set_state(
            angular_position=float(self.ui.pendulumAngle.text()) * pi / 180.0,
            angular_velocity=float(self.ui.angularVelocity.text()),
            cart_position=float(self.ui.cartPosition.text()),
            cart_velocity=float(self.ui.cartSpeed.text())
        )

    def __create_scene(self):
        self.pendulumView = PendulumView(
            self.width(),
            self.height(),
            visible_meters=float(self.ui.visibility.text()),
            length=float(self.ui.pendulumLength.text()),
            mass=float(self.ui.pendulumMass.text())
        )
        self.ui.view.setScene(self.pendulumView)
        self.pendulumModel = InvertedPendulumModel(
            pendulum_length=float(self.ui.pendulumLength.text()),
            pendulum_mass=float(self.ui.pendulumMass.text()),
            cart_mass=float(self.ui.cartMass.text())
        )
        self.pendulumModel.register_observer(self.pendulumView)
        self.set_pendulum_model()

    def __create_controller(self):
        return ControllersUtil.get_controller(str(self.ui.controller.currentText()))(
            float(self.ui.pendulumLength.text()),
            float(self.ui.pendulumMass.text()),
            float(self.ui.cartMass.text())
        )

    def __set_widget_state(self, running):
        self.ui.allSettings.setEnabled(not running)
        self.ui.stop.setEnabled(running)
        self.ui.run.setEnabled(not running)

    def __setup_ui(self):
        self.ui.controller.addItems(ControllersUtil.registered_controllers())
        self.ui.controller.setCurrentIndex(self.ui.controller.findText(ControllersUtil.default_controller()))

    def resizeEvent(self, QResizeEvent):
        self.ui.view.fitInView(self.pendulumView.sceneRect(), Qt.KeepAspectRatio)

    def show(self):
        QMainWindow.show(self)
        self.resizeEvent(None)

    def closeEvent(self, QCloseEvent):
        sys.exit(0)
Example #13
0
class DatasetBlindGenerator(object):

    def __init__(self,pendulum_mass, cart_mass,pendulum_length):
        self.pendulum_mass = pendulum_mass
        self.cart_mass=cart_mass
        self.pendulum_length = pendulum_length
        self.model = InvertedPendulumModel(self.pendulum_length,self.pendulum_mass,self.cart_mass)
        mass = cart_mass + pendulum_mass
        self.force = mass*10
        self.radian = 0.0174532925

    def get_force(self):
        return self.force

    def generateRandomDataset(self,tests_number):
        result_dataset = Dataset()
        for i in range(tests_number):
            situations = self._get_random_situations()
            for situation in situations:
                model_response = self._evaluate_model_response(situation)
                result_dataset.add_case(list(situation),model_response)
        return result_dataset

    def _get_random_situations(self):
        angular_position = random.uniform(*self._get_angle_range())
        angular_velocity = random.uniform(*self._get_angular_velocity_range())
        cart_position = random.uniform(*self._get_position_range())
        cart_velocity = random.uniform(*self._get_velocity_range())
        return [(angular_position,angular_velocity,cart_position,cart_velocity, self.force),
                (angular_position,angular_velocity,cart_position,cart_velocity, -self.force)]

    def _evaluate_model_response(self, situation):
        dt = 0.01
        angular_position = situation[0]
        angular_velocity = situation[1]
        cart_position = situation[2]
        cart_velocity = situation[3]
        force = situation[4]
        self.model.set_state(angular_position,angular_velocity,cart_position,cart_velocity)
        self.model.apply(force, dt)
        new_state = self.model.get_state()
        new_angular_position = new_state[0]
        #return self.normalize(new_angular_position)
        return new_angular_position

    def _get_velocity_range(self):
        return [-0.5,0.5]

    def _get_position_range(self):
        return [-20,20]

    def _get_angle_range(self):
        return [-0.785,0.785] #pi/4

    def _get_angular_velocity_range(self):
        return [-3.14,3.14]

    def sign(self,x):
        return 1 if x >= 0 else -1

    def normalize(self, rad):
        return int((rad / self.radian))

    def get_radian(self):
        return self.radian
Example #14
0
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.view.setRenderHint(QPainter.Antialiasing)
        self.__setup_ui()
        self.__create_scene()

    @pyqtSlot()
    def on_run_clicked(self):
        self.__set_widget_state(running=True)
        self.set_pendulum_model()
        self.simulation.run(int(self.ui.speedMultiplier.text()),
                            float(self.ui.dt.text()))

    @pyqtSlot()
    def on_learn_clicked(self):
        self.ui.run.setEnabled(True)
        self.__create_scene()
        self.simulation = Simulation(self.pendulumModel,
                                     self.__create_controller())
        self.simulation.controller.learn(int(self.ui.iterations.text()))

    @pyqtSlot()
    def on_stop_clicked(self):
        self.simulation.stop()
        self.__set_widget_state(running=False)

    @pyqtSlot()
    def on_visibility_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_pendulumLength_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_pendulumMass_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_pendulumAngle_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_cartPosition_lostFocus(self):
        self.__create_scene()

    @pyqtSlot()
    def on_updateButton_clicked(self):
        self.__create_scene()

    def set_pendulum_model(self):
        self.pendulumModel.set_state(
            angular_position=float(self.ui.pendulumAngle.text()) * pi / 180.0,
            angular_velocity=float(self.ui.angularVelocity.text()),
            cart_position=float(self.ui.cartPosition.text()),
            cart_velocity=float(self.ui.cartSpeed.text()))

    def __create_scene(self):
        self.pendulumView = PendulumView(
            self.width(),
            self.height(),
            visible_meters=float(self.ui.visibility.text()),
            length=float(self.ui.pendulumLength.text()),
            mass=float(self.ui.pendulumMass.text()))
        self.ui.view.setScene(self.pendulumView)
        self.pendulumModel = InvertedPendulumModel(
            pendulum_length=float(self.ui.pendulumLength.text()),
            pendulum_mass=float(self.ui.pendulumMass.text()),
            cart_mass=float(self.ui.cartMass.text()))
        self.pendulumModel.register_observer(self.pendulumView)
        self.set_pendulum_model()

    def __create_controller(self):
        return ControllersUtil.get_controller(
            str(self.ui.controller.currentText()))(
                float(self.ui.pendulumLength.text()),
                float(self.ui.pendulumMass.text()),
                float(self.ui.cartMass.text()))

    def __set_widget_state(self, running):
        self.ui.allSettings.setEnabled(not running)
        self.ui.stop.setEnabled(running)
        self.ui.run.setEnabled(not running)

    def __setup_ui(self):
        self.ui.controller.addItems(ControllersUtil.registered_controllers())
        self.ui.controller.setCurrentIndex(
            self.ui.controller.findText(ControllersUtil.default_controller()))

    def resizeEvent(self, QResizeEvent):
        self.ui.view.fitInView(self.pendulumView.sceneRect(),
                               Qt.KeepAspectRatio)

    def show(self):
        QMainWindow.show(self)
        self.resizeEvent(None)

    def closeEvent(self, QCloseEvent):
        sys.exit(0)