Skip to content

Instantly share code, notes, and snippets.

@iPinYou
Last active May 11, 2022 07:55
Show Gist options
  • Save iPinYou/a5acfc29e0d319eba2c1 to your computer and use it in GitHub Desktop.
Save iPinYou/a5acfc29e0d319eba2c1 to your computer and use it in GitHub Desktop.
package com.ipinyou.contest.algorithm;
public interface Algorithm {
void init() throws Exception ;
int getBidPrice(BidRequest bidRequest);
}
package com.ipinyou.contest.algorithm;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Bid class that need to be completed by the contest participants This is the
* only place to implement your bidding algorithm Note that the class name (Bid)
* and package name (com.ipinyou.contest.algorithm) are fixed to make a correct
* submission.
*
*/
public class Bid implements Algorithm {
/*** Algorithm parameters definition***/
// ratio of bidding in percent
private int bidRatio = 50;
// Fixed bid price
private int fixedBidPrice = 300;
// Config parameters def file
private String configFile = "algo.conf";
// Other model related variables may be defined here
// private Map<String, String> model = new HashMap<String, String>();
/**
* init() method initializes alogrithm paramters and model
* Memory limit: 512M
* @throws Exception
*/
@Override
public void init() throws Exception {
readConfigPara(this.configFile);
}
/**
* getBidPrice() method makes the real calculation Note: one bid reqeust
* decision has to be make less than 20ms.
*
*/
@Override
public int getBidPrice(BidRequest bidRequest) {
int bidPrice = -1;
Random r = new Random();
if (r.nextInt(100) < this.bidRatio)
bidPrice = this.fixedBidPrice;
return bidPrice;
}
/** Reading config parameters from config file
* The config file is defined in key=value format
* Lines starting with '#' are regarded as remarks
*/
public void readConfigPara(String filename) throws Exception {
InputStream inputStream = read(filename);
String line = null;
Map<String, String> kvMap = new HashMap<String, String>();
BufferedReader br = new BufferedReader(new InputStreamReader(
inputStream));
while ((line = br.readLine()) != null) {
if (line.matches("^#.*")) // Skip remark lines starting with #
continue;
String[] arr = line.trim().split("=");
if (arr.length == 2) {
kvMap.put(arr[0], arr[1]);
}
}
if (kvMap.containsKey("bidRatio"))
this.bidRatio = Integer.parseInt(kvMap.get("bidRatio"));
if (kvMap.containsKey("fixedBidPrice"))
this.fixedBidPrice = Integer.parseInt(kvMap.get("fixedBidPrice"));
}
/**
* read file(from jar files or not) to inputstream
*/
private InputStream read(String filename) {
InputStream resourceAsStream = Thread.currentThread()
.getContextClassLoader().getResourceAsStream(filename);
if (resourceAsStream == null) {
throw new RuntimeException("read file error ");
}
return resourceAsStream;
}
}
package com.ipinyou.contest.algorithm;
import java.io.Serializable;
public class BidRequest implements Serializable{
private static final long serialVersionUID = -3012027079030559912L;
private String bidId; // c0550000008e5a94ac18823d6f275121
private String timestamp ; // 20130218134701883
private String ipinyouId ; // dF_5qwD1UDI
private String userAgent; // Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0
private String ipAddress ; // 119.163.222.*
private String region; // 146
private String city; // 147
private String adExchange; // 2
private String domain ; // e80f4ec7f5bfbc9ca416a8c01cd1a049
private String url; // hz55b000008e5a94ac18823d6f275121
private String anonymousURLID ;// Null
private String adSlotID; // mm_10982364_973726_9023493
private String adSlotWidth; // 300
private String adSlotHeight; // 250
private String adSlotVisibility; // 1
private String adSlotFormat; // 1
private String adSlotFloorPrice; // (RMB/CPM) (*) 0
private String creativeID; // f80f4ec7f5bfbc9ca416a8c01cd1a049
private String advertiserId; //1234
private String userTags; //7892,12398,87927
public String getBidId() {
return bidId;
}
public void setBidId(String bidId) {
this.bidId = bidId;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getIpinyouId() {
return ipinyouId;
}
public void setIpinyouId(String ipinyouId) {
this.ipinyouId = ipinyouId;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAdExchange() {
return adExchange;
}
public void setAdExchange(String adExchange) {
this.adExchange = adExchange;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAnonymousURLID() {
return anonymousURLID;
}
public void setAnonymousURLID(String anonymousURLID) {
this.anonymousURLID = anonymousURLID;
}
public String getAdSlotID() {
return adSlotID;
}
public void setAdSlotID(String adSlotID) {
this.adSlotID = adSlotID;
}
public String getAdSlotWidth() {
return adSlotWidth;
}
public void setAdSlotWidth(String adSlotWidth) {
this.adSlotWidth = adSlotWidth;
}
public String getAdSlotHeight() {
return adSlotHeight;
}
public void setAdSlotHeight(String adSlotHeight) {
this.adSlotHeight = adSlotHeight;
}
public String getAdSlotVisibility() {
return adSlotVisibility;
}
public void setAdSlotVisibility(String adSlotVisibility) {
this.adSlotVisibility = adSlotVisibility;
}
public String getAdSlotFormat() {
return adSlotFormat;
}
public void setAdSlotFormat(String adSlotFormat) {
this.adSlotFormat = adSlotFormat;
}
public String getAdSlotFloorPrice() {
return adSlotFloorPrice;
}
public void setAdSlotFloorPrice(String adSlotFloorPrice) {
this.adSlotFloorPrice = adSlotFloorPrice;
}
public String getCreativeID() {
return creativeID;
}
public void setCreativeID(String creativeID) {
this.creativeID = creativeID;
}
public String getAdvertiserId() {
return advertiserId;
}
public void setAdvertiserId(String advertiserId) {
this.advertiserId = advertiserId;
}
public String getUserTags() {
return userTags;
}
public void setUserTags(String userTags) {
this.userTags = userTags;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment