Skip to content

Commit

Permalink
traffic example
Browse files Browse the repository at this point in the history
  • Loading branch information
allenanie committed Jun 24, 2024
1 parent 4f5435f commit f969a1a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
Binary file added docs/images/traffic_opt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 83 additions & 8 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -533,23 +533,98 @@ <h2 class="display-2 mb-4">Showcases</h2>
we can directly compare LLM's ability to find the optimal solution against the classical numerical optimizers like gradient descent.
In this case, the Trace graph is equivalent to the computation graph constructed by PyTorch -- representing the underlying numerical operations over <code>x</code>.
</p>
<div class="text-center">
<img src="images/opt_fig.png" style="width:30%">
</div>
<p>We run 30 trials over different randomly generated problems. All methods see the same randomness. On
average, Trace is able to match the best-in-class Adam; on the other hand, without access to the full
computational graph, the optimizer alone struggles to find the optimal <code>x</code>.</p>
<pre>
<code class="language-python">
from trace import node
from trace import node, GRAPH

program = NumericalProgramSampler(chain_length=7, param_num=1, max_gen_var=4)
x = node(-1.0, "input_x", trainable=True)

z.backward(feedback="Output should be larger.", visualize=True)</code>
for i in tqdm(range(n_steps)):
GRAPH.clear()

if feedback.lower() == "Success.".lower():
break

try:
output = program(x, seed=program_id)
feedback = program.feedback(output.data)
except TraceExecutionError as e:
output = e.exception_node
feedback = output.data

optimizer.zero_feedback()
optimizer.backward(output, feedback)
optimizer.step()</code>
</pre>
<div class="text-center">
<img src="images/opt_fig.png" style="width:40%">
</div>

</div>
<div class="tab-pane fade" id="tabs-icons-text-111" role="tabpanel" aria-labelledby="tabs-icons-text-111-tab">
<p>Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus,
Marfa eiusmod Pinterest in do umami readymade swag.</p>
<p>Day handsome addition horrible sensible goodness two contempt. Evening for married his account removal. Estimable me disposing of be moonlight cordially curiosity.</p>
<p>We tested Trace in a traffic control problem which is an instance of hyper-parameter tuning. We used <a href="https://toruseo.jp/UXsim/docs/">UXSim</a> to simulate traffic at a four-way intersection, where the trainable parameters are 2 integers in [15,90], which are the green light duration for each direction of traffic flow. </p>
<div class="text-center">
<img alt="https://raw.githubusercontent.com/toruseo/UXsim/images/gridnetwork_macro.gif" src="https://raw.githubusercontent.com/toruseo/UXsim/images/gridnetwork_macro.gif" style="width: 25%;">
<p class="font-small">Image sourced from UXSim Website</p>
</div>
<p>The feedback is the estimated delay experienced by all vehicles due to intersections, and the goal of an
optimizer is to minimize the delay using the fewest number of traffic simulations. To this end, this
optimizer must find the right trade-off for temporally distributed and variable demands.</p>
<p> We report the performance of a SOTA heuristic from the traffic control literature, SCATS as
well as two black-box optimization techniques: Gaussian Process Minimization (GP) and Particle
Swarm Optimization (PSO). All methods use the same starting parameters.</p>
<div class="text-center">
<img src="images/traffic_opt.png" style="width:30%">
</div>
<p>GP and PSO appear bad because 50 iterations are insufficient for their
convergence; given enough iterations, both will eventually perform well. Trace is quickly competitive
with the SCATS heuristic, whereas OPRO is not. We show the code sketch below. Trace sends a node object into the simulator
and let the environment operate on it. The underlying operation logic is automatically revealed to the Trace optimizer.</p>
<pre>
<code class="language-python">
from trace import node, GRAPH

def traffic_simulation(EW_green_time, NS_green_time):
W = None
try:
W = create_world(EW_green_time, NS_green_time)
except Exception as e:
e_node = ExceptionNode(
e,
inputs={"EW_green_time": EW_green_time, "NS_green_time": NS_green_time},
description="[exception] Simulation raises an exception with these inputs.",
name="exception_step",
)
return e_node
W.data.exec_simulation()
return_dict = analyze_world(W, verbosity)

return return_dict

EW_x = trace.node(MIN_GREEN_TIME, trainable=True, constraint=f"[{MIN_GREEN_TIME},{MAX_GREEN_TIME}]")
NS_x = trace.node(MIN_GREEN_TIME, trainable=True, constraint=f"[{MIN_GREEN_TIME},{MAX_GREEN_TIME}]")

optimizer.objective = (
"You should suggest values for the variables so that the OVERALL SCORE is as small as possible.\n"
+ "There is a trade-off in setting the green light durations.\n"
+ "If the green light duration for a given direction is set too low, then vehicles will queue up over time and experience delays, thereby lowering the score for the intersection.\n"
+ "If the green light duration for a given direction is set too high, vehicles in the other direction will queue up and experience delays, thereby lowering the score for the intersection.\n"
+ "The goal is to find a balance for each direction (East-West and North-South) that minimizes the overall score of the intersection.\n"
+ optimizer.default_objective
)

for i in range(num_iter):
result = traffic_simulation(EW_x, NS_x)
# some steps skipped for simplicity
feedback = result.data
optimizer.zero_feedback()
optimizer.backward(result, feedback, visualize=True)
optimizer.step()</code>
</pre>
</div>
<div class="tab-pane fade" id="tabs-icons-text-112" role="tabpanel" aria-labelledby="tabs-icons-text-112-tab">
<p>Photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod
Expand Down

0 comments on commit f969a1a

Please sign in to comment.