Skip to content

Commit

Permalink
add individual network speed params
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Sep 9, 2024
1 parent cc6cbf9 commit 8957ca7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
18 changes: 18 additions & 0 deletions matsim/scenariogen/network/ref_model/individual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
""" Reference model to fine tune individal links. This model does not use features and is not trasferable to other scenarios. """

# Each link has its own target speed


params = None

def score(params, inputs):
return params[inputs[0]]


def batch_loss(params, inputs, targets):
error = 0
inputs = inputs.astype(int)
for x, y in zip(inputs, targets):
preds = score(params, x)
error += (preds - y) ** 2
return error
21 changes: 11 additions & 10 deletions matsim/scenariogen/network/runOPT.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/bin/bash --login
#$ -l h_rt=700000
#$ -j y
#$ -m a
#$ -o ./logfile/logfile_$JOB_NAME.log
#$ -cwd
#$ -pe mp 6
#$ -l mem_free=4G
#$ -l cpuflag_avx2=1
#$ -N network-opt
#SBATCH --time=200:00:00
#SBATCH --partition=smp
#SBATCH --output=./logfile/logfile_%x-%j.log
#SBATCH --nodes=1 # How many computing nodes do you need (for MATSim usually 1)
#SBATCH --ntasks=1 # How many tasks should be run (For MATSim usually 1)
#SBATCH --cpus-per-task=6 # Number of CPUs per task (For MATSim usually 8 - 12)
#SBATCH --mem=12G # RAM for the job
#SBATCH --job-name=network-opt # name of your run, will be displayed in the joblist
#SBATCH --constraint=cpuflag_avx2:1
#SBATCH --mail-type=END,FAIL

date
hostname

source venv/bin/activate
module add java/17
module add java/21

jar="matsim-[name]-SNAPSHOT.jar"
input="input/*"
Expand Down
19 changes: 17 additions & 2 deletions matsim/scenariogen/network/run_opt_freespeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def setup(parser: argparse.ArgumentParser):
parser.add_argument("--resume", help="File with parameters to to resume", default=None)
parser.add_argument("--port", type=int, nargs="+", help="Port to connect on", default=[9090])
parser.add_argument("--ref-model", required=False, default=None,
help="Use an integrated model instead of importing", choices=["tree", "germany"])
help="Use an integrated model instead of importing", choices=["tree", "individual", "germany"])
parser.add_argument("--ref-size", type=int, help="Number of links (needed for individual model)", default=0)
parser.add_argument("--learning-rate", type=float, help="Start learning rate", default=1e-4)
parser.add_argument("--batch-size", type=int, help="Batch size", default=128)
parser.add_argument("--output", help="Output folder for params", default="output-params")
Expand All @@ -77,6 +78,15 @@ def main(args):
if args.ref_model == "tree":
from .ref_model import tree as p
rbl = tl = p
elif args.ref_model == "individual":
# Enables non relative import as well
try:
from .ref_model import individual as p
rbl = tl = p
except ImportError:
from ref_model import individual as p
rbl = tl = p

elif args.ref_model == "germany":
from .ref_model.germany import speedRelative_priority as p
from .ref_model.germany import speedRelative_right_before_left as rbl
Expand All @@ -100,8 +110,13 @@ def main(args):
staircase=False
)

# Need to load default params statically
if module.params is None:
params = jnp.array(resume[name]) if name in resume else jnp.ones(args.ref_size) * 0.85
else:
params = jnp.array(resume[name] if name in resume else module.params)

optimizer = optax.adam(schedule)
params = jnp.array(resume[name] if name in resume else module.params)
opt_state = optimizer.init(params)

models[name] = Model(
Expand Down

0 comments on commit 8957ca7

Please sign in to comment.