Skip to content

Commit

Permalink
Using but not utilising correctly history
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatakis committed Dec 30, 2017
1 parent f36d9ba commit 28167dc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/BestAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public double getConcessionThreshold() {

@Override
public int getMemoryDepth() {
return 3;
return 1;
}

@Override
Expand Down
51 changes: 40 additions & 11 deletions src/Core/BidHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import list.Tuple;
import negotiator.AgentID;
import negotiator.Bid;
import negotiator.parties.NegotiationInfo;
import negotiator.persistent.StandardInfoList;
import negotiator.persistent.StandardInfo;
Expand All @@ -10,41 +11,63 @@
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static Core.ThrashAgent.MemoryDepth;
import static Core.ThrashAgent.bidHistory;
import static Core.ThrashAgent.gLog;

public class BidHistory {
private StandardInfoList history;
private NegotiationInfo info;
private Map<String, Double> bestOfferedUtils;
private Map<String, Double> worstOfferedUtils;
private Map<String, Map<Integer, Double>> acceptedUtils; //The accepted bids of enemies for different rounds
private int round;
private Random RNG;

public BidHistory(NegotiationInfo info, PersistentDataContainer pData) {
this.info = info;
public BidHistory(Random RNG, PersistentDataContainer pData) {
this.RNG = RNG;
this.bestOfferedUtils = new HashMap<>();
this.worstOfferedUtils = new HashMap<>();
this.acceptedUtils = new HashMap<>();

switch (pData.getPersistentDataType()) {
case DISABLED:
break;
case SERIALIZABLE:
break;
case STANDARD:
history = (StandardInfoList) pData.get();
this.history = (StandardInfoList) pData.get();
this.round = history.size();

//Compute for each party the maximum and minimum utility in last session.
if (!history.isEmpty() && MemoryDepth > 0 && MemoryDepth <= history.size()) {
bestOfferedUtils = new HashMap<>();
worstOfferedUtils = new HashMap<>();
if (!history.isEmpty()) {

for (int i = history.size() - 1; i >= history.size() - MemoryDepth; i--) {
StandardInfo lastInfo = history.get(history.size() - 1);
if (MemoryDepth < 1 || history.size() <= MemoryDepth) {
MemoryDepth = history.size() - 1;
}

for (int i = history.size() - MemoryDepth; i < history.size(); i++) {
StandardInfo curInfo = history.get(i);

for (Tuple<String, Double> offered : lastInfo.getUtilities()) {
for (Tuple<String, Double> offered : curInfo.getUtilities()) {
String party = offered.get1().split("@")[0];
Double util = offered.get2();
bestOfferedUtils.put(party, bestOfferedUtils.containsKey(party) ? Math.max(bestOfferedUtils.get(party), util) : util);
worstOfferedUtils.put(party, worstOfferedUtils.containsKey(party) ? Math.min(worstOfferedUtils.get(party), util) : util);

if (!acceptedUtils.containsKey(party)) {
acceptedUtils.put(party, new HashMap<>());
}

acceptedUtils.get(party).put(i, util);
}
}

gLog.println("Accepted values");
for (String party : acceptedUtils.keySet()) {
for (Map.Entry<Integer, Double> value : acceptedUtils.get(party).entrySet()) {
gLog.println(party + " " + value);
}
}
}
Expand All @@ -67,11 +90,17 @@ public Map<String, Double> getWorstOfferedUtils() {
return this.worstOfferedUtils;
}

public void initOppVals(Opponent opponent, AgentID sender) {
public void setBestOppVals(Opponent opponent, AgentID sender) {
try {
opponent.BestOfferUtil = bestOfferedUtils.getOrDefault(sender.getName().split("@")[0], null);
opponent.WorstOfferUtil = worstOfferedUtils.getOrDefault(sender.getName().split("@")[0], null);
} catch (Exception ignored) {
}
}

public double getLuckyBid(AgentID partyId) {
double l = acceptedUtils.get(partyId.getName()).get(RNG.nextInt(round));
gLog.println("Lucky BID");
return l;
}
}
50 changes: 5 additions & 45 deletions src/Core/NegotiationStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class NegotiationStatistics {
private int round;
private double MaxPopularBidUtility;
private double OwnWorstOfferedUtility;
private Boolean ConstantWeightFunction;

NegotiationStatistics(AbstractUtilitySpace utilitySpace, Random RNG) {
this.utilitySpace = utilitySpace;
Expand All @@ -41,7 +40,6 @@ public class NegotiationStatistics {
round = 0;
MaxPopularBidUtility = 0.0;
OwnWorstOfferedUtility = 1.0;
ConstantWeightFunction = true;

ArrayList<Value> values;
for (Issue issue : issues) {
Expand Down Expand Up @@ -114,14 +112,6 @@ ArrayList<Value> getValues(Issue issue) {
void initOpponent(AgentID sender) {
Opponent opponent = new Opponent(RNG);
opponent.name = sender.getName().split("@")[0];
opponent.isSelf = opponent.name.equals(myDescription);

//Attempt to extract historical data
if(useHistory){
try {
// bidHistory.initOppVals(opponent,sender);
}catch (Exception ignored){}
}

for (Issue issue : issues) {
opponent.ValueFrequency.put(issue, new HashMap<>());
Expand Down Expand Up @@ -188,11 +178,6 @@ void updateInfo(AgentID sender, Bid offeredBid, double timeOfTheOffer) {
Value value = offeredBid.getValue(issue.getNumber());
opponent.updateFrequency(issue, value, 1);

//Avoid useless updates..
if (!ConstantWeightFunction) {
opponent.updateWeightedFrequency(issue, value, 1.0);
}

allValueFrequency.get(issue).put(value, allValueFrequency.get(issue).get(value) + 1); // update the list
}

Expand Down Expand Up @@ -229,7 +214,10 @@ void updatePopularBidList(Bid popularBid) {

public class UtilityComparator implements Comparator<Bid> {
public int compare(Bid a, Bid b) {
return Double.compare(utilitySpace.getUtilityWithDiscount(a, 0.0), utilitySpace.getUtilityWithDiscount(b, 0.0));
return Double.compare(
utilitySpace.getUtilityWithDiscount(a, 0.0),
utilitySpace.getUtilityWithDiscount(b, 0.0)
);
}
}

Expand Down Expand Up @@ -288,38 +276,10 @@ private Value getValueFrequencyList(AgentID sender, Issue issue) {
return max_value;
}

/**
* Based on the frequency of opponentsValueFrequencyWeighted, returns the most
* frequent weighted value of this issue of this opponent
*
* @param sender The AgentID of the sender.
* @param issue The issue.
*/
private Value getValueFrequencyListWeighted(AgentID sender, Issue issue) {
double curFreq;
double maxFreq = 0;
Value maxVal = null;
ArrayList<Value> randomOrderValues = getValues(issue);

Collections.shuffle(randomOrderValues);

for (Value value : randomOrderValues) {
curFreq = opponents.get(sender).ValueFrequencyWeighted.get(issue).get(value);
// Record the most frequent element
if (maxVal == null || curFreq > maxFreq) {
maxFreq = curFreq;
maxVal = value;
}
}
return maxVal;
}

HashMap<Issue, Value> getMaxValuesForOpponent(AgentID opponent) {
HashMap<Issue, Value> bestValuesForOpponent = new HashMap<>();
for (Issue issue : issues) {
bestValuesForOpponent.put(issue, ConstantWeightFunction ?
getValueFrequencyList(opponent, issue) :
getValueFrequencyListWeighted(opponent, issue));
bestValuesForOpponent.put(issue, getValueFrequencyList(opponent, issue));
}
return bestValuesForOpponent;
}
Expand Down
1 change: 0 additions & 1 deletion src/Core/Opponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class Opponent {
HashMap<Issue, HashMap<Value, Double>> ValueFrequencyWeighted; // the value frequency matrix of each issue of each negotiator weighted by the WeightFunction
HashMap<Issue, HashMap<Value, Integer>> AcceptedValueFrequency; // the value frequency matrix of each issue of each negotiator
private Random random;
public boolean isSelf;

Opponent(Random RNG) {
this.H = 0.5;
Expand Down
8 changes: 3 additions & 5 deletions src/Core/ThrashAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void init(NegotiationInfo info) {
filename = "C:/Users/gstamatakis/IdeaProjects/ANAC-agents/logs/" + AgentStrat + "_logs.txt";

try {
gLog = new PrintWriter(new FileWriter(filename, true), true);
gLog = new PrintWriter(new FileWriter(filename, true));
} catch (Exception e) {
gLog = new PrintWriter(System.out);
}
Expand All @@ -63,9 +63,7 @@ public void init(NegotiationInfo info) {
useHistory = useHistory();
if (useHistory) {
try {
gLog.println("FROM " + System.currentTimeMillis());
bidHistory = new BidHistory(info, getData());
gLog.println("TO " + System.currentTimeMillis());
bidHistory = new BidHistory(RNG, getData());
} catch (Exception e) {
gLog.println(e.toString());
useHistory = false;
Expand Down Expand Up @@ -101,7 +99,7 @@ public Action chooseAction(List<Class<? extends Action>> validActions) {
Double targetTime;
Bid bidToOffer;

if (Information.getRound() < 3) {
if (Information.getRound() < 2) {
try {
return new Offer(getPartyId(), Information.updateMyBidHistory(utilitySpace.getMaxUtilityBid()));
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion src/TimeAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public double getTimeScalingFactor() {

@Override
public double getCutoffValue() {
return 1e-3;
return 1e-6;
}

@Override
Expand Down

0 comments on commit 28167dc

Please sign in to comment.