The Geeks Shall Inherit the Earth

Personal Ramblings of the Techhouse Admin

Skip to: Content | Sidebar | Footer

Train Yard Data in Records

29 April, 2010 (20:57) | Erlang | By: Brian

I wanted to start on the second problem in the train yard assignment in Erlang. But I realized that my data structure for a yard was pretty cumbersome. I wanted to mess with the Record structure system, so I created a header file with the record and test data definitions.

%%% train yard data structures
-record(yard, {track_list}).
-record(state, {yard_state}).

%%% problem structure
-record(problem, {yard = #yard{}, init_state = #state{}, goal_state = #state{}}).

%%% problems

problem1() ->
 #problem{yard = [{t1, t2}, {t1, t3}, {t3, t5}, {t4, t5}, {t2, t6}, {t5, t6}],
 init_state = [{t1, [engine]}, {t2, [e]}, {t4, [b, c, a]}, {t6, [d]}],
 goal_state = [{t1, [engine, a, b, c, d, e]}] }.
problem2() ->
 #problem{yard = [{t1, t2}, {t2, t3}, {t2, t4}, {t1, t5}],
 init_state = [{t1, [engine]}, {t2, [d]}, {t3, [b]}, {t4, [a, e]}, {t5, 1}],
 goal_state = [{t1, [engine, a, b, c, d, e]}] } .

problem3() ->
 #problem{yard = [{t1, t2}, {t1, t3}],
 init_state = [{t1, [engine]}, {t2, [a]}, {t3, [b]}],
 goal_state = [{t1, [engine, a, b]}, {t2, []}, {t3, []}] } .

problem4() ->
 #problem{yard = [{t1, t2}, {t1, t3}, {t1, t4}],
 init_state = [{t1, [engine]}, {t2, [a]}, {t3, [b, c]}, {t4, [d]}],
 goal_state = [{t1, [engine, a, b, c, d]}, {t2, []}, {t3, []}] } .

problem5() ->
 #problem{yard = [{t1, t2}, {t1, t3}, {t1, t4}],
 init_state = [{t1, [engine]}, {t2, [a]}, {t3, 1}, {t4, [d]}], %note c and b are out of order
 goal_state = [{t1, [engine, a, b, c, d]}, {t2, []}, {t3, []}] } .

The Record system is definitely a mixed bag. While it’s great to have some kind of naming structure for organizing data together, the syntax is clumsy, and particularly annoying to work with in the shell.

151> rr(trainswitch).
153> TestYard = trainswitch:problem1().
#problem{yard = [{t1,t2},
 init_state = [{t1,[engine]},{t2,[e]},{t4,[b,c,a]},{t6,[d]}],
 goal_state = [{t1,[engine,a,b,c,d,e]}]}
154> TestState = TestYard#problem.init_state.

So that’s the structure for a problem. The object is given a yard and a start state, find the moves that bring you to the goal state. Next up are a few more sub problems that work toward that goal.

Write a comment

You need to login to post comments!