<s> import sys <EOL> import random <EOL> random . seed ( <NUM_LIT:10> ) <EOL> def const ( bid ) : <EOL> return bid <EOL> def rand ( upper ) : <EOL> return int ( random . random ( ) * upper ) <EOL> def mcpc ( ecpc , pctr ) : <EOL> return int ( ecpc * pctr ) <EOL> def lin ( pctr , basectr , basebid ) : <EOL> return int ( pctr * basebid / basectr ) <EOL> def win ( ccfme , bid ) : <EOL> return bid >= ccfme [ <NUM_LIT:2> ] and bid > ccfme [ <NUM_LIT:3> ] <EOL> if len ( sys . argv ) < <NUM_LIT:6> : <EOL> print '<STR_LIT>' <EOL> exit ( - <NUM_LIT:1> ) <EOL> ccfm = [ ] <EOL> lrpctrs = [ ] <EOL> gbrtpctrs = [ ] <EOL> totalcost = <NUM_LIT:0> <EOL> tecpc = <NUM_LIT:0.> <EOL> tctr = <NUM_LIT:0.> <EOL> fi = open ( sys . argv [ <NUM_LIT:1> ] , '<STR_LIT:r>' ) <EOL> first = True <EOL> num = <NUM_LIT:0> <EOL> for line in fi : <EOL> s = line . split ( '<STR_LIT:\t>' ) <EOL> if first : <EOL> first = False <EOL> continue <EOL> clk = int ( s [ <NUM_LIT:0> ] ) <EOL> cost = int ( s [ <NUM_LIT> ] ) <EOL> num += <NUM_LIT:1> <EOL> tctr += clk <EOL> tecpc += cost <EOL> fi . close ( ) <EOL> tecpc /= tctr <EOL> tctr /= num <EOL> fi = open ( sys . argv [ <NUM_LIT:2> ] , '<STR_LIT:r>' ) <EOL> first = True <EOL> for line in fi : <EOL> s = line . split ( '<STR_LIT:\t>' ) <EOL> if first : <EOL> first = False <EOL> continue <EOL> clk = int ( s [ <NUM_LIT> ] ) <EOL> cnv = int ( s [ <NUM_LIT> ] ) <EOL> floorprice = int ( s [ <NUM_LIT:20> ] ) <EOL> marketprice = int ( s [ <NUM_LIT> ] ) <EOL> ccfm . append ( ( clk , cnv , floorprice , marketprice ) ) <EOL> totalcost += marketprice <EOL> fi . close ( ) <EOL> fi = open ( sys . argv [ <NUM_LIT:3> ] , '<STR_LIT:r>' ) <EOL> for line in fi : <EOL> lrpctrs . append ( float ( line . strip ( ) ) ) <EOL> fi . close ( ) <EOL> fi = open ( sys . argv [ <NUM_LIT:4> ] , '<STR_LIT:r>' ) <EOL> for line in fi : <EOL> gbrtpctrs . append ( float ( line . strip ( ) ) ) <EOL> fi . close ( ) <EOL> budgetProportions = [ <NUM_LIT:32> , <NUM_LIT:8> , <NUM_LIT:2> ] <EOL> constParas = range ( <NUM_LIT:2> , <NUM_LIT:20> , <NUM_LIT:2> ) + range ( <NUM_LIT:20> , <NUM_LIT:100> , <NUM_LIT:5> ) + range ( <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT:10> ) <EOL> randParas = range ( <NUM_LIT:2> , <NUM_LIT:20> , <NUM_LIT:2> ) + range ( <NUM_LIT:20> , <NUM_LIT:100> , <NUM_LIT:5> ) + range ( <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT:10> ) <EOL> mcpcParas = [ <NUM_LIT:1> ] <EOL> linParas = range ( <NUM_LIT:2> , <NUM_LIT:20> , <NUM_LIT:2> ) + range ( <NUM_LIT:20> , <NUM_LIT:200> , <NUM_LIT:5> ) + range ( <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT:10> ) + range ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> algoParas = { "<STR_LIT>" : constParas , "<STR_LIT>" : randParas , "<STR_LIT>" : mcpcParas , "<STR_LIT>" : mcpcParas , "<STR_LIT>" : linParas , "<STR_LIT>" : linParas } <EOL> def simulateABiddingStategyWithParameter ( cases , tcost , proportion , algo , para ) : <EOL> budget = int ( tcost / proportion ) <EOL> cost = <NUM_LIT:0> <EOL> clks = <NUM_LIT:0> <EOL> cnvs = <NUM_LIT:0> <EOL> bids = <NUM_LIT:0> <EOL> imps = <NUM_LIT:0> <EOL> for idx in range ( <NUM_LIT:0> , len ( cases ) ) : <EOL> bid = <NUM_LIT:0> <EOL> if algo == "<STR_LIT>" : <EOL> bid = const ( para ) <EOL> elif algo == "<STR_LIT>" : <EOL> bid = rand ( para ) <EOL> elif algo == "<STR_LIT>" : <EOL> lrpctr = lrpctrs [ idx ] <EOL> bid = mcpc ( tecpc , lrpctr ) <EOL> elif algo == "<STR_LIT>" : <EOL> gbrtpctr = gbrtpctrs [ idx ] <EOL> bid = mcpc ( tecpc , gbrtpctr ) <EOL> elif algo == "<STR_LIT>" : <EOL> lrpctr = lrpctrs [ idx ] <EOL> bid = lin ( lrpctr , tctr , para ) <EOL> elif algo == "<STR_LIT>" : <EOL> gbrtpctr = gbrtpctrs [ idx ] <EOL> bid = lin ( gbrtpctr , tctr , para ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> bids += <NUM_LIT:1> <EOL> case = cases [ idx ] <EOL> if win ( case , bid ) : <EOL> imps += <NUM_LIT:1> <EOL> clks += case [ <NUM_LIT:0> ] <EOL> cnvs += case [ <NUM_LIT:1> ] <EOL> cost += case [ <NUM_LIT:3> ] <EOL> if cost > budget : <EOL> break <EOL> return str ( proportion ) + '<STR_LIT:\t>' + str ( clks ) + '<STR_LIT:\t>' + str ( cnvs ) + '<STR_LIT:\t>' + str ( bids ) + '<STR_LIT:\t>' + str ( imps ) + '<STR_LIT:\t>' + str ( budget ) + '<STR_LIT:\t>' + str ( cost ) + '<STR_LIT:\t>' + algo + '<STR_LIT:\t>' + str ( para ) <EOL> def simulateABiddingStrategy ( cases , tcost , proportion , algo , writer ) : <EOL> paras = algoParas [ algo ] <EOL> for para in paras : <EOL> res = simulateABiddingStategyWithParameter ( cases , tcost , proportion , algo , para ) <EOL> print res <EOL> writer . write ( res + '<STR_LIT:\n>' ) <EOL> fo = open ( sys . argv [ <NUM_LIT:5> ] , '<STR_LIT:w>' ) <EOL> header = "<STR_LIT>" <EOL> fo . write ( header + "<STR_LIT:\n>" ) <EOL> print header <EOL> for bp in budgetProportions : <EOL> for algo in algoParas : <EOL> simulateABiddingStrategy ( ccfm , totalcost , bp , algo , fo ) <EOL> fo . close ( ) </s>
