Skip to content

Instantly share code, notes, and snippets.

@amih
Created April 9, 2020 19:15
Show Gist options
  • Save amih/e661275a7328c49f2abb64d695c701aa to your computer and use it in GitHub Desktop.
Save amih/e661275a7328c49f2abb64d695c701aa to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/sagigac
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://unpkg.com/gaussian@1.1.0/lib/gaussian.js"></script>
<script src="https://bossanova.uk/jexcel/v3/jexcel.js"></script>
<script src="https://bossanova.uk/jsuites/v2/jsuites.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jexcel/v3/jexcel.css" type="text/css" />
<link rel="stylesheet" href="https://bossanova.uk/jsuites/v2/jsuites.css" type="text/css" />
<style id="jsbin-css">
html, body {
height: 100%;
}
#left, #right {
height: 100%;
display: inline-block;
}
#left {
width: 200px;
margin-right: 60px
}
#right {
width: 420px;
}
#summary, #details {
height: 95%;
border: 1px solid blue;
overflow-y : auto;
}
</style>
</head>
<body>
<div id="left">
<div>Summary</div>
<div id="summary"></div>
</div>
<div id="right">
<div>Details (of only the first few rounds)</div>
<div id="details"></div>
</div>
<script id="jsbin-javascript">
//jshint esnext:true
// noprotect
const gaussian = window.gaussian; // require('gaussian');
const N45 = (mean, sd=45) => gaussian(mean, sd*sd).ppf(Math.random());
const U = (min, max) => Math.floor(Math.random()*(max - min + 1) + min)
const removeSmallest = (arr) => {
arr.sort((a, b) => Math.sign(a.bet - b.bet));
const numOfLosers = arr.reduce((acc, cur) => (cur.bet == arr[0].bet) ? acc+1 : acc, 0);
const loser = arr.splice(U(1, numOfLosers) - 1, 1); // remove one of the losers
return arr;
}
var table1 = jexcel(document.getElementById('details'), {
data:[ [ 2, 10, 'this is just a dummy line', '=B1*C1'] ],
columns: [
{ title: 'Round', type: 'text', width:'50px', },
{ title: 'inner', type: 'text', width:'50px', },
{ title: 'X1', type: 'number', width:'30px', },
{ title: 'X2', type: 'number', width:'30px', },
{ title: 'Player-Strategy', type: 'number', width:'200px', align: 'left'},
{ title: '?', type: 'number', width:'30px', },
{ title: 'Bet', type: 'number', width:'30px', },
],
rowResize: true,
});
var dataS = [ [ 'this dummy line will be removed at the end of the program...', 10 ] ];
var tableS = jexcel(document.getElementById('summary'), {
data:dataS,
columns: [
{ title: 'Player-Strategy', type: 'text', width:'150px', align: 'left'},
{ title: 'wins', type: 'number', width:'50px'},
],
rowResize: true,
});
const sigmoid = (x) => x / (1 + Math.abs(x));
const getPlayers = () => {
let arr = [
{
strategy: 'simple,max(x1,x2)',
func: (X1, X2, T) => X1>X2 ? 'S' : 'R'
},
{
strategy: 'crazy,min(x1,x2)',
func: (X1, X2, T) => X1<X2 ? 'S' : 'R'
},
{
strategy: 'alwaysSafe',
func: (X1, X2, T) => 'S'
},
{
strategy: 'alwaysRisk',
func: (X1, X2, T) => 'R'
},
{
strategy: 'Ilan2(x1>x2-45?S:R)',
func: (X1, X2, T) => X1 > X2 - 45 ? 'S' : 'R'
},
{
strategy: 'Ilan3{.2}(x1>x2-45?S:R)',
func: (X1, X2, T) => {
const param = 0.2;
return X1 > X2 - 2 * param * 45 * Math.log(T - 1) ? 'S' : 'R';
}
},
{
strategy: 'Ilan3{.3}(x1>x2-45?S:R)',
func: (X1, X2, T) => {
const param = 0.3;
return X1 > X2 - 2 * param * 45 * Math.log(T - 1) ? 'S' : 'R';
}
},
{
strategy: 'Ilan4{ asimptote: 1.0, slope: 1.0, delta:1.5 }',
func: (X1, X2, T) => {
const asimptote = 1.0;
const slope = 1.0;
const delta = 1.5;
return X1 > X2 - 2 * asimptote * 45 * sigmoid( slope * (T - delta) ) ? 'S' : 'R';
}
},
];
// multiply each strategy
let mularr = [];
for(let item in arr){
for(let i=0; i<3; i++){
let temp = Object.assign({}, arr[item]);
temp.strategy += i;
mularr.push(temp);
}
}
return mularr;
}
var winnersList = [];
const main = () => {
for(let roundNum = 0; roundNum<10000; roundNum++){
let player = getPlayers();
const games = player.length;
while(player.length>1){
let X1 = U(0,150);
let X2 = U(0,150);
for(i=0; i<player.length; i++){
let decision = player[i].func(X1, X2, player.length);
if(decision == 'S'){
player[i].bet = X1;
}else{
player[i].bet = Math.round(N45(X2));
}
if(roundNum<2){
table1.insertRow([(roundNum+1).toString(), (games - player.length + 1).toString(), X1, X2, player[i].strategy, decision, player[i].bet]);
}
}
player = removeSmallest(player); // find loser and remove him
if(player.length==1){
// tableS.insertRow([(roundNum+1).toString(), player[0].strategy]);
winnersList.push(player[0].strategy);
}
}
}
var winnerFrequency = {};
for(let i=0; i<winnersList.length; i++){
if( winnerFrequency[winnersList[i]]===undefined ){
winnerFrequency[winnersList[i]] = 1;
}else{
winnerFrequency[winnersList[i]]++;
}
}
for(w in winnerFrequency){
tableS.insertRow([w, winnerFrequency[w]]);
}
table1.deleteRow(0,1);
tableS.deleteRow(0,1);
tableS.orderBy(1);
tableS.orderBy(1);
}
main();
</script>
<script id="jsbin-source-css" type="text/css">html, body {
height: 100%;
}
#left, #right {
height: 100%;
display: inline-block;
}
#left {
width: 200px;
margin-right: 60px
}
#right {
width: 420px;
}
#summary, #details {
height: 95%;
border: 1px solid blue;
overflow-y : auto;
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">//jshint esnext:true
// noprotect
const gaussian = window.gaussian; // require('gaussian');
const N45 = (mean, sd=45) => gaussian(mean, sd*sd).ppf(Math.random());
const U = (min, max) => Math.floor(Math.random()*(max - min + 1) + min)
const removeSmallest = (arr) => {
arr.sort((a, b) => Math.sign(a.bet - b.bet));
const numOfLosers = arr.reduce((acc, cur) => (cur.bet == arr[0].bet) ? acc+1 : acc, 0);
const loser = arr.splice(U(1, numOfLosers) - 1, 1); // remove one of the losers
return arr;
}
var table1 = jexcel(document.getElementById('details'), {
data:[ [ 2, 10, 'this is just a dummy line', '=B1*C1'] ],
columns: [
{ title: 'Round', type: 'text', width:'50px', },
{ title: 'inner', type: 'text', width:'50px', },
{ title: 'X1', type: 'number', width:'30px', },
{ title: 'X2', type: 'number', width:'30px', },
{ title: 'Player-Strategy', type: 'number', width:'200px', align: 'left'},
{ title: '?', type: 'number', width:'30px', },
{ title: 'Bet', type: 'number', width:'30px', },
],
rowResize: true,
});
var dataS = [ [ 'this dummy line will be removed at the end of the program...', 10 ] ];
var tableS = jexcel(document.getElementById('summary'), {
data:dataS,
columns: [
{ title: 'Player-Strategy', type: 'text', width:'150px', align: 'left'},
{ title: 'wins', type: 'number', width:'50px'},
],
rowResize: true,
});
const sigmoid = (x) => x / (1 + Math.abs(x));
const getPlayers = () => {
let arr = [
{
strategy: 'simple,max(x1,x2)',
func: (X1, X2, T) => X1>X2 ? 'S' : 'R'
},
{
strategy: 'crazy,min(x1,x2)',
func: (X1, X2, T) => X1<X2 ? 'S' : 'R'
},
{
strategy: 'alwaysSafe',
func: (X1, X2, T) => 'S'
},
{
strategy: 'alwaysRisk',
func: (X1, X2, T) => 'R'
},
{
strategy: 'Ilan2(x1>x2-45?S:R)',
func: (X1, X2, T) => X1 > X2 - 45 ? 'S' : 'R'
},
{
strategy: 'Ilan3{.2}(x1>x2-45?S:R)',
func: (X1, X2, T) => {
const param = 0.2;
return X1 > X2 - 2 * param * 45 * Math.log(T - 1) ? 'S' : 'R';
}
},
{
strategy: 'Ilan3{.3}(x1>x2-45?S:R)',
func: (X1, X2, T) => {
const param = 0.3;
return X1 > X2 - 2 * param * 45 * Math.log(T - 1) ? 'S' : 'R';
}
},
{
strategy: 'Ilan4{ asimptote: 1.0, slope: 1.0, delta:1.5 }',
func: (X1, X2, T) => {
const asimptote = 1.0;
const slope = 1.0;
const delta = 1.5;
return X1 > X2 - 2 * asimptote * 45 * sigmoid( slope * (T - delta) ) ? 'S' : 'R';
}
},
];
// multiply each strategy
let mularr = [];
for(let item in arr){
for(let i=0; i<3; i++){
let temp = Object.assign({}, arr[item]);
temp.strategy += i;
mularr.push(temp);
}
}
return mularr;
}
var winnersList = [];
const main = () => {
for(let roundNum = 0; roundNum<10000; roundNum++){
let player = getPlayers();
const games = player.length;
while(player.length>1){
let X1 = U(0,150);
let X2 = U(0,150);
for(i=0; i<player.length; i++){
let decision = player[i].func(X1, X2, player.length);
if(decision == 'S'){
player[i].bet = X1;
}else{
player[i].bet = Math.round(N45(X2));
}
if(roundNum<2){
table1.insertRow([(roundNum+1).toString(), (games - player.length + 1).toString(), X1, X2, player[i].strategy, decision, player[i].bet]);
}
}
player = removeSmallest(player); // find loser and remove him
if(player.length==1){
// tableS.insertRow([(roundNum+1).toString(), player[0].strategy]);
winnersList.push(player[0].strategy);
}
}
}
var winnerFrequency = {};
for(let i=0; i<winnersList.length; i++){
if( winnerFrequency[winnersList[i]]===undefined ){
winnerFrequency[winnersList[i]] = 1;
}else{
winnerFrequency[winnersList[i]]++;
}
}
for(w in winnerFrequency){
tableS.insertRow([w, winnerFrequency[w]]);
}
table1.deleteRow(0,1);
tableS.deleteRow(0,1);
tableS.orderBy(1);
tableS.orderBy(1);
}
main();
</script></body>
</html>
html, body {
height: 100%;
}
#left, #right {
height: 100%;
display: inline-block;
}
#left {
width: 200px;
margin-right: 60px
}
#right {
width: 420px;
}
#summary, #details {
height: 95%;
border: 1px solid blue;
overflow-y : auto;
}
//jshint esnext:true
// noprotect
const gaussian = window.gaussian; // require('gaussian');
const N45 = (mean, sd=45) => gaussian(mean, sd*sd).ppf(Math.random());
const U = (min, max) => Math.floor(Math.random()*(max - min + 1) + min)
const removeSmallest = (arr) => {
arr.sort((a, b) => Math.sign(a.bet - b.bet));
const numOfLosers = arr.reduce((acc, cur) => (cur.bet == arr[0].bet) ? acc+1 : acc, 0);
const loser = arr.splice(U(1, numOfLosers) - 1, 1); // remove one of the losers
return arr;
}
var table1 = jexcel(document.getElementById('details'), {
data:[ [ 2, 10, 'this is just a dummy line', '=B1*C1'] ],
columns: [
{ title: 'Round', type: 'text', width:'50px', },
{ title: 'inner', type: 'text', width:'50px', },
{ title: 'X1', type: 'number', width:'30px', },
{ title: 'X2', type: 'number', width:'30px', },
{ title: 'Player-Strategy', type: 'number', width:'200px', align: 'left'},
{ title: '?', type: 'number', width:'30px', },
{ title: 'Bet', type: 'number', width:'30px', },
],
rowResize: true,
});
var dataS = [ [ 'this dummy line will be removed at the end of the program...', 10 ] ];
var tableS = jexcel(document.getElementById('summary'), {
data:dataS,
columns: [
{ title: 'Player-Strategy', type: 'text', width:'150px', align: 'left'},
{ title: 'wins', type: 'number', width:'50px'},
],
rowResize: true,
});
const sigmoid = (x) => x / (1 + Math.abs(x));
const getPlayers = () => {
let arr = [
{
strategy: 'simple,max(x1,x2)',
func: (X1, X2, T) => X1>X2 ? 'S' : 'R'
},
{
strategy: 'crazy,min(x1,x2)',
func: (X1, X2, T) => X1<X2 ? 'S' : 'R'
},
{
strategy: 'alwaysSafe',
func: (X1, X2, T) => 'S'
},
{
strategy: 'alwaysRisk',
func: (X1, X2, T) => 'R'
},
{
strategy: 'Ilan2(x1>x2-45?S:R)',
func: (X1, X2, T) => X1 > X2 - 45 ? 'S' : 'R'
},
{
strategy: 'Ilan3{.2}(x1>x2-45?S:R)',
func: (X1, X2, T) => {
const param = 0.2;
return X1 > X2 - 2 * param * 45 * Math.log(T - 1) ? 'S' : 'R';
}
},
{
strategy: 'Ilan3{.3}(x1>x2-45?S:R)',
func: (X1, X2, T) => {
const param = 0.3;
return X1 > X2 - 2 * param * 45 * Math.log(T - 1) ? 'S' : 'R';
}
},
{
strategy: 'Ilan4{ asimptote: 1.0, slope: 1.0, delta:1.5 }',
func: (X1, X2, T) => {
const asimptote = 1.0;
const slope = 1.0;
const delta = 1.5;
return X1 > X2 - 2 * asimptote * 45 * sigmoid( slope * (T - delta) ) ? 'S' : 'R';
}
},
];
// multiply each strategy
let mularr = [];
for(let item in arr){
for(let i=0; i<3; i++){
let temp = Object.assign({}, arr[item]);
temp.strategy += i;
mularr.push(temp);
}
}
return mularr;
}
var winnersList = [];
const main = () => {
for(let roundNum = 0; roundNum<10000; roundNum++){
let player = getPlayers();
const games = player.length;
while(player.length>1){
let X1 = U(0,150);
let X2 = U(0,150);
for(i=0; i<player.length; i++){
let decision = player[i].func(X1, X2, player.length);
if(decision == 'S'){
player[i].bet = X1;
}else{
player[i].bet = Math.round(N45(X2));
}
if(roundNum<2){
table1.insertRow([(roundNum+1).toString(), (games - player.length + 1).toString(), X1, X2, player[i].strategy, decision, player[i].bet]);
}
}
player = removeSmallest(player); // find loser and remove him
if(player.length==1){
// tableS.insertRow([(roundNum+1).toString(), player[0].strategy]);
winnersList.push(player[0].strategy);
}
}
}
var winnerFrequency = {};
for(let i=0; i<winnersList.length; i++){
if( winnerFrequency[winnersList[i]]===undefined ){
winnerFrequency[winnersList[i]] = 1;
}else{
winnerFrequency[winnersList[i]]++;
}
}
for(w in winnerFrequency){
tableS.insertRow([w, winnerFrequency[w]]);
}
table1.deleteRow(0,1);
tableS.deleteRow(0,1);
tableS.orderBy(1);
tableS.orderBy(1);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment