import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; import geniusweb.actions.*; import geniusweb.boa.InstantiationFailedException; import geniusweb.boa.acceptancestrategy.AcceptanceStrategy; import geniusweb.boa.acceptancestrategy.TimeDependentAcceptanceStrategy; import geniusweb.boa.biddingstrategy.BiddingStrategy; import geniusweb.boa.biddingstrategy.TimeDependentBiddingStrategy; import geniusweb.inform.*; import geniusweb.issuevalue.Bid; import geniusweb.opponentmodel.FrequencyOpponentModel; import geniusweb.opponentmodel.OpponentModel; import geniusweb.party.Capabilities; import geniusweb.party.DefaultParty; import geniusweb.profile.Profile; import geniusweb.profileconnection.ProfileConnectionFactory; import geniusweb.profileconnection.ProfileInterface; import tudelft.utilities.logging.Reporter; public class DefaultMopacBoa extends DefaultParty { private Group5BoaState negoState; private ProfileInterface profileint = null; private Votes lastVotes; public DefaultMopacBoa() { super(); negoState = initialState(); } public DefaultMopacBoa(Reporter reporter) { super(reporter); // for debugging negoState = initialState(); } @Override public Capabilities getCapabilities() { return new Capabilities(Collections.singleton("MOPAC"), Collections.singleton(Profile.class)); } @Override public void notifyChange(Inform info) { try { if (info instanceof Settings) { /* * this should be first call. We can not yet create the state * because we need to wait for the profile fetch. */ Settings settings = (Settings) info; Class omClass = getOpponentModel( settings); BiddingStrategy bidStrat = getBiddingStrategy(settings); AcceptanceStrategy acceptStrat = getAccceptanceStrategy( settings); negoState = negoState.with(settings, bidStrat, acceptStrat, omClass); this.profileint = ProfileConnectionFactory .create(settings.getProfile().getURI(), getReporter()); return; } // Beyond this point we should have a profile if (negoState.getProfile() == null) { negoState = negoState.with(this.profileint.getProfile()); } if (info instanceof ActionDone) { // All actions already have been updated in the voting phase // negoState = negoState.with(((ActionDone) info).getAction()); } else if (info instanceof YourTurn) { getConnection().send(getAction()); // we will receive ActionDone and update state then } else if (info instanceof Voting) { lastVotes = vote((Voting) info); getConnection().send(lastVotes); } else if (info instanceof OptIn) { getConnection().send(lastVotes); } else if (info instanceof Finished) { getReporter().log(Level.INFO, "Final outcome:" + info); } } catch (Exception e) { throw new RuntimeException("Failed to handle info " + info, e); } } /** * Factory method to create initial state. Override to insert mock * * @return initial BoaState */ protected Group5BoaState initialState() { return new Group5BoaState(getReporter()); } protected Class getOpponentModel(Settings settings) throws InstantiationFailedException { return FrequencyOpponentModel.class; } protected BiddingStrategy getBiddingStrategy(Settings settings) throws InstantiationFailedException { return new TimeDependentBiddingStrategy(); } protected AcceptanceStrategy getAccceptanceStrategy(Settings settings) throws InstantiationFailedException { return new TimeDependentAcceptanceStrategy(); } @Override public String getDescription() { return "BOA implementation of Group 5."; } /** * @return the most recent bid that was offered, or null if no offer has * been done yet. */ protected Bid getLastBid() { List actions = negoState.getActionHistory(); for (int n = actions.size() - 1; n >= 0; n--) { Action action = actions.get(n); if (action instanceof Offer) { return ((Offer) action).getBid(); } } return null; } private Action getAction() { Bid lastBid = getLastBid(); if (lastBid != null && negoState.isAcceptable(lastBid)) { return new Accept(negoState.getSettings().getID(), lastBid); } return negoState.getAction(); } /** * @param voting the {@link Voting} object containing the options * * @return our next Votes. */ private Votes vote(Voting voting) throws IOException, InstantiationFailedException { for (Offer offer : voting.getBids()) { negoState = negoState.with(offer); } Object val = negoState.getSettings().getParameters().get("minPower"); Integer minpower = (val instanceof Integer) ? (Integer) val : 2; val = negoState.getSettings().getParameters().get("maxPower"); Integer maxpower = (val instanceof Integer) ? (Integer) val : Integer.MAX_VALUE; Set votes = voting.getBids().stream().distinct() .filter(offer -> negoState.isAcceptable(offer.getBid())) .map(offer -> new Vote(negoState.getSettings().getID(), offer.getBid(), minpower, maxpower)) .collect(Collectors.toSet()); return new Votes(negoState.getSettings().getID(), votes); } }