Exemple #1
0
    def reset(self, env=None):
        self._dissociate()
        if env:
            self.set_env(env=env)
        self._action = None
        self._proposal_offer = None
        self._current_offer_offer = None

        if self.init_proposal:
            self._ip = Issue.sample(issues=[Issue(self._ip_range)],
                                    n_outcomes=1,
                                    astype=tuple)[0]
            self._rp = Issue.sample(issues=[Issue(self._rp_range)],
                                    n_outcomes=1,
                                    astype=tuple)[0]

            if "ip" in self.ufun.__dict__:
                self.ufun.ip = self._ip
                self.set_proposal_offer(self._ip)

            if "rp" in self.ufun.__dict__:
                self.ufun.rp = self._rp

        self.end_time = 1
Exemple #2
0
def test_drl_negotiator():
    from drl_negotiation.negotiator import MyDRLNegotiator, DRLNegotiator
    from drl_negotiation.utility_functions import MyUtilityFunction
    from negmas import SAOMechanism, Issue, ResponseType, Outcome
    from drl_negotiation.env import DRLNegotiationEnv, NEnv

    name = "drl_negotiator"
    issues = [
        Issue(values=10, name="quantity"),
        Issue(values=100, name="delivery_time"),
        Issue(values=100, name="unit_price")
    ]
    # buyer
    weights = (0, -0.5, -0.8)

    # set the utility function with MyUtilityFunction
    mechanism = SAOMechanism(issues=issues, n_steps=100)
    ufun = MyUtilityFunction(weights=weights, )

    drl_negotiator = MyDRLNegotiator(name=name, ufun=ufun)

    mechanism.add(drl_negotiator)

    drl_negotiator_two = MyDRLNegotiator(name=name + "_two")

    mechanism.add(drl_negotiator_two,
                  ufun=MyUtilityFunction(weights=(0, -0.2, -0.6)))
    assert drl_negotiator.name == name
    assert drl_negotiator.ufun == ufun

    # Test the basic attributes and functions
    # None
    assert drl_negotiator.env is None
    assert drl_negotiator.action is None

    #TODO: initial value and reserved value

    # Test the respond and propose of negotiator
    offer = Issue.sample(issues, 1)

    # Test acceptance strategy
    # Action: None, Env: None, Train: True
    # env is None, so the respond is always ResponseType.REJECT_OFFER
    drl_negotiator.train = True
    respond = drl_negotiator.respond(mechanism.state, offer[0])
    assert type(respond) == ResponseType
    if respond == ResponseType.REJECT_OFFER:
        proposal_offer = drl_negotiator.propose(mechanism.state)
        if proposal_offer is not None:
            assert proposal_offer in drl_negotiator.ami.outcomes
    else:
        assert False, "Reponse must be ResponseType.Reject_OFFER!"

    # Action: None, Env: not None, Train: True
    # Env will automatically set the issues, games and so on attributes.
    drl_negotiator.train = True
    name = "drl_negotiation_env"
    n_env = DRLNegotiationEnv(name=name)

    drl_negotiator.reset(env=n_env)
    assert isinstance(drl_negotiator.env, NEnv)
    respond = drl_negotiator.respond(mechanism.state, offer[0])
    assert type(
        respond) == ResponseType, "Type of response must be ResponseType!"

    # Action: not None, Env: not None
    drl_negotiator.reset(env=n_env)
    action = drl_negotiator.env.action_space.sample()

    drl_negotiator.set_current_action(action)
    assert type(drl_negotiator.action) == ResponseType
    assert type(
        respond) == ResponseType, "Type of response must be ResponseType!"

    respond = drl_negotiator.respond(mechanism.state, offer[0])
    assert respond == drl_negotiator.action