当前位置: 首页>>代码示例>>Python>>正文


Python World.instance方法代码示例

本文整理汇总了Python中salma.model.world.World.instance方法的典型用法代码示例。如果您正苦于以下问题:Python World.instance方法的具体用法?Python World.instance怎么用?Python World.instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在salma.model.world.World的用法示例。


在下文中一共展示了World.instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: perform_property_3_test

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def perform_property_3_test(self, item, pos_grab, min_pos, goal_pos):
        world = World.instance()

        p = OneShotProcess([
            While("xpos(self) < params[0]", [goal_pos], [
                If("xpos(self) == params[0]", [pos_grab], Act("paint", [SELF, item])),
                Act("move_right", [SELF]),
                Wait("not moving(self)")
            ])
        ])
        rob1 = Agent("rob1", "robot", [p])
        world.addAgent(rob1)
        world.addEntity(Entity("item1", "item"))
        world.addEntity(Entity("item2", "item"))
        world.initialize(False)
        self.setNoOneCarriesAnything()
        self.place_agents_in_column(x=10)

        g_str = """
        implies(occur(paint(rob1,?)), xpos(rob1) > {})
""".format(min_pos)

        def logger(world, **info):
            print("X = {}".format(world.get_fluent_value("xpos", ["rob1"])))

        experiment = Experiment(world)
        experiment.step_listeners.append(logger)
        experiment.property_collection.register_property("g", g_str, INVARIANT)
        experiment.property_collection.register_property("h", "xpos(rob1) >= {}".format(goal_pos), ACHIEVE)
        verdict, results = experiment.run()
        print("Verdict: " + str(verdict))
        print("Results: " + str(results))
        return verdict, results
开发者ID:salmatoolkit,项目名称:salma,代码行数:35,代码来源:property_evaluation_test_02.py

示例2: setupSelectionContext

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def setupSelectionContext(self):
        world = World.instance()

        item1 = Entity("item1", "item")
        item2 = Entity("item2", "item")

        world.addEntity(item1)
        world.addEntity(item2)

        seq1 = Sequence([
            Act("grab", [Entity.SELF, "item1"])
        ])
        seq2 = Sequence([
            Act("grab", [Entity.SELF, "item2"])
        ])

        # implicitly creates one-shot processes
        agent1 = Agent("rob1", "robot", Procedure("main", [], seq1))
        agent2 = Agent("rob2", "robot", Procedure("main", [], seq2))

        grabMap = {"rob1": item1, "rob2": item2}
        world.addAgent(agent1)
        world.addAgent(agent2)

        world.initialize(False)

        self.place_agents_in_column()

        self.setNoOneCarriesAnything()

        return agent1, agent2, grabMap
开发者ID:salmatoolkit,项目名称:salma,代码行数:33,代码来源:worldtest.py

示例3: test_custom_distrib_lambda

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_custom_distrib_lambda(self):
        world = World.instance()

        rob1 = self.create_random_walk_robot("rob1")
        rob2 = self.create_random_walk_robot("rob2")

        world.add(rob1, rob2)

        world.initialize(False)
        world.deactivate_info_transfer()

        world.world_width = 500
        world.world_height = 500
        world.safety_distance = 10
        self.init_robot(rob1, 250, 250, 1, 2)
        self.init_robot(rob2, 255, 250, 3, 4)

        collision = world.get_exogenous_action("collide")
        collision.config.occurrence_distribution = BernoulliDistribution(1.0)
        sev = lambda r1, r2: 1000 * r1.vx + 100 * r1.vy + 10 * r2.vx + r2.vy

        collision.config.set_param_distribution("severity", CustomDistribution("integer", sev))
        e1 = Experiment(world)
        reclist = []

        def recorder(w, **kwargs):
            reclist.append(kwargs)

        e1.step_listeners.append(recorder)
        e1.run(max_world_time=0)
        print(reclist)
开发者ID:salmatoolkit,项目名称:salma,代码行数:33,代码来源:event_scheduling_test.py

示例4: test_event_instance_scheduled_only_once

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_event_instance_scheduled_only_once(self):
        world = World.instance()

        rob1 = self.create_random_walk_robot("rob1")
        rob2 = self.create_random_walk_robot("rob2")

        world.add(rob1, rob2)

        world.initialize(False)
        world.deactivate_info_transfer()

        world.world_width = 500
        world.world_height = 500
        world.safety_distance = 10
        self.init_robot(rob1, 250, 250, 0, 0)
        self.init_robot(rob2, 300, 250, 0, 0)
        welcome = world.get_exogenous_action("welcome_message")
        welcome.config.occurrence_distribution = GeometricDistribution(0.1)
        welcome.config.uniform_param("code", (0, 10))

        e1 = Experiment(world)
        reclist = []

        def recorder(w, **kwargs):
            reclist.append(kwargs)

        e1.step_listeners.append(recorder)
        e1.run(max_world_time=100)
        print(reclist)
开发者ID:salmatoolkit,项目名称:salma,代码行数:31,代码来源:event_scheduling_test.py

示例5: test_procedure_call

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_procedure_call(self):

        world = World.instance()
        transportToX = Procedure("transportToX",
                                 [("r1", "robot"), ("i", "item"), ("targetX", "integer")],
                                 Sequence(
                                     [
                                         Act("grab", [Variable("r1"), Variable("i")]),
                                         While(EvaluationContext.TRANSIENT_FLUENT, "robotLeftFrom",
                                               [Variable("r1"), Variable("targetX")],
                                               Act("move_right", [Variable("r1")])),
                                         Act("drop", [Variable("r1"), Variable("i")])
                                     ]
                                 ))
        registry = ProcedureRegistry()
        registry.register_procedure(transportToX)

        controlProc = Procedure("main", [],
                                Sequence([
                                    ProcedureCall("transportToX", ["rob1", "coffee", 17])
                                ]))

        agent = Agent("rob1", "robot", controlProc, registry)
        world.addAgent(agent)
        world.addEntity(Entity("coffee", "item"))
        world.initialize(False)
        self.place_agents_in_column(10)
        self.setNoOneCarriesAnything()
        world.runUntilFinished()
        world.printState()
        self.assertEqual(world.get_fluent_value('xpos', ['rob1']), 17)
开发者ID:salmatoolkit,项目名称:salma,代码行数:33,代码来源:worldtest.py

示例6: perform_property_1_test

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def perform_property_1_test(self, x, x_loop_goal, x_prop_goal, time_limit):
        world = World.instance()

        proc = Procedure([
            Act("move_right", [SELF]),
            Wait("not moving(self)"),
            Act("paint", [SELF, "item1"]),
            While("xpos(self) < params[0]", [x_loop_goal], [
                Act("move_right", [SELF]),
                Wait("not moving(self)")
            ])
        ])
        rob1 = Agent("rob1", "robot", proc)
        world.addAgent(rob1)
        world.addEntity(Entity("item1", "item"))
        world.addEntity(Entity("item2", "item"))
        world.initialize(False)
        self.setNoOneCarriesAnything()
        self.place_agents_in_column(x=x)

        f = Forall("r", "robot",
                   Implies(
                       Occur("paint", ["r", "?"]),
                       Until("{time_limit}", True, "xpos(r) >= {x_prop_goal}")
                   ))
        print("Testing property: {}".format(str(f)))
        experiment = Experiment(world)
        experiment.step_listeners.append(lambda world, **args: print(world.get_fluent_value("xpos", ["rob1"])))
        experiment.property_collection.register_property("f", f, INVARIANT, time_limit=time_limit,
                                                         x_prop_goal=x_prop_goal)
        verdict, results = experiment.run()
        print("Verdict: " + str(verdict))
        print("Results: " + str(results))
        world.printState()
        return verdict, results
开发者ID:salmatoolkit,项目名称:salma,代码行数:37,代码来源:property_evaluation_test_01.py

示例7: test_evaluate_python_function

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_evaluate_python_function(self):
        world = World.instance()

        def myfunc1(a, b):
            return a * b

        def myfunc2(a, b, **ctx):
            return a * b * ctx["x"] * ctx["xpos"]("rob1")

        def myfunc3(a, b, x=None, xpos=None, ypos=None, **ctx):
            return a * b * x * xpos("rob1") * ypos("rob1")

        proc1 = OneShotProcess([
            Assign("x", lambda i: i ** 2, [3]),
            Assign("y", myfunc1, [4, 6]),
            Assign("z", myfunc2, [-1, 2]),
            Assign("z2", myfunc3, [-1, 2])
        ])

        agent = Agent("rob1", "robot", [proc1])
        world.addAgent(agent)

        world.initialize(False)
        self.initialize_robot("rob1", 10, 15, 0, 0)

        world.printState()

        experiment = Experiment(world)
        experiment.run_until_finished()

        self.assertEqual(proc1.current_evaluation_context.resolve(Variable("x"))[0], 9)
        self.assertEqual(proc1.current_evaluation_context.resolve(Variable("y"))[0], 24)
        self.assertEqual(proc1.current_evaluation_context.resolve(Variable("z"))[0], -1 * 2 * 9 * 10)
        self.assertEqual(proc1.current_evaluation_context.resolve(Variable("z2"))[0], -1 * 2 * 9 * 10 * 15)
开发者ID:salmatoolkit,项目名称:salma,代码行数:36,代码来源:basic_simulation_test.py

示例8: testIterate_python

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def testIterate_python(self):
        world = World.instance()
        items = []
        for i in range(5):
            item = Entity("item{}".format(i), "item")
            world.addEntity(item)
            items.append(item)

        seq1 = Sequence([
            Iterate(EvaluationContext.ITERATOR, items,
                    [("i", "item")],
                    Sequence([
                        Act("paint", [Entity.SELF, Variable("i")])
                    ])

            )
        ])

        agent = Agent("rob1", "robot", Procedure("main", [], seq1))
        world.addAgent(agent)

        world.initialize(False)
        self.setNoOneCarriesAnything()
        for item in items:
            world.set_fluent_value("painted", [item.id], False)

        world.runUntilFinished()
        world.printState()
        for item in items:
            self.assertTrue(world.get_fluent_value("painted", [item.id]))
开发者ID:salmatoolkit,项目名称:salma,代码行数:32,代码来源:worldtest.py

示例9: test_run_right_until_max_X_pos

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_run_right_until_max_X_pos(self):
        world = World.instance()

        w = While("robotLeftFrom",
                  [Entity.SELF, 100, "s0"],
                  [
                      Act("move_right", [Entity.SELF]),
                      Wait("not moving(self)")
                  ])

        agent = Agent("rob1", "robot", Procedure(w))
        world.addAgent(agent)
        world.initialize(False)
        self.initialize_robot("rob1", 10, 10, 0, 0)

        world.printState()
        experiment = Experiment(world)
        verdict, info = experiment.run_until_finished()
        print("Verdict: {}  after {} steps".format(verdict, info['steps']))

        print("----")
        world.printState()
        print("----\n\n")

        self.assertEqual(world.get_fluent_value("xpos", ["rob1"]), 100)
        self.assertEqual(verdict, constants.OK)
        self.assertTrue(450 <= world.getTime() <= 452)
        self.assertTrue(world.is_finished())
        print(info)
开发者ID:salmatoolkit,项目名称:salma,代码行数:31,代码来源:basic_simulation_test.py

示例10: test_variable_assignment

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_variable_assignment(self):
        world = World.instance()

        # run from (x,y) to (y,y)
        seq = Sequence([
            Assign("myY", "ypos", [Entity.SELF]),
            FunctionStatement(print_value, [Variable("myY")])
        ])
        w = While("robotLeftFrom",
                  [Entity.SELF, Variable("myY"), "s0"],
                  [
                      Act("move_right", [Entity.SELF]),
                      Wait("not moving(self)"),
                      Assign("myX", "xpos", [Entity.SELF]),
                      FunctionStatement(print_value, [Variable("myX")])
                  ])
        seq.add_child(w)
        proc1 = OneShotProcess(Procedure(seq))
        rob1 = Agent("rob1", "robot", [proc1])
        world.addAgent(rob1)

        world.initialize(False)

        self.initialize_robot("rob1", 10, 20, 0, 0)
        experiment = Experiment(world)
        experiment.run_until_finished()
        self.assertEqual(proc1.current_evaluation_context.resolve(Variable('myY'))[0], 20)
        self.assertEqual(proc1.current_evaluation_context.resolve(Variable('myX'))[0], 20)
开发者ID:salmatoolkit,项目名称:salma,代码行数:30,代码来源:basic_simulation_test.py

示例11: test_ordering

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_ordering(self):
        self.assertTrue(self.rob1 < self.rob2)
        self.assertFalse(self.rob2 < self.rob1)
        self.assertFalse(self.rob1 < self.rob1)
        self.assertTrue(self.item1 < self.rob1)
        self.assertTrue(self.item1 < Entity("item1b", "item"))
        self.assertTrue(self.item1 < Entity("item1", "itemb"))

        self.assertTrue(self.rob2 > self.rob1)
        self.assertFalse(self.rob1 > self.rob2)
        self.assertFalse(self.rob1 > self.rob1)

        self.assertTrue(self.rob2 >= self.rob1)
        self.assertFalse(self.rob1 >= self.rob2)
        self.assertTrue(self.rob1 >= self.rob1)

        self.assertTrue(self.rob1 <= self.rob2)
        self.assertFalse(self.rob2 <= self.rob1)
        self.assertTrue(self.rob1 <= self.rob1)

        self.assertTrue(self.rob1 == self.rob1)
        self.assertTrue(self.rob1 != self.rob2)
        self.assertFalse(self.rob1 == self.rob2)

        world = World.instance()
        self.assertListEqual(sorted(world.getDomain("robot") | world.getDomain("item")),
                             [self.item1, self.item2, self.item3, self.rob1, self.rob2])
开发者ID:salmatoolkit,项目名称:salma,代码行数:29,代码来源:entity_test.py

示例12: test_set_agent_properties

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_set_agent_properties(self):
        world = World.instance()
        self.rob1.xpos = 200
        self.rob1.ypos = 300
        self.rob2.xpos = 250
        self.rob2.ypos = 350

        self.assertEqual(world.get_fluent_value("xpos", ["rob1"]), 200)
        self.assertEqual(world.get_fluent_value("ypos", ["rob1"]), 300)
        self.assertEqual(world.get_fluent_value("xpos", ["rob2"]), 250)
        self.assertEqual(world.get_fluent_value("ypos", ["rob2"]), 350)

        self.rob1.robot_radius = 25
        self.rob2.robot_radius = 55
        self.assertEqual(world.getConstantValue("robot_radius", ["rob1"]), 25)
        self.assertEqual(world.getConstantValue("robot_radius", ["rob2"]), 55)

        world.gravity = 42.123
        self.assertEqual(world.getConstantValue("gravity", []), 42.123)

        self.assertFalse(world.get_fluent_value("carrying", ["rob1", "item2"]))
        self.rob1.set_carrying(self.item2, True)
        self.assertTrue(world.get_fluent_value("carrying", ["rob1", "item2"]))

        self.rob1.set_vx(42)
        self.assertTrue(world.get_fluent_value("vx", ["rob1"]), 42)

        world.set_gravity(4711)
        self.assertEqual(world.getConstantValue("gravity", []), 4711)
开发者ID:salmatoolkit,项目名称:salma,代码行数:31,代码来源:entity_test.py

示例13: test_get_agent_properties

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def test_get_agent_properties(self):
        world = World.instance()
        x = self.rob1.get_own_fluent_value("xpos")
        print(x)
        self.assertEqual(x, 10)
        print("rob1: x = {}, y = {}, vx = {}, vy = {}\n".format(self.rob1.xpos, self.rob1.ypos, self.rob1.vx,
                                                                self.rob1.vy))
        self.assertEqual(self.rob1.xpos, 10)
        self.assertEqual(self.rob1.ypos, 15)
        self.assertEqual(self.rob1.vx, 1)
        self.assertEqual(self.rob1.vy, -1)

        print("rob2: x = {}, y = {}, vx = {}, vy = {}\n".format(self.rob2.xpos, self.rob2.ypos, self.rob2.vx,
                                                                self.rob2.vy))
        self.assertEqual(self.rob2.xpos, 100)
        self.assertEqual(self.rob2.ypos, 150)
        self.assertEqual(self.rob2.vx, 0)
        self.assertEqual(self.rob2.vy, 0)

        self.assertTrue(self.rob1.carrying(self.item1))
        self.assertTrue(self.rob2.carrying(self.item2))
        self.assertFalse(self.rob1.carrying(self.item2))
        self.assertFalse(self.rob2.carrying(self.item1))
        self.assertFalse(self.rob2.carrying("item1"))

        # derived fluents
        print("rob1.dist_from_origin = {}".format(self.rob1.dist_from_origin))
        self.assertEqual(self.rob1.dist_from_origin, sqrt(self.rob1.xpos ** 2 + self.rob1.ypos ** 2))
        self.assertTrue(self.rob1.moving)
        self.assertFalse(self.rob2.moving)
        # constants
        self.assertEqual(self.rob1.robot_radius, 5)
        self.assertEqual(self.rob2.robot_radius, 8)
        self.assertEqual(world.gravity, 9.81)
开发者ID:salmatoolkit,项目名称:salma,代码行数:36,代码来源:entity_test.py

示例14: setNoOneCarriesAnything

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
 def setNoOneCarriesAnything(self):
     world = World.instance()
     robots = world.getDomain('robot')
     items = world.getDomain('item')
     for r in robots:
         for i in items:
             world.set_fluent_value('carrying', [r.id, i.id], False)
开发者ID:salmatoolkit,项目名称:salma,代码行数:9,代码来源:world_test_base.py

示例15: testUniformStochasticAction

# 需要导入模块: from salma.model.world import World [as 别名]
# 或者: from salma.model.world.World import instance [as 别名]
    def testUniformStochasticAction(self):
        world = World.instance()

        seq = Sequence([
            Act("move_right", [Entity.SELF]),
            Assign("i", 0),
            While("i < 100", [
                Act("jump", [Entity.SELF, 42]),
                Assign("i", "i + 1")
            ])
        ])

        agent = Agent("rob1", "robot", Procedure(seq))
        world.addAgent(agent)
        world.initialize(False)

        jump_action = world.get_stochastic_action("jump")
        self.generate_outcomes(jump_action)
        jump_action.selection_strategy = NonDeterministic()

        self.initialize_robot("rob1", 10, 20, 0, 0)

        world.printState()
        self.__crash_count = 0
        self.__land_on_count = 0
        experiment = Experiment(world)
        experiment.step_listeners.append(self.record_outcomes)
        experiment.run_until_finished()
        world.printState()
        print("crash: {} - land on: {}".format(self.__crash_count, self.__land_on_count))
        self.assertTrue(self.__crash_count > 40)
        self.assertTrue(self.__land_on_count > 40)
        self.assertEqual(self.__crash_count + self.__land_on_count, 100)
开发者ID:salmatoolkit,项目名称:salma,代码行数:35,代码来源:basic_simulation_test.py


注:本文中的salma.model.world.World.instance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。