diff --git a/partyrepository.xml b/partyrepository.xml index fb4f683c02e82a1552384815716fc565d61fc112..9de96ae7eb9315408ed5b26d9c20e7185813272b 100644 --- a/partyrepository.xml +++ b/partyrepository.xml @@ -444,7 +444,7 @@ <partyRepItem classPath="negotiator.parties.FeedbackHillClimber" strategyParameters=""> <properties/> </partyRepItem> - <partyRepItem classPath="/Users/morge/Documents/Lille/Enseignement/2021/1SEM/SCI/Annexes/workspace/MyGenius/bin/MyStrategy.class" strategyParameters=""> + <partyRepItem classPath="/Users/morge/Documents/Lille/Enseignement/2021/1SEM/SCI/Annexes/workspace/MyGenius/bin/My2ndStrategy.class" strategyParameters=""> <properties/> </partyRepItem> </partyRepItems> diff --git a/src/MyStrategy.java b/src/MyStrategy.java index 1e1e0d20eca121fbe9adc9bd69e0b36c3633960d..dc69d36cbdc9d978531f21905e08bc3d71ff7fcd 100644 --- a/src/MyStrategy.java +++ b/src/MyStrategy.java @@ -12,129 +12,129 @@ import genius.core.issue.*; /** * My strategy chooses randomly a bid better than its reservation value * and accepts the last bid of the opponent if the current bid is not better - */ + */ public class MyStrategy extends Agent { - private final boolean debug = true; - private static final long serialVersionUID = 1L; - - /** - * Human readable strategy description - */ - public String getDescription() { - return "My strategy"; - } - - /** - * Reservation value - */ - private static double RESERVATION_VALUE = 0.0; - - /** - * Method called when a new session starts - */ - @Override - public void init() { - RESERVATION_VALUE = utilitySpace.getReservationValueUndiscounted(); - } - - /** - * Method which informs the agent about its utility - */ - @Override - public void endSession(NegotiationResult result) { - if (debug) System.out.println("Agent "+getAgentID()+" utility: " + result.getMyDiscountedUtility()); - } - - /** - * The last action of the opponent - */ - private Action lastOpponentAction = null; - - - /** - * Assign the last opponent action - */ - public void ReceiveMessage(Action opponentAction) { - lastOpponentAction = opponentAction; - } - - /** - * Compute the next action - */ - @Override - public Action chooseAction() { - Action action = null; - Bid lastOpponentBid = null; - try { - if (lastOpponentAction == null) - action = chooseRandomBidAction(); - if (lastOpponentAction instanceof Offer) { - // Look after the last opponent bid - lastOpponentBid = ((Offer) lastOpponentAction).getBid(); - double utilityOfLastOpponentBid = getUtility(lastOpponentBid); - - // Choose a random bid - action = chooseRandomBidAction(); - Bid nextBid = ((Offer) action).getBid(); - double utilityOfNextBid = getUtility(nextBid); - - // Accept the last bid of the opponent if my bid is not better - if (utilityOfLastOpponentBid > utilityOfNextBid) - action = new Accept(getAgentID(), lastOpponentBid); - } - } catch (Exception e) { - System.out.println("MyStrategy exception in chooseAction: " + e.getMessage()); - action = new Accept(getAgentID(), lastOpponentBid); // best guess if things go wrong. - } - return action; - } - - - /** - * Convenient wrapper for getRandomBid - * which returns an acceptance if an exception is raised - */ - private Action chooseRandomBidAction() { - Bid nextBid = null; - try { - nextBid = getRandomBid(); - if (debug) System.out.println("Agent "+getAgentID()+" randomly chooses the bid: " + nextBid); - } catch (Exception e) { - System.out.println("MyStrategy exception in chooseRandomBid:" + e.getMessage()); - } - if (nextBid == null) - return new Accept(getAgentID(), ((ActionWithBid) lastOpponentAction).getBid()); - return new Offer(getAgentID(), nextBid); - } - - - /** - * Create a random bid better than the reservation value - */ - private Bid getRandomBid() throws Exception { - HashMap<Integer, Value> values = new HashMap<>(); - // pairs of issue number, value - List<Issue> issues = utilitySpace.getDomain().getIssues(); - Random randomGenerator = new Random(); - Bid bid; - do { - for (Issue issue : issues) { - switch (issue.getType()) { - case DISCRETE : - IssueDiscrete discreteIssue = (IssueDiscrete) issue; - int indexValue = randomGenerator - .nextInt(discreteIssue.getNumberOfValues()); - values.put(issue.getNumber(), - discreteIssue.getValue(indexValue)); - break; - default : - throw new Exception("MyStrategy exception in getRandomBid: " + - issue.getType() + " is not supported by my strategy"); - } - } - bid = new Bid(utilitySpace.getDomain(), values); - } while (getUtility(bid) < RESERVATION_VALUE); - return bid; - } -} + private final boolean debug = true; + private static final long serialVersionUID = 1L; + + /** + * Human readable strategy description + */ + public String getDescription() { + return "My strategy"; + } + + /** + * Reservation value + */ + private static double RESERVATION_VALUE = 0.0; + + /** + * Method called when a new session starts + */ + @Override + public void init() { + RESERVATION_VALUE = utilitySpace.getReservationValueUndiscounted(); + } + + /** + * Method which informs the agent about its utility + */ + @Override + public void endSession(NegotiationResult result) { + if (debug) System.out.println("Agent "+getAgentID()+" utility: " + result.getMyDiscountedUtility()); + } + + /** + * The last action of the opponent + */ + private Action lastOpponentAction = null; + + + /** + * Assign the last opponent action + */ + public void ReceiveMessage(Action opponentAction) { + lastOpponentAction = opponentAction; + } + + /** + * Compute the next action + */ + @Override + public Action chooseAction() { + Action action = null; + Bid lastOpponentBid = null; + try { + if (lastOpponentAction == null) + action = chooseRandomBidAction(); + if (lastOpponentAction instanceof Offer) { + // Look after the last opponent bid + lastOpponentBid = ((Offer) lastOpponentAction).getBid(); + double utilityOfLastOpponentBid = getUtility(lastOpponentBid); + + // Choose a random bid + action = chooseRandomBidAction(); + Bid nextBid = ((Offer) action).getBid(); + double utilityOfNextBid = getUtility(nextBid); + + // Accept the last bid of the opponent if my bid is not better + if (utilityOfLastOpponentBid > utilityOfNextBid) + action = new Accept(getAgentID(), lastOpponentBid); + } + } catch (Exception e) { + System.out.println("MyStrategy exception in chooseAction: " + e.getMessage()); + action = new Accept(getAgentID(), lastOpponentBid); // best guess if things go wrong. + } + return action; + } + + + /** + * Convenient wrapper for getRandomBid + * which returns an acceptance if an exception is raised + */ + private Action chooseRandomBidAction() { + Bid nextBid = null; + try { + nextBid = getRandomBid(); + if (debug) System.out.println("Agent "+getAgentID()+" randomly chooses the bid: " + nextBid); + } catch (Exception e) { + System.out.println("MyStrategy exception in chooseRandomBid:" + e.getMessage()); + } + if (nextBid == null) + return new Accept(getAgentID(), ((ActionWithBid) lastOpponentAction).getBid()); + return new Offer(getAgentID(), nextBid); + } + + + /** + * Create a random bid better than the reservation value + */ + private Bid getRandomBid() throws Exception { + HashMap<Integer, Value> values = new HashMap<>(); + // pairs of issue number, value + List<Issue> issues = utilitySpace.getDomain().getIssues(); + Random randomGenerator = new Random(); + Bid bid; + do { + for (Issue issue : issues) { + switch (issue.getType()) { + case DISCRETE : + IssueDiscrete discreteIssue = (IssueDiscrete) issue; + int indexValue = randomGenerator + .nextInt(discreteIssue.getNumberOfValues()); + values.put(issue.getNumber(), + discreteIssue.getValue(indexValue)); + break; + default : + throw new Exception("MyStrategy exception in getRandomBid: " + + issue.getType() + " is not supported by my strategy"); + } + } + bid = new Bid(utilitySpace.getDomain(), values); + } while (getUtility(bid) < RESERVATION_VALUE); + return bid; + } +} \ No newline at end of file