storm_interface.py 6.08 KB
Newer Older
Thom Badings's avatar
Thom Badings committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import numpy as np

from tqdm import tqdm

import core.create_drone_prism_model as create_prism
import stormpy
import stormpy.core
import stormpy.logic
import stormpy.pars
import stormpy.examples
import stormpy.examples.files

def search(values, searchFor):
    '''
    Search for a value in a list of values
    '''
    
    for k in values:
        if searchFor in k:
            return k
    return None

def load_problem(model_file, property_file, bisimulation_type):
    '''
    Load a model in Storm, given a model and property file

    Parameters
    ----------
    model_file : String of the model file to load
    property_file : String of the property file to load
    bisimulation_type : Either 'strong', 'weak', or 'none'

    Returns
    -------
    parameters : Object with info about all parameters
    model : Storm model
    properties : Parsed property in Storm

    '''
    
    # Load and build model from file
    path = model_file
    prism_program = stormpy.parse_prism_program(path)
    print("\nBuilding model from {}".format(path))
    with open(property_file) as f:
        formula_str= " ".join([l.rstrip() for l in f])
    
    # Load properties
    properties = stormpy.parse_properties_for_prism_program(formula_str, 
                                                            prism_program)
    
    # Set model options
    options = stormpy.BuilderOptions([p.raw_formula for p in properties])
    options. set_add_out_of_bounds_state()
    model = stormpy.build_sparse_parametric_model_with_options(prism_program, 
                                                               options)

    print((model.model_type))
    print("Model supports parameters: {}".format(model.supports_parameters))
    
    parameters = model.collect_probability_parameters()
    numstate=model.nr_states
    
    print("Number of states before bisim:",numstate)
    print ("Number of params before bisim:",len(parameters))
    
    # Set bisimulation for model
    if bisimulation_type == 'strong':
        
        print(" -- Perform strong bisimulation")
        model = stormpy.perform_bisimulation(model, properties, 
                                             stormpy.BisimulationType.STRONG)
    
    elif bisimulation_type == 'weak':
        
        print(" -- Perform weak bisimulation")
        model = stormpy.perform_bisimulation(model, properties, 
                                             stormpy.BisimulationType.WEAK)
    
    elif bisimulation_type == 'none':
        
        print(" -- Skip bisimulation")
        pass
    
    else:
        
        raise RuntimeError(
          "Invalid bisimulation type (should be 'strong', 'weak', or 'none').")
    
    parameters= model.collect_probability_parameters()
    parameters_rew = model.collect_reward_parameters()
    parameters.update(parameters_rew)
    
    if bisimulation_type != 'none':
        numstate=model.nr_states
        print("Number of states after bisim:",numstate)
        print ("Number of params after bisim:",len(parameters))
    
    print('\nModel successfully loaded!\n')
    
    return parameters,model,properties


Thom Badings's avatar
Thom Badings committed
104
def sample_results(N, parameters, model, properties, model_file,
Thom Badings's avatar
Thom Badings committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
                   weather=None):
    '''
    Sample results for instantiated MDPs from Storm.

    Parameters
    ----------
    N : Number of parameter samples/solutions (integer)
    parameters : Parameters object
    model : Model object
    properties : Parsed properties in Storm
    model_file : String of the model file that is loaded
    weather : Weather conditions (optional; only used for drone benchmark)

    Returns
    -------
    solutions : List of solutions (one for each parameter sample)

    '''
    
    if 'drone' in model_file:
        drone = True
    else:
        drone = False
    
    # Evaluate model type
    if str(model.model_type)=='ModelType.DTMC':
        instantiator = stormpy.pars.PDtmcInstantiator(model)
    elif  str(model.model_type)=='ModelType.MDP':
        instantiator = stormpy.pars.PMdpInstantiator(model)
    else:
        raise RuntimeError("Invalid model type (should be a pDTMC or pMDP).")
    
    # Initialize results array
    solutions = np.zeros(N)
    
    # If we are performing the drone benchmark
    if drone:
        groups = create_prism.parameter_groups()
        
        # Instantiate parameters in the model
        create_prism.parameter_definitions(groups,False)
    
    # For every sample...
    for n in tqdm(range(N)):
            
        # Sample parameters according to the region defined by
        # "Parameter synthesis for Markov models: Faster than ever" paper
        point=dict()

        # Switch between drone and other benchmarks
        if drone:
            param_inst = create_prism.parameter_dirichlet_instantiations(
                groups, weather)
            param_samps = create_prism.parameter_dirichlet_samples(param_inst)
            
            for x in parameters:

            #instantiate parameters
                parameter_group = search(param_inst, str(x.name))

                element_int=0
                for element in parameter_group:
                    
                    param_sample_array = param_samps[tuple(parameter_group)]
                    if (str(element)) == (str(x.name)):

                        point[x] = param_sample_array[0,element_int]
                    element_int=element_int+1
        else:
            
            for x in parameters:
                if "coin" in model_file:
                    s = np.random.uniform(0.2, 0.8)
    
                else:
                    s= np.random.uniform(1e-5,1-1e-5)
    
                point[x] = stormpy.RationalRF(s)
        
        # Assign parameter values to model
        rational_parameter_assignments = dict(
            [[x, stormpy.RationalRF(val)] for x, val in point.items()])
        
        # Instantiate model
        inst_model = instantiator.instantiate(rational_parameter_assignments)
        
        # Obtain solution and store
        sol = stormpy.model_checking(inst_model, 
                             properties[0]).at(inst_model.initial_states[0])
        solutions[n] = float(sol)
        
    return solutions