CDT++
Causal Dynamical Triangulations in C++
Loading...
Searching...
No Matches
optimize-initialize.py
1# Causal Dynamical Triangulations in C++ using CGAL
2#
3# Copyright © 2018 Adam Getchell
4#
5# A program that optimizes spacetime generation parameters
6
7# @file optimize-initialize.py
8# @brief Optimize spacetime generation
9# @author Adam Getchell
10
11# Usage: python optimize-initialize.py
12#
13from __future__ import absolute_import, division, print_function
14
15import os
16import traceback
17
18# Import Comet.ml
19from comet_ml import Experiment
20# from comet_ml import Optimizer
21import comet_ml as cm
22
23# Import TensorFlow
24# import tensorflow as tf
25# import tensorflow.contrib.eager as tfe
26import matplotlib.pyplot as plt
27import numpy as np
28
29# Run command line programs
30import shlex
31from subprocess import check_output as qx
32import re
33
34# Create an optimizer for dynamic parameters
35# optimizer = Optimizer(api_key=os.environ['COMET_API_KEY'])
36# params = """
37# initial_radius integer [1, 2] [1]
38# foliation_spacing integer [1, 2] [1]
39# """
40#
41# optimizer.set_params(params)
42
43# tf.enable_eager_execution()
44
45# Create parameters to vary
46parameters = [(initial_radius, spacing) for initial_radius in range(1, 4) for spacing in np.arange(1, 2.5, 0.5)]
47
48try:
49 # while True:
50 # Get a suggestion
51 # suggestion = optimizer.get_suggestion()
52 for parameter_pair in parameters:
53
54 # Create an experiment with api key
55 experiment = Experiment(api_key=os.environ['COMET_API_KEY'], project_name="cdt-plusplus", team_name="ucdavis")
56
57 # print('TensorFlow version: {}'.format(tf.VERSION))
58
59 hyper_params = {'simplices': 12000, 'foliations': 12}
60 experiment.log_multiple_params(hyper_params)
61 # init_radius = suggestion["initial_radius"]
62 init_radius = parameter_pair[0]
63 # radial_factor = suggestion["foliation_spacing"]
64 radial_factor = parameter_pair[1]
65
66 command_line = "../build/src/initialize --s -n" + str(experiment.get_parameter("simplices")) \
67 + " -t" + str(experiment.get_parameter("foliations")) + " -i" + str(init_radius) \
68 + " -f" + str(radial_factor)
69 args = shlex.split(command_line)
70
71 print(args)
72
73 output = qx(args)
74
75 # Parse output into a list of [simplices, min timeslice, max timeslice]
76 result = [0, 0, 0]
77 graph = []
78 for line in output.splitlines():
79 if line.startswith("Minimum timevalue"):
80 s = re.findall('\d+', line)
81 result[1] = float(s[0])
82 elif line.startswith("Maximum timevalue"):
83 s = re.findall('\d+', line)
84 result[2] = float(s[0])
85 elif line.startswith("Final number"):
86 # print(line)
87 s = re.findall('\d+', line)
88 # print(s)
89 result[0] = float(s[0])
90 elif line.startswith("Timeslice"):
91 t = re.findall('\d+', line)
92 graph.append(t)
93
94 print(result)
95 print('Initial radius is: {}'.format(init_radius))
96 print('Radial factor is: {}'.format(radial_factor))
97 for element in graph:
98 print("Timeslice {} has {} spacelike faces.".format(element[0], element[1]))
99 print("")
100
101 # Score model
102 score = ((result[0] - experiment.get_parameter("simplices"))/(experiment.get_parameter("simplices")))*100
103
104 # Report results
105 experiment.log_metric("Error %", score)
106 experiment.log_other("Min Timeslice", result[1])
107 experiment.log_other("Max Timeslice", result[2])
108
109 # Graph volume profile
110 timeslice = []
111 volume = []
112 for element in graph:
113 timeslice.append(int(element[0]))
114 volume.append(int(element[1]))
115 plt.plot(timeslice, volume)
116 plt.xlabel('Timeslice')
117 plt.ylabel('Volume (spacelike faces)')
118 plt.title('Volume Profile')
119 plt.grid(True)
120 experiment.log_figure(figure_name="Volume per Timeslice", figure=plt)
121 plt.clf()
122
123
124except cm.exceptions.NoMoreSuggestionsAvailable as NoMore:
125 print("No more suggestions.")
126
127except (TypeError, KeyError):
128 pass
129
130finally:
131 traceback.print_exc()
132
133print("All done with parameter optimization, look at Comet.ml for results.")