コード例 #1
0
def test_alternating_offers_mechanism_fails_on_no_offerer():
    p = SAOMechanism(
        outcomes=10,
        n_steps=10,
        dynamic_entry=False,
        publish_n_acceptances=True,
        publish_proposer=True,
    )
    to_be_offered = [(0, ), (1, ), (2, )]
    to_be_accepted = [(2, )]
    a1 = LimitedOutcomesAcceptor(acceptable_outcomes=to_be_offered)
    a2 = LimitedOutcomesAcceptor(acceptable_outcomes=to_be_accepted)
    p.add(a1, ufun=MappingUtilityFunction(lambda x: x[0] + 1.0))
    p.add(a2, ufun=MappingUtilityFunction(lambda x: x[0] + 1.0))
    try:
        p.run()
    except RuntimeError:
        pass
    a1offers = [
        s.current_offer for s in p.history if s.current_proposer == a1.id
    ]
    a2offers = [
        s.current_offer for s in p.history if s.current_proposer == a2.id
    ]
    assert len(p.history) > 0
    assert len(a2offers) == 0, "acceptor did not offer"
    assert len(a1offers) == 0, "acceptor did not offer"
    assert p.agreement is None, "no agreement"
コード例 #2
0
    def ranking_error(
        self,
        gt: Union[List[float], Dict[Outcome, float], UtilityFunction],
        tolerance: float = 0.0,
    ) -> float:
        """
        Evaluates the utility function against gt counting differences in ranking only

        Args:

            gt: Ground truth ufun
            tolerance: a small tolerance when finding if the ranking is respected

        Returns:

            A value between zero and one indicating the ranking error
        """
        if isinstance(gt, list):
            gt = dict(zip(self.outcomes, gt))
        if not isinstance(gt, UtilityFunction):
            gt = MappingUtilityFunction(gt)
        rank = ranking(self, self.outcomes)
        return sum(
            int(float(gt(w1)) + tolerance < float(gt(w2)))
            for w1, w2 in zip(rank[:-1], rank[1:])) / (len(self.outcomes) - 1)
コード例 #3
0
ファイル: test_sao.py プロジェクト: k-ninagawa/negmas
def test_exceptions_are_saved():
    n_outcomes, n_negotiators = 10, 2
    mechanism = SAOMechanism(outcomes=n_outcomes,
                             n_steps=n_outcomes,
                             ignore_negotiator_exceptions=True)
    ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                   outcomes=mechanism.outcomes)
    mechanism.add(AspirationNegotiator(name=f"agent{0}"), ufun=ufuns[0])
    mechanism.add(MyRaisingNegotiator(name=f"agent{1}"), ufun=ufuns[1])
    assert mechanism.state.step == 0
    mechanism.step()
    mechanism.step()
    mechanism.step()
    assert mechanism.state.step == 1
    assert mechanism._current_offer is not None
    assert len(mechanism.stats) == 3
    stats = mechanism.stats
    assert "times" in stats.keys()
    assert "exceptions" in stats.keys()
    assert stats["exceptions"] is not None
    assert len(stats["exceptions"]) == 1
    print(stats["exceptions"][mechanism.negotiators[1].id])
    assert len(stats["exceptions"][mechanism.negotiators[1].id]) == 1
    assert exception_str in stats["exceptions"][mechanism.negotiators[1].id][0]
    assert len(stats["exceptions"][mechanism.negotiators[0].id]) == 0
コード例 #4
0
ファイル: test_sao.py プロジェクト: k-ninagawa/negmas
def test_times_are_calculated(n_outcomes, n_negotiators, n_steps):
    mechanism = SAOMechanism(outcomes=n_outcomes, n_steps=8)
    ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                   outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[i])
    assert mechanism.state.step == 0
    _strt = time.perf_counter()
    for _ in range(n_steps):
        print(f"Stepping: {_}")
        mechanism.step()
    time.sleep(0.01)
    duration = time.perf_counter() - _strt
    # assert mechanism.current_step == n_steps
    assert mechanism._current_offer is not None
    assert len(mechanism.stats) == 3
    stats = mechanism.stats
    assert "round_times" in stats.keys()
    assert 0 < sum(stats["round_times"]) < duration
    assert "times" in stats.keys()
    assert "exceptions" in stats.keys()
    assert stats["times"] is not None
    assert stats["exceptions"] is not None
    assert len(stats["times"]) == n_negotiators
    assert len(stats["exceptions"]) == 0
    for i in range(n_negotiators):
        assert 0 < stats["times"][mechanism.negotiators[i].id] < duration
        assert len(stats["exceptions"][mechanism.negotiators[i].id]) == 0
    assert 0 < sum(stats["times"].values()) < duration
コード例 #5
0
def test_pickling_mechanism(tmp_path):
    import pickle

    file = tmp_path / "mechanism.pck"
    n_outcomes, n_negotiators = 5, 3
    mechanism = SAOMechanism(
        outcomes=n_outcomes,
        n_steps=3,
        offering_is_accepting=True,
        avoid_ultimatum=False,
    )
    ufuns = MappingUtilityFunction.generate_random(n_negotiators, outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[i])

    assert mechanism.state.step == 0
    with open(file, "wb") as f:
        pickle.dump(mechanism, f)
    with open(file, "rb") as f:
        pickle.load(f)
    assert mechanism.state.step == 0
    mechanism.step()
    with open(file, "wb") as f:
        pickle.dump(mechanism, f)
    with open(file, "rb") as f:
        pickle.load(f)
    assert mechanism.state.step == 1
コード例 #6
0
def test_mechanism_can_run(n_negotiators):
    n_outcomes = 5
    mechanism = SAOMechanism(outcomes=n_outcomes, n_steps=3)
    ufuns = MappingUtilityFunction.generate_random(n_negotiators, outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[i])
    assert mechanism.state.step == 0
    mechanism.step()
    mechanism.run()
コード例 #7
0
ファイル: test_sao.py プロジェクト: k-ninagawa/negmas
def test_loops_are_broken(keep_order):
    """Tests that loops formed by concurrent negotiations are broken for syn controllers"""
    from negmas.mechanisms import Mechanism
    from negmas.sao import SAOSingleAgreementAspirationController

    a, b, c = (
        SAOSingleAgreementAspirationController(
            ufun=MappingUtilityFunction(lambda x: x["price"]), strict=False),
        SAOSingleAgreementAspirationController(
            ufun=MappingUtilityFunction(lambda x: x["price"]), strict=False),
        SAOSingleAgreementAspirationController(
            ufun=MappingUtilityFunction(lambda x: x["price"]), strict=False),
    )

    n1 = SAOMechanism(
        name="ab",
        issues=[Issue((0.0, 1.0), "price")],
        n_steps=50,
        outcome_type=dict,
    )
    n2 = SAOMechanism(
        name="ac",
        issues=[Issue((0.0, 1.0), "price")],
        n_steps=50,
        outcome_type=dict,
    )
    n3 = SAOMechanism(
        name="bc",
        issues=[Issue((0.0, 1.0), "price")],
        n_steps=50,
        outcome_type=dict,
    )

    n1.add(a.create_negotiator(name="a>b"))
    n1.add(b.create_negotiator(name="b>a"))
    n2.add(a.create_negotiator(name="a>c"))
    n2.add(c.create_negotiator(name="c>a"))
    n3.add(b.create_negotiator(name="b>c"))
    n3.add(c.create_negotiator(name="c>b"))
    negs = [n1, n2, n3]
    Mechanism.runall(negs, keep_order)

    agreements = [neg.state.agreement for neg in negs]
    assert sum(_ is not None for _ in agreements) > 0
コード例 #8
0
def test_round_n_agents(n_negotiators):
    n_outcomes = 5
    mechanism = SAOMechanism(outcomes=n_outcomes, n_steps=3)
    ufuns = MappingUtilityFunction.generate_random(n_negotiators, outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[i])
    assert mechanism.state.step == 0
    mechanism.step()
    assert mechanism.state.step == 1
    assert mechanism._current_offer is not None
コード例 #9
0
def test_ga_mechanism(n_negotiators, n_outcomes):
    mechanism = GAMechanism(outcomes=n_outcomes, n_steps=3)
    ufuns = MappingUtilityFunction.generate_random(n_negotiators, outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(SorterNegotiator(name=f"agent{i}"), ufun=ufuns[i])
    assert mechanism.state.step == 0
    mechanism.step()
    assert mechanism.state.step == 1
    assert mechanism.dominant_outcomes is not None
    mechanism.run()
コード例 #10
0
ファイル: test_st.py プロジェクト: k-ninagawa/negmas
def test_hill_climbing_mechanism(n_negotiators, n_outcomes):
    mechanism = HillClimbingSTMechanism(outcomes=n_outcomes, n_steps=3)
    ufuns = MappingUtilityFunction.generate_random(n_negotiators, outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(BinaryComparatorNegotiator(name=f"agent{i}"), ufun=ufuns[i])
    assert mechanism.state.step == 0
    mechanism.step()
    assert mechanism.state.step == 1
    assert mechanism.current_offer is not None
    mechanism.run()
    assert mechanism.agreement is not None
コード例 #11
0
 def _respond_to_negotiation_request(
     self,
     initiator: str,
     partners: List[str],
     issues: List[Issue],
     annotation: Dict[str, Any],
     mechanism: AgentMechanismInterface,
     role: Optional[str],
     req_id: Optional[str],
 ) -> Optional[Negotiator]:
     negotiator = AspirationNegotiator(ufun=MappingUtilityFunction(
         mapping=lambda x: 1.0 - x["i1"] / 10.0))
     return negotiator
コード例 #12
0
ファイル: test_sao.py プロジェクト: k-ninagawa/negmas
def test_checkpointing_mechanism(tmp_path):
    file = tmp_path
    n_outcomes, n_negotiators = 5, 3
    mechanism = SAOMechanism(
        outcomes=n_outcomes,
        n_steps=3,
        offering_is_accepting=True,
        avoid_ultimatum=False,
    )
    ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                   outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[i])

    assert mechanism.state.step == 0
    file_name = mechanism.checkpoint(file)

    info = SAOMechanism.checkpoint_info(file_name)
    assert isinstance(info["time"], str)
    assert info["step"] == 0
    assert info["type"].endswith("SAOMechanism")
    assert info["id"] == mechanism.id
    assert info["name"] == mechanism.name

    mechanism, info = SAOMechanism.from_checkpoint(file_name, return_info=True)
    assert isinstance(info["time"], str)
    assert info["step"] == 0
    assert info["type"].endswith("SAOMechanism")
    assert info["id"] == mechanism.id
    assert info["name"] == mechanism.name

    assert mechanism.state.step == 0
    mechanism.step()

    file_name = mechanism.checkpoint(file)

    info = SAOMechanism.checkpoint_info(file_name)
    assert isinstance(info["time"], str)
    assert info["step"] == 1
    assert info["type"].endswith("SAOMechanism")
    assert info["id"] == mechanism.id
    assert info["name"] == mechanism.name

    mechanism, info = SAOMechanism.from_checkpoint(file_name, return_info=True)
    assert isinstance(info["time"], str)
    assert info["step"] == 1
    assert info["type"].endswith("SAOMechanism")
    assert info["id"] == mechanism.id
    assert info["name"] == mechanism.name

    mechanism.run()
コード例 #13
0
ファイル: test_sao.py プロジェクト: k-ninagawa/negmas
def test_auto_checkpoint(tmp_path, single_checkpoint, checkpoint_every,
                         exist_ok):
    import shutil

    new_folder: Path = tmp_path / unique_name("empty", sep="")
    new_folder.mkdir(parents=True, exist_ok=True)
    shutil.rmtree(new_folder)
    new_folder.mkdir(parents=True, exist_ok=True)
    filename = "mechanism"

    n_outcomes, n_negotiators = 5, 3
    n_steps = 50
    mechanism = SAOMechanism(
        outcomes=n_outcomes,
        n_steps=n_steps,
        offering_is_accepting=True,
        avoid_ultimatum=False,
        checkpoint_every=checkpoint_every,
        checkpoint_folder=new_folder,
        checkpoint_filename=filename,
        extra_checkpoint_info=None,
        exist_ok=exist_ok,
        single_checkpoint=single_checkpoint,
    )
    ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                   outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(
            AspirationNegotiator(name=f"agent{i}"),
            ufun=ufuns[i],
            aspiration_type="conceder",
        )

    mechanism.run()

    if 0 < checkpoint_every <= n_steps:
        if single_checkpoint:
            assert len(list(new_folder.glob("*"))) == 2
        else:
            assert len(list(new_folder.glob("*"))) >= 2 * (max(
                1, mechanism.state.step // checkpoint_every))
    elif checkpoint_every > n_steps:
        assert len(list(new_folder.glob("*"))) == 2
    else:
        assert len(list(new_folder.glob("*"))) == 0
コード例 #14
0
def test_mechanism_runall(n_negotiators, oia):
    n_outcomes = 5
    mechanisms = []
    for _ in range(10):
        mechanism = SAOMechanism(
            outcomes=n_outcomes,
            n_steps=random.randint(3, 20),
            offering_is_accepting=oia,
            avoid_ultimatum=False,
        )
        ufuns = MappingUtilityFunction.generate_random(1, outcomes=n_outcomes)
        for i in range(n_negotiators):
            mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[0])
        mechanisms.append(mechanism)

    states = SAOMechanism.runall(mechanisms)
    assert len(states) == 10
    assert not any(_.running for _ in states)
コード例 #15
0
def test_mechanism_runs_with_offering_not_accepting(n_negotiators, oia):
    n_outcomes = 5
    mechanism = SAOMechanism(
        outcomes=n_outcomes, n_steps=3, offering_is_accepting=oia, avoid_ultimatum=False
    )
    ufuns = MappingUtilityFunction.generate_random(1, outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[0])
    assert mechanism.state.step == 0
    mechanism.step()
    assert mechanism._current_proposer.name == "agent0"
    assert mechanism._n_accepting == n_negotiators + int(oia) - 1
    assert (mechanism.agreement is not None) is oia
    if mechanism.agreement is not None:
        return
    mechanism.step()
    assert mechanism._current_proposer.name == "agent0"
    assert mechanism._n_accepting == n_negotiators
    assert mechanism.agreement is not None
コード例 #16
0
def test_lap_ufun_full_ranking():
    issues = [Issue(5, "Price"), Issue(5, "Distance")]
    gt = {(o["Price"], o["Distance"]): 0.2 * o["Price"] - 0.45 * o["Distance"]
          for o in Issue.enumerate(issues, astype=dict)}
    full_ranking = [
        _[0] for _ in sorted(
            zip(gt.keys(), gt.values()), key=lambda x: x[1], reverse=True)
    ]
    for kind in ("error_sums", "errors"):
        ufun = RankingLAPUfunLearner(issues=issues, degree=1, kind=kind)
        ufun.fit(ranking_list=full_ranking)
        learned_ranking = [
            _[0] for _ in sorted(
                zip(ufun.uvals.keys(), ufun.uvals.values()),
                key=lambda x: x[1],
                reverse=True,
            )
        ]
        if kind == "error_sums":
            assert full_ranking == learned_ranking, f"Failed on {kind}"
            assert (ufun.ranking_error(gt=MappingUtilityFunction(
                lambda o: 0.2 * o[0] - 0.45 * o[1])) == 0.0)
コード例 #17
0
ファイル: test_sao.py プロジェクト: k-ninagawa/negmas
def test_sync_controller(n_negotiations, n_negotiators, oia):
    n_outcomes = 2

    mechanisms = []
    controller = MySAOSync()
    for i in range(n_negotiators):
        mechanisms.append(
            SAOMechanism(
                outcomes=n_outcomes,
                n_steps=5,
                offering_is_accepting=oia,
                avoid_ultimatum=False,
            ))
        ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                       outcomes=n_outcomes)
        for i in range(n_negotiators):
            mechanisms[-1].add(AspirationNegotiator(name=f"agent{i}"),
                               ufun=ufuns[i])

        mechanisms[-1].add(controller.create_negotiator())

    states = SAOMechanism.runall(mechanisms)
    assert all(_.agreement is not None for _ in states)
コード例 #18
0
    def value_error(
        self, gt: Union[List[float], Dict[Outcome, float], UtilityFunction]
    ) -> Tuple[float, float]:
        """
        Finds the error in ufun values against gt (after normalization from 0-1)

        Args:
            gt: Ground truth

        Returns:

            Mean and std. dev of errors

        """
        if isinstance(gt, list):
            gt = dict(zip(self.outcomes, gt))
        if not isinstance(gt, UtilityFunction):
            gt = MappingUtilityFunction(gt)
        data = np.array([[float(gt(outcome)),
                          float(self(outcome))] for outcome in self.outcomes])
        mx, mn = data.max(axis=0), data.min(axis=0)
        data = (data - mn) / (mx - mn)
        err = (data[:, 0] - data[:, 1])**2
        return float(np.sqrt(np.mean(err))), float(np.std(err))
コード例 #19
0
ファイル: nmv_agent.py プロジェクト: ayansengupta17/scml
    def init(self):
        """Called once after the agent-world interface is initialized"""

        # We assume the agent only takes one kind of input.
        if len(self.consuming.keys()) != 1:
            raise Exception('The agent is design to consume only one input')
        self.input_index = list(self.consuming.keys())[0]

        # We assume the agent only produced one kind of output.
        if len(self.producing.keys()) != 1:
            raise Exception('The agent is design to produce only one output')
        self.output_index = list(self.producing.keys())[0]

        self.production_cost = self.line_profiles[0][0].cost
        self.num_intermediate_products = len(self.awi.processes) - 1

        # Compute Expected Catalog Prices
        self.expected_catalog_prices[0] = 1.0
        for p in range(1, self.num_intermediate_products + 2):
            self.expected_catalog_prices[p] = 1.15 * (self.expected_catalog_prices[p - 1] + 2.5)

        # Initialize book keeping structures.
        for p in range(0, self.num_intermediate_products + 2):
            self.storage_history[p] = []
            self.contracted_buys_at_t[p], self.contracted_sales_at_t[p], self.executed_buys_at_t[p], self.executed_sales_at_t[p] = {}, {}, {}, {}
            self.signed_contracts_for_factory = {t: [] for t in range(0, self.awi.n_steps)}
            for t in range(0, self.awi.n_steps):
                self.contracted_buys_at_t[p][t] = 0
                self.contracted_sales_at_t[p][t] = 0
                self.executed_buys_at_t[p][t] = (0, 0)
                self.executed_sales_at_t[p][t] = (0, 0)

        # Initialize the negotiator that will negotiate for inputs
        self.input_negotiator_ufun = MappingUtilityFunction(mapping=lambda outcome: 1 - outcome['unit_price'], reserved_value=INVALID_UTILITY)

        # Initialize the negotiator that will negotiate for outputs
        self.output_negotiator_ufun = MappingUtilityFunction(
            mapping=lambda outcome: (math.exp(outcome['unit_price']) - 1.5) * outcome['quantity'] if outcome["unit_price"] > 0.0 else INVALID_UTILITY)

        # Set the time limit for posting CFPs.
        self.limit_post_cfps = self.awi.n_steps - 16

        # Set the time limit to sign CFPs to buy input
        self.limit_sign_time_input_product = self.awi.n_steps - 10

        # Initialize the brain of the agent. The brain takes in the input product, the output product, cost of production and num_intm_products.
        self.agent_brain = AgentBrain(game_length=self.awi.n_steps,
                                      input_product_index=self.input_index,
                                      output_product_index=self.output_index,
                                      production_cost=self.production_cost,
                                      num_intermediate_products=self.num_intermediate_products,
                                      verbose=self.verbose)

        # Register interest in all products.
        self.awi.unregister_interest([self.input_index, self.output_index])
        self.awi.register_interest([p for p in range(0, self.num_intermediate_products + 2)])

        if self.verbose:
            # Print some init info for informational purposes
            print(f'\n+++++++++++++++++++++++++++++++++++++++\n'
                  f'Starting game with a total of {self.awi.n_steps} steps\n'
                  f'\t Expected catalog prices = {self.expected_catalog_prices}\n'
                  f'\t There are {self.num_intermediate_products} intermediate products. \n'
                  f'\t SCML2020World processes = {self.awi.processes}\n'
                  f'\t My Cost = {self.line_profiles[0][0].cost}\n'
                  f'\t I, {self.id}, consume {self.input_index} and produce {self.output_index}\n'
                  f'\t Is the middle man active? {self.middle_man_active}\n'
                  f'+++++++++++++++++++++++++++++++++++++++\n')

            print(f'Parameters: '
                  f'\n\t hyper_parameter_optimization = {self.hyper_parameter_optimization}'
                  f'\n\t agent_aspiration_type = {self.agent_aspiration_type}'
                  f'\n\t limit_sign_storage = {self.limit_sign_storage}'
                  f'\n\t limit_number_sales_contracts_at_t = {self.limit_number_sales_contracts_at_t}'
                  f'\n\t limit_number_buys_contracts_at_t = {self.limit_number_buys_contracts_at_t}'
                  f'\n\t cfp_qtty_range_width = {self.cfp_qtty_range_width}'
                  f'\n ------------------------------------')

        # Which products will the middle man buy and sell?
        self.middle_man_products = {p for p in range(0, self.num_intermediate_products + 2) if p != self.input_index and p != self.output_index}
        if self.verbose:
            print(f'The middle man can buy and sell {self.middle_man_products}')
コード例 #20
0
def test_can_run_from_checkpoint(tmp_path, single_checkpoint, checkpoint_every,
                                 exist_ok):
    import shutil

    new_folder: Path = tmp_path / unique_name("empty", sep="")
    second_folder: Path = tmp_path / unique_name("second", sep="")
    new_folder.mkdir(parents=True, exist_ok=True)
    shutil.rmtree(new_folder)
    new_folder.mkdir(parents=True, exist_ok=True)
    filename = "mechanism"
    second_folder.mkdir(parents=True, exist_ok=True)

    n_outcomes, n_negotiators = 5, 3
    n_steps = 50
    mechanism = SAOMechanism(
        outcomes=n_outcomes,
        n_steps=n_steps,
        offering_is_accepting=True,
        avoid_ultimatum=False,
        checkpoint_every=checkpoint_every,
        checkpoint_folder=new_folder,
        checkpoint_filename=filename,
        extra_checkpoint_info=None,
        exist_ok=exist_ok,
        single_checkpoint=single_checkpoint,
    )
    ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                   outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(AspirationNegotiator(name=f"agent{i}"), ufun=ufuns[i])

    mechanism.run()
    files = list(new_folder.glob("*"))
    if 0 < checkpoint_every <= n_steps:
        if single_checkpoint:
            assert len(list(new_folder.glob("*"))) == 2
        else:
            assert len(list(new_folder.glob("*"))) >= 2 * (max(
                1, mechanism.state.step // checkpoint_every))
    elif checkpoint_every > n_steps:
        assert len(list(new_folder.glob("*"))) == 2
    else:
        assert len(list(new_folder.glob("*"))) == 0

    runner = CheckpointRunner(folder=new_folder)

    assert len(runner.steps) * 2 == len(files)
    assert runner.current_step == -1
    assert runner.loaded_object is None

    runner.step()

    assert runner.current_step == (0 if not single_checkpoint else
                                   runner.steps[-1])
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.reset()
    assert len(runner.steps) * 2 == len(files)
    assert runner.current_step == -1
    assert runner.loaded_object is None

    runner.goto(runner.last_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.next_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.previous_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.first_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.reset()

    runner.run()
コード例 #21
0
def test_can_run_from_checkpoint(tmp_path, checkpoint_every, exist_ok, copy,
                                 fork_after_reset):
    import shutil

    new_folder: Path = tmp_path / unique_name("empty", sep="")
    second_folder: Path = tmp_path / unique_name("second", sep="")
    new_folder.mkdir(parents=True, exist_ok=True)
    shutil.rmtree(new_folder)
    new_folder.mkdir(parents=True, exist_ok=True)
    second_folder.mkdir(parents=True, exist_ok=True)
    shutil.rmtree(second_folder)
    second_folder.mkdir(parents=True, exist_ok=True)

    filename = "mechanism"

    n_outcomes, n_negotiators = 5, 3
    n_steps = 50
    mechanism = SAOMechanism(
        outcomes=n_outcomes,
        n_steps=n_steps,
        offering_is_accepting=True,
        avoid_ultimatum=False,
        checkpoint_every=checkpoint_every,
        checkpoint_folder=new_folder,
        checkpoint_filename=filename,
        extra_checkpoint_info=None,
        exist_ok=exist_ok,
        single_checkpoint=False,
    )
    ufuns = MappingUtilityFunction.generate_random(n_negotiators,
                                                   outcomes=n_outcomes)
    for i in range(n_negotiators):
        mechanism.add(
            AspirationNegotiator(name=f"agent{i}"),
            ufun=ufuns[i],
            aspiration_type="conceder",
        )

    mechanism.run()
    files = list(new_folder.glob("*"))
    if 0 < checkpoint_every <= n_steps:
        assert len(list(new_folder.glob("*"))) >= 2 * (max(
            1, mechanism.state.step // checkpoint_every))
    elif checkpoint_every > n_steps:
        assert len(list(new_folder.glob("*"))) == 2
    else:
        assert len(list(new_folder.glob("*"))) == 0

    runner = CheckpointRunner(folder=new_folder)

    assert len(runner.steps) * 2 == len(files)
    assert runner.current_step == -1
    assert runner.loaded_object is None

    runner.step()

    assert runner.current_step == 0
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.reset()
    assert len(runner.steps) * 2 == len(files)
    assert runner.current_step == -1
    assert runner.loaded_object is None

    runner.goto(runner.last_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.next_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.previous_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.first_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.reset()

    if fork_after_reset:
        m = runner.fork(copy_past_checkpoints=copy, folder=second_folder)
        assert m is None
        return
    runner.step()
    m = runner.fork(copy_past_checkpoints=copy, folder=second_folder)

    if copy:
        step = runner.current_step
        assert len(list(second_folder.glob("*"))) >= 2
        assert len(list(second_folder.glob(f"*{step}.mechanism"))) > 0
    else:
        assert len(list(second_folder.glob("*"))) == 0

    assert isinstance(m, SAOMechanism)
    step = m.current_step
    m.step()
    assert m.current_step == step + 1

    state = m.run()
    assert state.agreement is not None

    runner.reset()
    assert len(runner.steps) * 2 == len(files)
    assert runner.current_step == -1
    assert runner.loaded_object is None

    runner.goto(runner.last_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.next_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.previous_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.goto(runner.first_step, exact=True)
    assert isinstance(runner.loaded_object, SAOMechanism)
    assert runner.loaded_object.state.step == runner.current_step

    runner.reset()

    runner.run()
コード例 #22
0
        if self.ami:
            # the ami is not None, means, can use the factor defined by user!
            # return self.delta**(getattr(self.ami.state, self.factor) - 1) * sum(w * v for w, v in zip(self.weights, offer))
            return sum(w * v for w, v in zip(self.weights, offer))
        else:
            return sum(w * v for w, v in zip(self.weights, offer))

    def xml(self, issues: List[Issue]) -> str:
        output = ""
        keys = list(ikeys(issues))
        for i, k in enumerate(keys):
            issue_name = iget(issues, k).name
            output += f'<issue index="{i+1}" etype="discrete" type="discrete" vtype="discrete" name="{issue_name}">\n'
            vals = iget(issues, k).all
            for indx, u in enumerate(vals):
                output += (
                    f'    <item index="{indx+1}" value="{u}" evaluation="{u}" />\n'
                )
            output += "</issue>\n"
        for i, k in enumerate(keys):
            output += (
                f'<weight index="{i+1}" value="{iget(self.weights, k)}">\n</weight>\n'
            )
        return output

    def __str__(self):
        return f"w: {self.weights}, delta: {self.delta}, factor: {self.factor}"


MyOpponentUtilityFunction = MappingUtilityFunction(
    lambda x: random.random() * x[0])