Skip to content

Machine learning #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 113 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
71d5db7
Resolved compile errors and made the project runnable
Jul 24, 2019
bdd8119
connected bayesian optimizer of python to bo.Main
Jul 24, 2019
e97a47a
added a machinery for input/output generation and evaluation
Jul 25, 2019
0c1f154
change scala call from python
vrupp Jul 25, 2019
3084af9
resolved merge conflicts sbt call
vrupp Jul 25, 2019
a15f26d
added scaladoc comments for BOUtil
Jul 25, 2019
ff25c6e
call sbt package
vrupp Jul 25, 2019
145a6c5
call sbt package
vrupp Jul 25, 2019
ab35e31
added params to BO
vrupp Jul 26, 2019
6bdd2d3
added the basic tool for calculating adapted lyapunov exponent
Jul 29, 2019
a38758f
refactored params into simulation class
vrupp Jul 29, 2019
cfdf226
added simple visualization tools
Jul 29, 2019
a94b2ef
read params from json
vrupp Jul 30, 2019
40473e9
added json support to bayesian_optimizer.py
Jul 30, 2019
85d49f6
added a train/test model for evaluating bayesian optimization perform…
Jul 31, 2019
97c4199
added a train/test model for evaluating bayesian optimization perform…
Jul 31, 2019
de18b53
Merge remote-tracking branch 'origin/master'
Jul 31, 2019
d04063d
added params to simulation
vrupp Jul 31, 2019
b097d13
changed error function
vrupp Jul 31, 2019
98ee926
added factory params
vrupp Aug 2, 2019
e2d2cc1
tuned the params for meaningful output, added two-mode plotting from …
Aug 5, 2019
34389a6
fixed error calculation and log with buffer
vrupp Aug 6, 2019
2c645f1
added partial-parameter optimization to python
Aug 6, 2019
d0a8cc6
plot multiple lines with same intervall, fix param intervalls for BO
vrupp Aug 6, 2019
0987cfa
simplified optimization process and removed inputs
Aug 7, 2019
925ebbf
modified bayesian optimization loop
Aug 7, 2019
cb1ec06
added params for gender and capital, normalized viz output
vrupp Aug 8, 2019
31423fd
removed output ranges
vrupp Aug 8, 2019
90c74d9
added observables, fixed bug empty pl
vrupp Aug 9, 2019
1becf3d
fixed viz bugs
vrupp Aug 9, 2019
e86e3f2
write data to csv before plotting
vrupp Aug 9, 2019
e7f2367
set higher resolution for viz
vrupp Aug 9, 2019
58ed859
generate csv for train set
vrupp Aug 12, 2019
0c88406
added the first implementation of rnn
Aug 12, 2019
23bc72c
created time series train csv data
vrupp Aug 12, 2019
aec76f5
added training from csv to rnn.py
Aug 12, 2019
a864170
created csv for x and y variable
vrupp Aug 12, 2019
91195b0
added cross fold validation
vrupp Aug 13, 2019
a8bf578
made it able to run on gpu
vrupp Aug 13, 2019
8d2d9fc
added simple version of rnn for test/debug
Aug 14, 2019
a0af531
removed all unused classes and methods
vrupp Aug 22, 2019
4cbb3c9
fixed salary bug
vrupp Aug 22, 2019
2817c0c
added factories and params for their pls
vrupp Aug 23, 2019
b00dae5
added agent.py
Aug 26, 2019
abed14c
create csv for training a person agent
vrupp Aug 26, 2019
25e1869
added env.py
Aug 26, 2019
d43c827
added example for using enivornment/agents
Aug 26, 2019
e3b5036
scaled input
vrupp Aug 26, 2019
1b9cd5d
implemented a high-level(relationship graph)/low-level(bunch of netwo…
Aug 27, 2019
7fb4699
mapped values between env and graph
vrupp Aug 28, 2019
065ac9b
correlation matrix for agent
vrupp Aug 29, 2019
1a366b1
derivative matrix
vrupp Aug 29, 2019
70cdecd
Setting up the experiment: cleaning up parameters
Aug 30, 2019
72da1f6
Setting up the experiment: speicalized factories into different agent…
Aug 30, 2019
d6a78fc
Setting up the experiment: added global statistics for joint training
Aug 31, 2019
7f6224a
implemented group train
vrupp Sep 1, 2019
c5eda0c
added two testing scenarios for the network model
Sep 2, 2019
8b40281
changed double type of variables to function
Aug 30, 2019
6684340
updated generation and evaluation data for BO
vrupp Sep 2, 2019
2f63ce9
renamed params and added params in BO python file
vrupp Sep 3, 2019
8180a38
added single_nn.py for comparison with our network
Sep 3, 2019
b6bcd0e
fixed typo
vrupp Sep 3, 2019
2bb77b7
added the option of configuring low-level network while defining an a…
Sep 3, 2019
068efd6
Merge remote-tracking branch 'origin/master'
Sep 3, 2019
3dcbccc
set up environment configuration for our simulation plus solo trainin…
Sep 4, 2019
e8b5cf7
added analysis tool for simdata/debugged random initialization bug
Sep 5, 2019
0d25b97
learn input values with tf optimizer
vrupp Sep 5, 2019
b2696ea
devided state indicators to (initializable) variables and (over time)…
Sep 5, 2019
2ec3975
added saving and loading ability to nn models
Sep 5, 2019
8f3656a
implemented group train for simulation
vrupp Sep 5, 2019
f7a74cd
group train for simulation
vrupp Sep 6, 2019
81fce6f
implemented input learning for simulation
vrupp Sep 6, 2019
b3e41ab
implemented prediction over time/added scale save-load
Sep 9, 2019
a703278
resolved a bug with global statistics (Source is a sim but not an agent)
Sep 9, 2019
e293602
Partially updated Readme
alirezamoosio Sep 9, 2019
6ce7e52
python code done! except debugging
Sep 9, 2019
a1a1b5d
added recursion depth for better debugging
vrupp Sep 9, 2019
bbeca8c
added python docstring for some files (not complete)
Sep 10, 2019
60c60f5
Update README.md
alirezamoosio Sep 10, 2019
47259ff
Update README.md
alirezamoosio Sep 10, 2019
e82ab97
Update README.md - fixing link bugs
alirezamoosio Sep 10, 2019
3bff5e7
Merge remote-tracking branch 'origin/master'
Sep 10, 2019
827cd3e
fixed bug with copying agents' constants
vrupp Sep 10, 2019
be73f6b
changed BO for new json format and equalized param ranges
vrupp Sep 10, 2019
efb0364
simplified bayesian optimization
vrupp Sep 11, 2019
2c1b7d7
fixing typo
vrupp Sep 11, 2019
46a9fd6
saving intermediate results BO
vrupp Sep 11, 2019
3b1ea8b
Another update to the README.md!
alirezamoosio Sep 11, 2019
567974d
added learning rate for group training and input learning
Sep 11, 2019
0093912
added supplementary files and changed readme
Sep 11, 2019
70049c7
write bo target in result json
vrupp Sep 11, 2019
04541a0
refactored network and bo for supplementary directory
vrupp Sep 11, 2019
d7621d6
moved params.json to supplementary
vrupp Sep 11, 2019
0b0ae97
added sigma variables to data generation and network configuration
Sep 12, 2019
5aa12a8
tuned single_nn's config and added 0 sd support
Sep 12, 2019
3bd9d62
wrote results of input learning in json file
vrupp Sep 12, 2019
aacc814
minimum 2 agents per type
vrupp Sep 12, 2019
0e7e7ea
added batch-prediction
Sep 12, 2019
7e18cdf
removed todos and added docstring
vrupp Sep 12, 2019
c940bc5
fixed the 0 sd problem of single_nn.py
Sep 12, 2019
2cace38
Merge branch 'master' of https://github.com/alirezamoosio/economic_si…
Sep 12, 2019
db8762d
adding datasets with 3000 lines
vrupp Sep 13, 2019
d334845
models with new dataset
vrupp Sep 13, 2019
4797c1e
comment assertion because of imprecisely rounding
vrupp Sep 16, 2019
b8fc91a
added global stat scaling for error measurement in scala
Sep 19, 2019
bd5f5c1
splitted dataset into differently generated train and test data
Sep 27, 2019
87acd82
splitted dataset into differently generated train and test data
Sep 27, 2019
99f33c4
Merge remote-tracking branch 'origin/master'
Sep 27, 2019
b48de67
changed all group metrics to mae for consistency
Sep 28, 2019
9c95be4
fixed an input-learning bug, the same input parameters for different …
Sep 29, 2019
62abfb3
added the ability to run the simulation directly from bo.Main
Sep 30, 2019
3bd7d43
moved files to ml subdirectory
Oct 3, 2019
bb2af50
moved everything to machine-learning branch
Nov 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.idea/
project/
target/

# python
__pycache__/

# conda env config file
*.cfg

# models and parameters
*.json
!supplementary/params/params.json
!supplementary/simulation.json
!data_vec.json

# debug
debug/
1 change: 0 additions & 1 deletion README.md

This file was deleted.

206 changes: 206 additions & 0 deletions ml/README.md

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion build.sbt → ml/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ lazy val root = (project in file(".")).settings(
scalaVersion := "2.11.8"
)

mainClass in (Compile, packageBin) := Some("ml_supplements.Main")
mainClass in (Compile, run) := Some("ml_supplements.Main")


// libraryDependencies += "com.quantifind" %% "wisp" % "0.0.4"

libraryDependencies ++= Seq(
// "com.github.fommil.netlib" % "all" % "1.1.2",
"org.scalanlp" %% "breeze" % "0.12",
// "org.scalanlp" %% "breeze-natives" % "0.12",
"org.scalanlp" %% "breeze-viz" % "0.12",
"org.scalatest" %% "scalatest" % "3.0.0" % "test"
"org.scalatest" %% "scalatest" % "3.0.0" % "test",

"io.spray" %% "spray-json" % "1.3.5",

// https://mvnrepository.com/artifact/org.apache.commons/commons-csv
"org.apache.commons" % "commons-csv" % "1.7"

)

resolvers += "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
Expand Down
66 changes: 66 additions & 0 deletions ml/src/main/python/aggregator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from abc import ABC, abstractmethod

import pandas as pd
import tensorflow as tf


class Aggregator(ABC):
def __init__(self, output_names):
super().__init__()
self._output_names = output_names

@abstractmethod
def aggregate(self, output_tensors, n_samples, indices):
pass

def aggregate_pd(self, agent_outputs):
output_tensors = {agent: tf.constant(agent_outputs[agent].to_numpy()) for agent in
agent_outputs}
n_samples = agent_outputs[list(agent_outputs.keys())[0]].shape[0]
indices = {agent: {name: i for i, name in enumerate(agent_outputs[agent].columns.values)} for agent in agent_outputs}
return pd.DataFrame(tf.keras.backend.eval(self.aggregate(output_tensors, n_samples, indices)), columns=self._output_names)


class GlobalStateAggregator(Aggregator):
def __init__(self, np_population, output_names):
super().__init__(output_names)
self._np_population = np_population

"""
When speaking of agents, nodes can also be meant
"""
def aggregate(self, output_tensors, n_samples, indices):
dtype = output_tensors[list(output_tensors.keys())[0]].dtype
average_names = ["capital", "total_value_destroyed", "happiness", "valueProduced", "goodwill"]
result = {
average_name: tf.concat([
tf.reshape(output_tensors[agent][:, indices[agent]["{}.var_{}Mu".format(agent.name, average_name)]], (n_samples, 1))
for agent in output_tensors if "{}.var_{}Mu".format(agent.name, average_name) in indices[agent]
], axis=1)
for average_name in average_names
}
result = {average_name: tf.math.reduce_mean(result[average_name], axis=1) for average_name in average_names}
employees = tf.concat([tf.reshape(output_tensors[agent][:, indices[agent][agent.name + ".var_employeesMu"]], (n_samples, 1))
for agent in output_tensors if agent.name + ".var_employeesMu" in indices[agent]], axis=1)
employees = tf.math.reduce_sum(employees, axis=1)
result["unemploymentRate"] = tf.ones(n_samples, dtype) - tf.math.divide(employees, tf.constant(self._np_population.values, dtype))
return tf.concat([tf.reshape(result[output_name], (n_samples, 1)) for output_name in result], axis=1)


class DummyAggregator(Aggregator):
def __init__(self, output_names):
super().__init__(output_names)

def aggregate(self, output_tensors, n_samples, indices):
dtype = output_tensors[list(output_tensors.keys())[0]].dtype
result = tf.zeros(shape=(n_samples, 2), dtype=dtype)
p3 = tf.zeros(n_samples, dtype=dtype)
for agent in output_tensors:
i_s = indices[agent]
result += tf.slice(output_tensors[agent], [0, 0], result.shape)
if "p3" in i_s:
p3 += output_tensors[agent][:, i_s["p3"]]
s1 = tf.reshape(result[:, 0], shape=(n_samples, 1))
s2 = tf.reshape(tf.math.multiply(result[:, 1], p3), shape=(n_samples, 1))
result = tf.concat([s1, s2], axis=1)
return result
132 changes: 132 additions & 0 deletions ml/src/main/python/bayesian_optimizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import json
import os
import subprocess

import numpy as np
import sys
from bayes_opt import BayesianOptimization, UtilityFunction


def toJson(params):
agentTypes = {
"constants": set(),
"variables": set()
}
for key in params:
split = key.split("-")
agentTypes[split[0]].add(split[1])
return {
type: {
agentType: {
key.split("-")[2]: params[key] for key in params if key.split("-")[0:2] == [type, agentType]
} for agentType in agentTypes[type]
} for type in agentTypes
}


def black_box_function(stepSize, entry, **params):
all_params.update(toJson(params))
f = open(json_temp, "w")
f.write(json.dumps(all_params))
f.close()

result = runCmd('sbt --warn "run evaluate {} {} {}"'.format(json_temp, stepSize, entry))
target = -float(result.decode("utf-8")[:-1])
print(target)
return target


def runCmd(cmd):
print("Run command:", cmd)
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(result, error) = process.communicate()
rc = process.wait()
if rc != 0:
print("Error: failed to execute command:", cmd)
print(result.decode("utf-8"), error.decode("utf-8"))
sys.exit(rc)
return result


if __name__ == '__main__':
if len(sys.argv) != 2:
raise Exception("number of bo iterations required")

os.chdir('../../..') # going to the root of the project
json_original = 'supplementary/params/params.json'
json_optimize = "supplementary/params/optimize.json"
json_temp = 'supplementary/params/temp.json'
json_result = 'supplementary/params/bo-result.json'

f = open(json_original, "r")
all_params = json.loads(f.read())
f.close()

f = open(json_optimize, "r")
params = json.loads(f.read())
f.close()

pbounds = {}
constants = params["constants"]
variables = params["variables"]
for agentType in constants:
append = {
"number": (100, 200) if agentType == "Person" else (2, 10),
**{gender: (0, 1) for gender in constants[agentType] if "gender" in gender.lower()},
**{units: (1, 10) for units in constants[agentType] if units in ["buyMu", "buySigma", "consumeMu", "consumeSigma"]},
**{buyFood: (0, 1) for buyFood in constants[agentType] if "buy" in buyFood.lower() and buyFood not in ["buyMu", "buySigma"]},
**{consumeFood: (0, 1) for consumeFood in constants[agentType] if "consume" in consumeFood.lower()
and consumeFood not in ["consumeMu", "consumeSigma"]},
**{enjoyment: (0, 1000) for enjoyment in constants[agentType] if "enjoy" in enjoyment.lower()},
**{edu: (0, 10) for edu in constants[agentType] if "edu" in edu.lower()},
**{bonusSal: (0, 1000) for bonusSal in constants[agentType] if "bonussal" in bonusSal.lower()},
**{active: (0, 1) for active in constants[agentType] if "active" in active.lower()},
**{salary: (10 ** 4, 10 ** 6) for salary in constants[agentType] if "salary" in salary.lower()},
**{iters: (0, 20) for iters in constants[agentType] if "iters" in iters.lower()},
**{tactics: (0, 1) for tactics in constants[agentType] if "tactics" in tactics.lower()},
**{plUnits: (1, 20) for plUnits in constants[agentType] if plUnits in ["required", "consumed", "produced", "time"]},
**{units: (1, 1000) for units in ["units"] if agentType == "Landlord"},
**{price: (10 ** 5, 10 ** 7) for price in ["price"] if agentType == "Landlord"}
}
pbounds = {**pbounds, **{("constants-{}-{}".format(agentType, key)): append[key] for key in append}}
for agentType in variables:
append = {
**{capital: (0, 10000) for capital in variables[agentType] if "capital" in capital.lower()},
**{value_destroyed: (0, 100) for value_destroyed in variables[agentType] if "value_destroyed" in value_destroyed.lower()},
**{happiness: (0, 100) for happiness in variables[agentType] if "happiness" in happiness.lower()},
**{salary: (10 ** 4, 10 ** 6) for salary in variables[agentType] if "salary" in salary.lower()}
}
pbounds = {**pbounds, **{("variables-{}-{}".format(agentType, key)): append[key] for key in append}}

stepSizes = [20, 50, 100]
sampleSize = 5
nSteps = 1
optimization_iters = int(sys.argv[1])
params_result = {"stepSize-{}".format(stepSize): {"entry-{}".format(entry): {} for entry in range(sampleSize)} for stepSize in stepSizes}

runCmd("sbt clean compile")
for stepSize in stepSizes:
runCmd('sbt "run generate {} {} {} {}"'.format(json_original, sampleSize, nSteps, stepSize))
targets = np.empty(shape=sampleSize)
for entry in range(sampleSize):
optimizer = BayesianOptimization(None, pbounds, 10)
utility = UtilityFunction(kind="ei", kappa=2.5, xi=0.0)
for i in range(optimization_iters):
print("stepSize:", stepSize, "entry:", entry, "iteration:", i)
next_point = optimizer.suggest(utility)
target = black_box_function(stepSize, entry, **next_point)
optimizer.register(params=next_point, target=target)
print()

all_params.update(toJson(optimizer.max['params']))
params_result["stepSize-{}".format(stepSize)]["entry-{}".format(entry)].update(all_params)
targets[entry] = optimizer.max["target"]
params_result["stepSize-{}".format(stepSize)]["entry-{}".format(entry)]["target"] = targets[entry]

f = open(json_result, "w")
f.write(json.dumps(params_result))
f.close()
params_result["stepSize-{}".format(stepSize)]["mean-target"] = targets.mean()
f = open(json_result, "w")
f.write(json.dumps(params_result))
f.close()
Loading