Browse Source

add ShortestPathsGenerator method binding (and fancy test fixture)

refactoring
Tom Janson 8 years ago
parent
commit
09b2bfcf67
  1. 5
      src/utility/shortestPaths.cpp
  2. 85
      tests/utility/test_shortestpaths.py

5
src/utility/shortestPaths.cpp

@ -59,7 +59,8 @@ void define_ksp(py::module& m) {
.def(py::init<Matrix const&, std::vector<double> const&, BitVector const&, MatrixFormat>(), "transition_matrix"_a, "target_prob_vector"_a, "initial_states"_a, "matrix_format"_a) .def(py::init<Matrix const&, std::vector<double> const&, BitVector const&, MatrixFormat>(), "transition_matrix"_a, "target_prob_vector"_a, "initial_states"_a, "matrix_format"_a)
.def(py::init<Matrix const&, StateProbMap const&, BitVector const&, MatrixFormat>(), "transition_matrix"_a, "target_prob_map"_a, "initial_states"_a, "matrix_format"_a) .def(py::init<Matrix const&, StateProbMap const&, BitVector const&, MatrixFormat>(), "transition_matrix"_a, "target_prob_map"_a, "initial_states"_a, "matrix_format"_a)
// ShortestPathsGenerator(Matrix const& transitionMatrix, std::vector<T> const& targetProbVector, BitVector const& initialStates, MatrixFormat matrixFormat);
// ShortestPathsGenerator(Matrix const& maybeTransitionMatrix, StateProbMap const& targetProbMap, BitVector const& initialStates, MatrixFormat matrixFormat);
.def("get_distance", &ShortestPathsGenerator::getDistance, "k"_a)
.def("get_states", &ShortestPathsGenerator::getStates, "k"_a)
.def("get_path_as_list", &ShortestPathsGenerator::getPathAsList, "k"_a)
; ;
} }

85
tests/utility/test_shortestpaths.py

@ -6,13 +6,67 @@ from stormpy.utility import MatrixFormat
from helpers.helper import get_example_path from helpers.helper import get_example_path
import pytest import pytest
import math
# this is admittedly slightly overengineered
class ModelWithKnownShortestPaths:
"""Knuth's die model with reference kSP methods"""
def __init__(self):
self.target_label = "one"
program_path = get_example_path("dtmc", "die.pm")
raw_formula = "P=? [ F \"" + self.target_label + "\" ]"
program = stormpy.parse_prism_program(program_path)
formulas = stormpy.parse_formulas_for_prism_program(raw_formula, program)
self.model = stormpy.build_model(program, formulas[0])
def probability(self, k):
return (1/2)**((2 * k) + 1)
def state_set(self, k):
return BitVector(self.model.nr_states, [0, 1, 3, 7])
def path(self, k):
path = [0] + k * [1, 3] + [7]
return list(reversed(path)) # SPG returns traversal from back
@pytest.fixture(scope="module", params=[1, 2, 3, 3000, 42])
def index(request):
return request.param
@pytest.fixture(scope="module")
def model_with_known_shortest_paths():
return ModelWithKnownShortestPaths()
@pytest.fixture(scope="module")
def model(model_with_known_shortest_paths):
return model_with_known_shortest_paths.model
@pytest.fixture(scope="module")
def expected_distance(model_with_known_shortest_paths):
return model_with_known_shortest_paths.probability
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def model(program_path=get_example_path("dtmc", "die.pm"), raw_formula="P=? [ F \"one\" ]"):
program = stormpy.parse_prism_program(program_path)
formulas = stormpy.parse_formulas_for_prism_program(raw_formula, program)
return stormpy.build_model(program, formulas[0])
def expected_state_set(model_with_known_shortest_paths):
return model_with_known_shortest_paths.state_set
@pytest.fixture(scope="module")
def expected_path(model_with_known_shortest_paths):
return model_with_known_shortest_paths.path
@pytest.fixture(scope="module")
def target_label(model_with_known_shortest_paths):
return model_with_known_shortest_paths.target_label
@pytest.fixture @pytest.fixture
@ -34,13 +88,6 @@ def state_bitvector(model, state_list):
return BitVector(length=model.nr_states, set_entries=state_list) return BitVector(length=model.nr_states, set_entries=state_list)
@pytest.fixture
def label(model):
some_label = "one"
assert some_label in model.labels, "test model does not contain label '" + some_label + "'"
return some_label
@pytest.fixture @pytest.fixture
def transition_matrix(model): def transition_matrix(model):
return model.transition_matrix return model.transition_matrix
@ -76,8 +123,8 @@ class TestShortestPaths:
def test_spg_ctor_state_list_target(self, model, state_list): def test_spg_ctor_state_list_target(self, model, state_list):
_ = ShortestPathsGenerator(model, state_list) _ = ShortestPathsGenerator(model, state_list)
def test_spg_ctor_label_target(self, model, label):
_ = ShortestPathsGenerator(model, label)
def test_spg_ctor_label_target(self, model, target_label):
_ = ShortestPathsGenerator(model, target_label)
def test_spg_ctor_matrix_vector(self, transition_matrix, target_prob_list, initial_states, matrix_format): def test_spg_ctor_matrix_vector(self, transition_matrix, target_prob_list, initial_states, matrix_format):
_ = ShortestPathsGenerator(transition_matrix, target_prob_list, initial_states, matrix_format) _ = ShortestPathsGenerator(transition_matrix, target_prob_list, initial_states, matrix_format)
@ -85,4 +132,14 @@ class TestShortestPaths:
def test_spg_ctor_matrix_map(self, transition_matrix, target_prob_map, initial_states, matrix_format): def test_spg_ctor_matrix_map(self, transition_matrix, target_prob_map, initial_states, matrix_format):
_ = ShortestPathsGenerator(transition_matrix, target_prob_map, initial_states, matrix_format) _ = ShortestPathsGenerator(transition_matrix, target_prob_map, initial_states, matrix_format)
# TODO: add tests that check actual functionality
def test_spg_distance(self, model, target_label, index, expected_distance):
spg = ShortestPathsGenerator(model, target_label)
assert math.isclose(spg.get_distance(index), expected_distance(index))
def test_spg_state_set(self, model, target_label, index, expected_state_set):
spg = ShortestPathsGenerator(model, target_label)
assert spg.get_states( sp/tempest - resources/3rdparty/eigen/bench/btl/CMakeLists.txt at 13a2bd30570e5b3d8981ff33c0017cb200db4bb0 - tempest - Gitea: Git with a cup of tea
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
2.8 KiB

PROJECT(BTL)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.2)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${Eigen_SOURCE_DIR}/cmake)
include(MacroOptionalAddSubdirectory)
OPTION(BTL_NOVEC "Disable SSE/Altivec optimizations when possible" OFF)
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
string(REGEX MATCH icpc IS_ICPC ${CMAKE_CXX_COMPILER})
IF(CMAKE_COMPILER_IS_GNUCXX OR IS_ICPC)
SET(CMAKE_CXX_FLAGS "-g0 -O3 -DNDEBUG")
SET(CMAKE_Fortran_FLAGS "-g0 -O3 -DNDEBUG")
IF(NOT BTL_NOVEC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2")
SET(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -msse2")
ELSE(NOT BTL_NOVEC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_DONT_VECTORIZE")
ENDIF(NOT BTL_NOVEC)
ENDIF(CMAKE_COMPILER_IS_GNUCXX OR IS_ICPC)
IF(MSVC)
SET(CMAKE_CXX_FLAGS " /O2 /Ot /GL /fp:fast -DNDEBUG")
# SET(CMAKE_Fortran_FLAGS "-g0 -O3 -DNDEBUG")
IF(NOT BTL_NOVEC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:SSE2")
ELSE(NOT BTL_NOVEC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_DONT_VECTORIZE")
ENDIF(NOT BTL_NOVEC)
ENDIF(MSVC)
if(IS_ICPC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fast")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fast")
endif(IS_ICPC)
include_directories(
${PROJECT_SOURCE_DIR}/actions
${PROJECT_SOURCE_DIR}/generic_bench
${PROJECT_SOURCE_DIR}/generic_bench/utils
${PROJECT_SOURCE_DIR}/libs/STL)
# find_package(MKL)
# if (MKL_FOUND)
# add_definitions(-DHAVE_MKL)
# set(DEFAULT_LIBRARIES ${MKL_LIBRARIES})
# endif (MKL_FOUND)
MACRO(BTL_ADD_BENCH targetname)
foreach(_current_var ${ARGN})
set(_last_var ${_current_var})
endforeach(_current_var)
set(_sources ${ARGN})
list(LENGTH _sources _argn_length)
list(REMOVE_ITEM _sources ON OFF TRUE FALSE)
list(LENGTH _sources _src_length)
if (${_argn_length} EQUAL ${_src_length})
set(_last_var ON)
endif (${_argn_length} EQUAL ${_src_length})
OPTION(BUILD_${targetname} "Build benchmark ${targetname}" ${_last_var})
IF(BUILD_${targetname})
ADD_EXECUTABLE(${targetname} ${_sources})
ADD_TEST(${targetname} "${targetname}")
target_link_libraries(${targetname} ${DEFAULT_LIBRARIES} rt)
ENDIF(BUILD_${targetname})
ENDMACRO(BTL_ADD_BENCH)
macro(btl_add_target_property target prop value)
if(BUILD_${target})
get_target_property(previous ${target} ${prop})
if(NOT previous)
set(previous "")
endif()
set_target_properties(${target} PROPERTIES ${prop} "${previous} ${value}")
endif()
endmacro(btl_add_target_property)
ENABLE_TESTING()
add_subdirectory(libs/eigen3)
add_subdirectory(libs/eigen2)
add_subdirectory(libs/BLAS)
add_subdirectory(libs/ublas)
add_subdirectory(libs/gmm)
add_subdirectory(libs/mtl4)
add_subdirectory(libs/blitz)
add_subdirectory(libs/tvmet)
add_subdirectory(libs/STL)
add_subdirectory(data)
0