-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleAdNetwork.java
589 lines (496 loc) · 17.9 KB
/
SampleAdNetwork.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package edu.umich.tacadx.agents;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import se.sics.isl.transport.Transportable;
import se.sics.tasim.aw.Agent;
import se.sics.tasim.aw.Message;
import se.sics.tasim.props.SimulationStatus;
import se.sics.tasim.props.StartInfo;
import tau.tac.adx.ads.properties.AdType;
import tau.tac.adx.demand.CampaignStats;
import tau.tac.adx.devices.Device;
import tau.tac.adx.props.AdxBidBundle;
import tau.tac.adx.props.AdxQuery;
import tau.tac.adx.props.PublisherCatalog;
import tau.tac.adx.props.PublisherCatalogEntry;
import tau.tac.adx.report.adn.AdNetworkReport;
import tau.tac.adx.report.adn.MarketSegment;
import tau.tac.adx.report.demand.AdNetBidMessage;
import tau.tac.adx.report.demand.AdNetworkDailyNotification;
import tau.tac.adx.report.demand.CampaignOpportunityMessage;
import tau.tac.adx.report.demand.CampaignReport;
import tau.tac.adx.report.demand.CampaignReportKey;
import tau.tac.adx.report.demand.InitialCampaignMessage;
import tau.tac.adx.report.publisher.AdxPublisherReport;
import tau.tac.adx.report.publisher.AdxPublisherReportEntry;
import edu.umich.eecs.tac.props.Ad;
import edu.umich.eecs.tac.props.BankStatus;
/**
*
* @author Mariano Schain
*
*/
public class SampleAdNetwork extends Agent {
private final Logger log = Logger
.getLogger(SampleAdNetwork.class.getName());
/*
* Basic simulation information. An agent should receive the {@link
* StartInfo} at the beginning of the game or during recovery.
*/
@SuppressWarnings("unused")
private StartInfo startInfo;
/**
* Messages received:
*
* We keep all the {@link CampaignReport campaign reports}
* delivered to the agent. We also keep the initialization
* messages {@link PublisherCatalog} and
* {@link InitialCampaignMessage} and the most recent messages and reports
* {@link CampaignOpportunityMessage}, {@link CampaignReport}, and
* {@link AdNetworkDailyNotification}.
*/
private final Queue<CampaignReport> campaignReports;
private PublisherCatalog publisherCatalog;
private InitialCampaignMessage initialCampaignMessage;
private AdNetworkDailyNotification adNetworkDailyNotification;
/*
* The addresses of server entities to which the agent should send the daily
* bids data
*/
private String demandAgentAddress;
private String adxAgentAddress;
/*
* we maintain a list of queries - each characterized by the web site (the
* publisher), the device type, the ad type, and the user market segment
*/
private AdxQuery[] queries;
/**
* Information regarding the latest campaign opportunity announced
*/
private CampaignData pendingCampaign;
/**
* We maintain a collection (mapped by the campaign id) of the campaigns won
* by our agent.
*/
private Map<Integer, CampaignData> myCampaigns;
/*
* the bidBundle to be sent daily to the AdX
*/
private AdxBidBundle bidBundle;
/*
* The current bid level for the user classification service
*/
double ucsBid;
/*
* The targeted service level for the user classification service
*/
double ucsTargetLevel;
/*
* current day of simulation
*/
private int day;
private Random randomGenerator;
public SampleAdNetwork() {
campaignReports = new LinkedList<CampaignReport>();
}
@Override
protected void messageReceived(Message message) {
try {
Transportable content = message.getContent();
//log.fine(message.getContent().getClass().toString());
if (content instanceof InitialCampaignMessage) {
handleInitialCampaignMessage((InitialCampaignMessage) content);
} else if (content instanceof CampaignOpportunityMessage) {
handleICampaignOpportunityMessage((CampaignOpportunityMessage) content);
} else if (content instanceof CampaignReport) {
handleCampaignReport((CampaignReport) content);
} else if (content instanceof AdNetworkDailyNotification) {
handleAdNetworkDailyNotification((AdNetworkDailyNotification) content);
} else if (content instanceof AdxPublisherReport) {
handleAdxPublisherReport((AdxPublisherReport) content);
} else if (content instanceof SimulationStatus) {
handleSimulationStatus((SimulationStatus) content);
} else if (content instanceof PublisherCatalog) {
handlePublisherCatalog((PublisherCatalog) content);
} else if (content instanceof AdNetworkReport) {
handleAdNetworkReport((AdNetworkReport) content);
} else if (content instanceof StartInfo) {
handleStartInfo((StartInfo) content);
} else if (content instanceof BankStatus) {
handleBankStatus((BankStatus) content);
} else {
log.info("UNKNOWN Message Received: " + content);
}
} catch (NullPointerException e) {
this.log.log(Level.SEVERE,
"Exception thrown while trying to parse message." + e);
return;
}
}
private void handleBankStatus(BankStatus content) {
log.info("Day " + day + " :" + content.toString());
}
/**
* Processes the start information.
*
* @param startInfo
* the start information.
*/
protected void handleStartInfo(StartInfo startInfo) {
this.startInfo = startInfo;
}
/**
* Process the reported set of publishers
*
* @param publisherCatalog
*/
private void handlePublisherCatalog(PublisherCatalog publisherCatalog) {
this.publisherCatalog = publisherCatalog;
generateAdxQuerySpace();
}
/**
* On day 0, a campaign (the "initial campaign") is allocated to each
* competing agent. The campaign starts on day 1. The address of the
* server's AdxAgent (to which bid bundles are sent) and DemandAgent (to
* which bids regarding campaign opportunities may be sent in subsequent
* days) are also reported in the initial campaign message
*/
private void handleInitialCampaignMessage(
InitialCampaignMessage campaignMessage) {
log.info(campaignMessage.toString());
day = 0;
initialCampaignMessage = campaignMessage;
demandAgentAddress = campaignMessage.getDemandAgentAddress();
adxAgentAddress = campaignMessage.getAdxAgentAddress();
CampaignData campaignData = new CampaignData(initialCampaignMessage);
campaignData.setBudget(initialCampaignMessage.getReachImps() / 1000.0);
/*
* The initial campaign is already allocated to our agent so we add it
* to our allocated-campaigns list.
*/
log.info("Day " + day + ": Allocated campaign - " + campaignData);
myCampaigns.put(initialCampaignMessage.getId(), campaignData);
}
/**
* On day n ( > 0) a campaign opportunity is announced to the competing
* agents. The campaign starts on day n + 2 or later and the agents may send
* (on day n) related bids (attempting to win the campaign). The allocation
* (the winner) is announced to the competing agents during day n + 1.
*/
private void handleICampaignOpportunityMessage(
CampaignOpportunityMessage com) {
day = com.getDay();
pendingCampaign = new CampaignData(com);
log.info("Day " + day + ": Campaign opportunity - " + pendingCampaign);
/*
* The campaign requires com.getReachImps() impressions. The competing
* Ad Networks bid for the total campaign Budget (that is, the ad
* network that offers the lowest budget gets the campaign allocated).
* The advertiser is willing to pay the AdNetwork at most 1$ CPM,
* therefore the total number of impressions may be treated as a reserve
* (upper bound) price for the auction.
*/
long cmpBid = 1 + Math.abs((randomGenerator.nextLong())
% (com.getReachImps()));
double cmpBidUnits = cmpBid / 1000.0;
log.info("Day " + day + ": Campaign total budget bid: " + cmpBidUnits);
/*
* Adjust ucs bid s.t. target level is achieved. Note: The bid for the
* user classification service is piggybacked
*/
if (adNetworkDailyNotification != null) {
double ucsLevel = adNetworkDailyNotification.getServiceLevel();
double prevUcsBid = ucsBid;
/* UCS Bid should not exceed 0.2 */
ucsBid = Math.min(0.1 + 0.1*randomGenerator.nextDouble(), prevUcsBid * (1 + ucsTargetLevel - ucsLevel));
log.info("Day " + day + ": Adjusting ucs bid: was " + prevUcsBid
+ " level reported: " + ucsLevel + " target: "
+ ucsTargetLevel + " adjusted: " + ucsBid);
} else {
log.info("Day " + day + ": Initial ucs bid is " + ucsBid);
}
/* Note: Campaign bid is in millis */
AdNetBidMessage bids = new AdNetBidMessage(ucsBid, pendingCampaign.id,
cmpBid);
sendMessage(demandAgentAddress, bids);
}
/**
* On day n ( > 0), the result of the UserClassificationService and Campaign
* auctions (for which the competing agents sent bids during day n -1) are
* reported. The reported Campaign starts in day n+1 or later and the user
* classification service level is applicable starting from day n+1.
*/
private void handleAdNetworkDailyNotification(
AdNetworkDailyNotification notificationMessage) {
adNetworkDailyNotification = notificationMessage;
log.info("Day " + day + ": Daily notification for campaign "
+ adNetworkDailyNotification.getCampaignId());
String campaignAllocatedTo = " allocated to "
+ notificationMessage.getWinner();
if ((pendingCampaign.id == adNetworkDailyNotification.getCampaignId())
&& (notificationMessage.getCost() != 0)) {
/* add campaign to list of won campaigns */
pendingCampaign.setBudget(notificationMessage.getCost());
myCampaigns.put(pendingCampaign.id, pendingCampaign);
campaignAllocatedTo = " WON at cost "
+ notificationMessage.getCost();
}
log.info("Day " + day + ": " + campaignAllocatedTo
+ ". UCS Level set to " + notificationMessage.getServiceLevel()
+ " at price " + notificationMessage.getPrice()
+ " Qualit Score is: " + notificationMessage.getQualityScore());
}
/**
* The SimulationStatus message received on day n indicates that the
* calculation time is up and the agent is requested to send its bid bundle
* to the AdX.
*/
private void handleSimulationStatus(SimulationStatus simulationStatus) {
log.info("Day " + day + " : Simulation Status Received");
sendBidAndAds();
log.info("Day " + day + " ended. Starting next day");
++day;
}
/**
*
*/
protected void sendBidAndAds() {
bidBundle = new AdxBidBundle();
int entrySum = 0;
/*
*
*/
for (CampaignData campaign : myCampaigns.values()) {
int dayBiddingFor = day + 1;
/* A fixed random bid, for all queries of the campaign */
/*
* Note: bidding per 1000 imps (CPM) - no more than average budget
* revenue per imp
*/
Random rnd = new Random();
double avgCmpRevenuePerImp = campaign.budget / campaign.reachImps;
double rbid = 1000.0 * rnd.nextDouble() * avgCmpRevenuePerImp;
/*
* add bid entries w.r.t. each active campaign with remaining
* contracted impressions.
*
* for now, a single entry per active campaign is added for queries
* of matching target segment.
*/
if ((dayBiddingFor >= campaign.dayStart)
&& (dayBiddingFor <= campaign.dayEnd)
&& (campaign.impsTogo() >= 0)) {
int entCount = 0;
for (int i = 0; i < queries.length; i++) {
Set<MarketSegment> segmentsList = queries[i]
.getMarketSegments();
for (MarketSegment marketSegment : segmentsList) {
if (campaign.targetSegment == marketSegment) {
/*
* among matching entries with the same campaign id,
* the AdX randomly chooses an entry according to
* the designated weight. by setting a constant
* weight 1, we create a uniform probability over
* active campaigns
*/
++entCount;
bidBundle.addQuery(queries[i], rbid, new Ad(null),
campaign.id, 1);
}
}
if (segmentsList.size() == 0) {
++entCount;
bidBundle.addQuery(queries[i], rbid, new Ad(null),
campaign.id, 1);
}
}
double impressionLimit = 0.5 * campaign.impsTogo();
double budgetLimit = 0.5 * Math.max(0, campaign.budget
- campaign.stats.getCost());
bidBundle.setCampaignDailyLimit(campaign.id,
(int) impressionLimit, budgetLimit);
entrySum += entCount;
log.info("Day " + day + ": Updated " + entCount
+ " Bid Bundle entries for Campaign id " + campaign.id);
}
}
if (bidBundle != null) {
log.info("Day " + day + ": Sending BidBundle");
sendMessage(adxAgentAddress, bidBundle);
}
}
/**
* Campaigns performance w.r.t. each allocated campaign
*/
private void handleCampaignReport(CampaignReport campaignReport) {
campaignReports.add(campaignReport);
/*
* for each campaign, the accumulated statistics from day 1 up to day
* n-1 are reported
*/
for (CampaignReportKey campaignKey : campaignReport.keys()) {
int cmpId = campaignKey.getCampaignId();
CampaignStats cstats = campaignReport.getCampaignReportEntry(
campaignKey).getCampaignStats();
myCampaigns.get(cmpId).setStats(cstats);
log.info("Day " + day + ": Updating campaign " + cmpId + " stats: "
+ cstats.getTargetedImps() + " tgtImps "
+ cstats.getOtherImps() + " nonTgtImps. Cost of imps is "
+ cstats.getCost());
}
}
/**
* Users and Publishers statistics: popularity and ad type orientation
*/
private void handleAdxPublisherReport(AdxPublisherReport adxPublisherReport) {
log.info("Publishers Report: ");
for (PublisherCatalogEntry publisherKey : adxPublisherReport.keys()) {
AdxPublisherReportEntry entry = adxPublisherReport
.getEntry(publisherKey);
log.info(entry.toString());
}
}
/**
*
* @param AdNetworkReport
*/
private void handleAdNetworkReport(AdNetworkReport adnetReport) {
log.info("Day "+ day + " : AdNetworkReport");
/*
for (AdNetworkKey adnetKey : adnetReport.keys()) {
double rnd = Math.random();
if (rnd > 0.95) {
AdNetworkReportEntry entry = adnetReport
.getAdNetworkReportEntry(adnetKey);
log.info(adnetKey + " " + entry);
}
}
*/
}
@Override
protected void simulationSetup() {
randomGenerator = new Random();
day = 0;
bidBundle = new AdxBidBundle();
ucsTargetLevel = 0.5 + (randomGenerator.nextInt(5) + 1) / 10.0;
/* initial bid between 0.1 and 0.2 */
ucsBid = 0.1 + 0.1*randomGenerator.nextDouble();
myCampaigns = new HashMap<Integer, CampaignData>();
log.fine("AdNet " + getName() + " simulationSetup");
}
@Override
protected void simulationFinished() {
campaignReports.clear();
bidBundle = null;
}
/**
* A user visit to a publisher's web-site results in an impression
* opportunity (a query) that is characterized by the the publisher, the
* market segment the user may belongs to, the device used (mobile or
* desktop) and the ad type (text or video).
*
* An array of all possible queries is generated here, based on the
* publisher names reported at game initialization in the publishers catalog
* message
*/
private void generateAdxQuerySpace() {
if (publisherCatalog != null && queries == null) {
Set<AdxQuery> querySet = new HashSet<AdxQuery>();
/*
* for each web site (publisher) we generate all possible variations
* of device type, ad type, and user market segment
*/
for (PublisherCatalogEntry publisherCatalogEntry : publisherCatalog) {
String publishersName = publisherCatalogEntry
.getPublisherName();
for (MarketSegment userSegment : MarketSegment.values()) {
Set<MarketSegment> singleMarketSegment = new HashSet<MarketSegment>();
singleMarketSegment.add(userSegment);
querySet.add(new AdxQuery(publishersName,
singleMarketSegment, Device.mobile, AdType.text));
querySet.add(new AdxQuery(publishersName,
singleMarketSegment, Device.pc, AdType.text));
querySet.add(new AdxQuery(publishersName,
singleMarketSegment, Device.mobile, AdType.video));
querySet.add(new AdxQuery(publishersName,
singleMarketSegment, Device.pc, AdType.video));
}
/**
* An empty segments set is used to indicate the "UNKNOWN" segment
* such queries are matched when the UCS fails to recover the user's
* segments.
*/
querySet.add(new AdxQuery(publishersName,
new HashSet<MarketSegment>(), Device.mobile,
AdType.video));
querySet.add(new AdxQuery(publishersName,
new HashSet<MarketSegment>(), Device.mobile,
AdType.text));
querySet.add(new AdxQuery(publishersName,
new HashSet<MarketSegment>(), Device.pc, AdType.video));
querySet.add(new AdxQuery(publishersName,
new HashSet<MarketSegment>(), Device.pc, AdType.text));
}
queries = new AdxQuery[querySet.size()];
querySet.toArray(queries);
}
}
private class CampaignData {
/* campaign attributes as set by server */
Long reachImps;
long dayStart;
long dayEnd;
MarketSegment targetSegment;
double videoCoef;
double mobileCoef;
int id;
/* campaign info as reported */
CampaignStats stats;
double budget;
public CampaignData(InitialCampaignMessage icm) {
reachImps = icm.getReachImps();
dayStart = icm.getDayStart();
dayEnd = icm.getDayEnd();
targetSegment = icm.getTargetSegment().iterator().next(); // FIXME hack from interface mismatch
videoCoef = icm.getVideoCoef();
mobileCoef = icm.getMobileCoef();
id = icm.getId();
stats = new CampaignStats(0, 0, 0);
budget = 0.0;
}
public void setBudget(double d) {
budget = d;
}
public CampaignData(CampaignOpportunityMessage com) {
dayStart = com.getDayStart();
dayEnd = com.getDayEnd();
id = com.getId();
reachImps = com.getReachImps();
targetSegment = com.getTargetSegment().iterator().next(); // FIXME hack from interface mismatch
mobileCoef = com.getMobileCoef();
videoCoef = com.getVideoCoef();
stats = new CampaignStats(0, 0, 0);
budget = 0.0;
}
@Override
public String toString() {
return "Campaign ID " + id + ": " + "day " + dayStart + " to "
+ dayEnd + " " + targetSegment.name() + ", reach: "
+ reachImps + " coefs: (v=" + videoCoef + ", m="
+ mobileCoef + ")";
}
int impsTogo() {
return (int) Math.max(0, reachImps - stats.getTargetedImps());
}
void setStats(CampaignStats s) {
stats.setValues(s);
}
}
}