OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / SudokuUTest.cxxtest

260 lines · cxxtest ref:
/*
 * tests/query/SudokuUTest.cxxtest
 *
 * Copyright (C) 2009, 2013, 2014 Linas Vepstas <linasvepstas@gmail.com>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <opencog/guile/SchemeEval.h>
#include <opencog/util/Logger.h>
#include "imply.h"

#include "test-types.h"

using namespace opencog;

class SudokuPuzzle :  public CxxTest::TestSuite
{
	private:
		AtomSpacePtr as;
		SchemeEval* eval;

	public:

		SudokuPuzzle(void)
		{
			logger().set_level(Logger::DEBUG);
			logger().set_print_to_stdout_flag(true);

			as = createAtomSpace();
			eval = SchemeEval::get_scheme_evaluator(as);
			eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");

		}

		~SudokuPuzzle()
		{
			// Erase the log file if no assertions failed.
			if (!CxxTest::TestTracker::tracker().suiteFailed())
				std::remove(logger().get_filename().c_str());
		}

		void setUp(void);

		void tearDown(void) { }

		void test_2x2_puzzle(void);
		void test_2x2_any(void);
		void test_3x3_puzzle(void);
		void test_9x9_puzzle(void);
		void test_9x9_inkala(void);
		void xtest_9x9_miracle(void);
};

/*
 * Clear the atomspace, load various atom types.
 */
#define an as->add_node
#define al as->add_link
void SudokuPuzzle::setUp(void)
{
	as->clear();
}

#define getlink(hand,pos) hand->getOutgoingAtom(pos)
#define getarity(hand) hand->get_arity()

/* ========================================================================
 * Extremely simple 2x2 puzzle solver
 */
void SudokuPuzzle::test_2x2_puzzle(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/sudoku-simple.scm\")");

	// Create an implication link that will be tested.
	Handle puzzle = eval->eval_h("(x2-puzzle)");

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != puzzle);
	logger().debug("puzzle is:\n%s\n", oc_to_string(puzzle).c_str());

	// Solve the puzzle
	// Handle puzzle_solution = bindlink(as, puzzle);
	Handle puzzle_solution = eval->eval_h("(cog-execute! (x2-puzzle))");
	logger().debug("puzzle solution is %s\n", oc_to_string(puzzle_solution).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(puzzle_solution));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/* ========================================================================
 * Extremely simple 2x2 puzzle solver
 */
void SudokuPuzzle::test_2x2_any(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/sudoku-simple.scm\")");

	// Create an implication link that will be tested.
	Handle puzzle = eval->eval_h("(x2-any)");

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != puzzle);
	logger().debug("puzzle is:\n%s\n", oc_to_string(puzzle).c_str());

	// Solve the puzzle. Expect 2 solutions, as there are no
	// fixed cells here.
	Handle puzzle_solution = HandleCast(puzzle->execute(as.get()));
	logger().debug("puzzle solution is %s\n", oc_to_string(puzzle_solution).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 2, getarity(puzzle_solution));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/* ========================================================================
 * Extremely simple 3x3 puzzle solver.
 */
void SudokuPuzzle::test_3x3_puzzle(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/sudoku-simple.scm\")");

	// Create an implication link that will be tested.
	Handle puzzle = eval->eval_h("(x3-puzzle)");

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != puzzle);
	logger().debug("puzzle is:\n%s\n", oc_to_string(puzzle).c_str());

	// Solve the puzzle
	Handle puzzle_solution = eval->eval_h("(cog-execute! (x3-puzzle))");
	logger().debug("puzzle solution is %s\n", oc_to_string(puzzle_solution).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 4, getarity(puzzle_solution));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/* ========================================================================
 * Full 9x9 Puzzle solver. This exercises the constraint-propagation
 * code in the query engine.
 */
void SudokuPuzzle::test_9x9_puzzle(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/sudoku-rules.scm\")");
	eval->eval("(load-from-path \"tests/query/sudoku-9x9-2944-hard.scm\")");

	// Create an implication link that will be tested.
	Handle puzzle = eval->eval_h("(puzzle-9x9-2944-hard)");

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != puzzle);
	logger().debug("puzzle is:\n%s\n", oc_to_string(puzzle).c_str());
	logger().flush();

	// Solve the puzzle
	Handle puzzle_solution = HandleCast(puzzle->execute(as.get()));
	logger().debug("puzzle solution is %s\n", oc_to_string(puzzle_solution).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(puzzle_solution));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/* ========================================================================
 * Full 9x9 Puzzle solver. The Inkala puzzle, claimed to be one of the
 * hardest Sudoku puzzles ever created. This exercises the constraint
 * propagation code in the query engine.
 */
void SudokuPuzzle::test_9x9_inkala(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/sudoku-rules.scm\")");
	eval->eval("(load-from-path \"tests/query/sudoku-9x9-inkala.scm\")");

	// Load some additional gorp just to make things confusing.
	eval->eval("(load-from-path \"tests/query/sudoku-9x9-2944-hard.scm\")");
	eval->eval("(load-from-path \"tests/query/sudoku-simple.scm\")");

	// Load junk from completely unrelated unit tests.
	// Basically, jam random junk into atomspace to confuse the system
	eval->eval("(load-from-path \"tests/query/unordered-exhaust.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-jswiergo.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-more.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-below.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-couplayer.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-couple.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-distinct.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-equal.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-equpr.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-indistinct.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odo-layer.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-odometer.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered-quote.scm\")");
	eval->eval("(load-from-path \"tests/query/unordered.scm\")");

	// Create an implication link that will be tested.
	Handle puzzle = eval->eval_h("(puzzle-9x9-inkala)");

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != puzzle);
	logger().debug("puzzle is:\n%s\n", oc_to_string(puzzle).c_str());
	logger().flush();

	// Solve the puzzle
	Handle puzzle_solution = HandleCast(puzzle->execute(as.get()));
	logger().debug("puzzle solution is %s\n", oc_to_string(puzzle_solution).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(puzzle_solution));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/* ========================================================================
 * Miracle Sudoku - only 2 given digits, but with anti-knight, anti-king,
 * and non-consecutive constraints. This currently fails hitting a bug
 * in the constraint propagation engine thoroughly. Disabled for now.
 */
void SudokuPuzzle::xtest_9x9_miracle(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/sudoku-rules.scm\")");
	eval->eval("(load-from-path \"tests/query/sudoku-miracle-rules.scm\")");
	eval->eval("(load-from-path \"tests/query/sudoku-9x9-miracle.scm\")");

	// Create an implication link that will be tested.
	Handle puzzle = eval->eval_h("(puzzle-9x9-miracle)");

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != puzzle);
	logger().debug("puzzle is:\n%s\n", oc_to_string(puzzle).c_str());
	logger().flush();

	// Solve the puzzle - expect exactly 1 solution
	Handle puzzle_solution = HandleCast(puzzle->execute(as.get()));
	logger().debug("puzzle solution is %s\n", oc_to_string(puzzle_solution).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(puzzle_solution));

	logger().debug("END TEST: %s", __FUNCTION__);
}