mono <- read.csv("monotone.23.csv")

indx <- function(c1, c2){
	return((2 * (c1 - 1)) + c2)
}

pay <- function(c1, c2, cd, dc){
	paym <- c(1, cd, dc, 0)
	pay <- paym[indx(c1, c2)]
	return(pay)
}

pv <- function(s1, s2, cd, dc, delta){
	st <- c(s1[5], s2[5])
	pay <- 0
	for(ii in 1:36){
		newpay <- pay(st[1], st[2], cd, dc)
		newpay <- (1-delta) * (delta^(ii-1)) * newpay
		nx <- indx(st[1], st[2])
		st <- c(s1[nx], s2[nx])
		pay <- pay + newpay
	}
	return(pay)
}

strat <- function(){
	pst <- matrix(0, 32, 5)
	ns <- 1
	for(ii in 1:2){
		for(jj in 1:2){
			for(kk in 1:2){
				for(ll in 1:2){
					for(mm in 1:2){
						pst[ns, 1] <- ii
						pst[ns, 2] <- jj
						pst[ns, 3] <- kk
						pst[ns, 4] <- ll
						pst[ns, 5] <- mm
						ns <- ns + 1
					}
				}
			}
		}
	}
	return(pst)
}

strats <- strat()

paym <- function(cd, dc, delta){
	pmat <- matrix(0, 32, 32)
	for(ii in 1:32){
		for(jj in 1:32){
			pmat[ii, jj] <- pv(strats[ii,], strats[jj,], cd, dc, delta)
		}
	}
	return(pmat)
}

isdom <- function(p1, p2){
	nn <- NROW(p1)
	for(ii in 1:nn){
		if(p2[ii] > p1[ii]) return(0)
	}
	for(ii in 1:nn){
		if(p1[ii] > p2[ii]) return(1)
	}
	return(0)
}


wdom <- function(pmat){
	doms <- rep(0,32)
	for(ii in 1:32){
		for(jj in 1:32){
			if(isdom(pmat[jj,], pmat[ii,]) == 1){
				doms[ii] <- 1
				break
			}
		}
	}
	return(doms)
}

showdom <- function(doms){
	n <- 0
	for(ii in 1:32){
		if(doms[ii] == 0) {
			#print(strats[ii,])
			n <- n + 1
		}
	}
	print(n)
}

#for(ii in 1:12){
#	print(paste(mono$experiment[ii], mono$CD[ii], mono$delta[ii]))
#	p <- paym(mono$CD[ii], mono$DC[ii], mono$delta[ii])
#	w <- wdom(p)
#	showdom(w)
#}

l1 <- function(hh){
	print(paste(mono$experiment[hh], mono$CD[hh], mono$delta[hh]))
	p <- paym(mono$CD[hh], mono$DC[hh], mono$delta[hh])
	ml1 <- -1000
	for(ii in 1:32){
		newu <- mean(p[ii,])
		if(newu >= ml1) ml1 <- newu
	}
	nbest <- 0
	best <- 0
	for(ii in 1:32){
		newu <- mean(p[ii,])
		if(newu == ml1) {
print(strats[ii,])
			best <- best + 2 - strats[ii,5]
			nbest <- nbest + 1
		}
	}
	print(ml1)
	print(best/nbest)
	print(nbest)
}

for(ii in 1:12) l1(ii)




