Ejemplo n.º 1
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()
Ejemplo n.º 2
0
 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)
Ejemplo n.º 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()
Ejemplo n.º 4
0
 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()))
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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()))
Ejemplo n.º 8
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)