# Copyright (c) 2009 Tim Freeman # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # (This is the standard MIT License, copied from # http://www.opensource.org/licenses/mit-license.php on 24 Apr 2007.) # Test video prediction in the case where the AI is taking actions, # and those actions are a given rather than something we have to # explain. # The AI's actions will be 0's or 1's in each time step. # The law of physics simply ignores the previous state, and the new # state is the new action. Since the input to the physical law is the # old state concatenated with the action, the AI's action happens to # be under the tape head when the law of physics starts executing. # The perceptual mechanism will be the identity function. from turing_tape import turing_tape, tape_tuple import video_prediction_act from video_prediction_act import Video_prediction_problem, next_frame, \ Video_prediction_explanation import bits from test_util import assert_equals, expect_exception from constants import left, right, end, done, truncate from compile import turing_program, turing_compile from speed_prior import Speed_prior import machine physics_as = turing_program("start", start={(end,0):(0, left, "trunc"), 1:(1, left, "trunc")}, trunc={(end, 0, 1): (truncate, right, "done")}, done=done) physics = Speed_prior(absyn=physics_as, logtime=2) t0 = turing_tape(length=1, bits=0) t1 = turing_tape(length=1, bits=1) assert_equals(physics.run(machine.empty_tape), t0) assert_equals(physics.run(tape_tuple(t0, t1)), t1) assert_equals(physics.run(tape_tuple(t1, t0)), t0) assert_equals(physics.run(tape_tuple(t1, t1)), t1) perception_as = turing_program("start",start=done) perception = Speed_prior(absyn=perception_as, logtime=1) assert_equals(perception.run(t0), t0) assert_equals(perception.run(t1), t1) explanation = video_prediction_act.Video_prediction_explanation( physics, perception, name="explanation") bad_physics = Speed_prior(program=0, logtime=0) bad_explanation = video_prediction_act.Video_prediction_explanation( bad_physics, perception, name="bad explanation") video = [t0,t0,t1] actions = [t0,t1] next_act = t1 Video_prediction_problem( video=video, actions=actions, next_act=next_act).matches(explanation) expect_exception(lambda: Video_prediction_problem( video=video, actions=actions, next_act=next_act).matches(bad_explanation)) def check_video(ringers): # ringers are the high complexity explanations we throw into the # sham physical law generator. all_explanations = [] for i in range(4): all_explanations.extend(Video_prediction_explanation() .generate_explanations(i)) all_explanations.extend(ringers) class Test_video_prediction_problem(Video_prediction_problem): # Sham physical law generator. def generate_explanations(self, n): result = [explanation for explanation in all_explanations if explanation.size() == n] return result problem = Test_video_prediction_problem(video = video, actions = actions, next_act = next_act) problem.matches(explanation) assert next_frame(video, actions, next_act, problem_class = Test_video_prediction_problem) == \ t1 check_video([explanation])