Beispiel #1
0
    def test_newobj_no_parameters_initializes_int_field_to_zero(self):
        from VM import VM

        vm = VM()

        m = MethodDefinition()
        m.name = 'ctor'
        m.namespace = 'testnamespace.testclass'
        vm.methods.append(m)

        c = ClassDefinition()
        c.name = 'testclass'
        c.namespace = 'testnamespace'
        c.methods.append(m)

        v = Variable()
        v.name = 'xyz'
        v.type = Types.Int32

        c.fieldDefinitions.append(v)

        t = Types.register_custom_type(c)

        n = newobj('instance void testnamespace.testclass::.ctor()')
        n.execute(vm)
        Types.unregister_custom_type(t)

        o = vm.stack.pop()
        self.assertEqual(o.type, t)
        self.assertEqual(len(o.fields), 1)
        self.assertEqual(o.fields[0].value, 0)

        self.assertEqual(len(o.fieldNames), 1)
        self.assertEqual(o.fieldNames[0], 'xyz')
Beispiel #2
0
    def test_callvirt_one_parameter_instance_puts_this_pointer_and_parameter_on_stack(
            self):
        from VM import VM
        from MethodDefinition import MethodDefinition
        vm = VM()

        paramObject = Variable()
        paramObject.type = Types.Int32

        m = MethodDefinition()
        m.name = 'TestMethod'
        m.namespace = 'A.B'
        m.returnType = Types.Int32
        m.parameters = [paramObject]
        m.names = 'A.B'
        m.attributes.append(MethodDefinition.AttributeTypes['instance'])
        vm.methods.append(m)

        r = ReferenceType()
        vm.stack.push(r)
        v = Variable(8888)
        vm.stack.push(v)
        self.assertEqual(vm.currentMethod, None)

        c = callvirt('instance int32 A.B::TestMethod ( int32 )')
        c.execute(vm)

        self.assertEqual(vm.currentMethod.methodDefinition, m)
        self.assertEqual(vm.stack.get_number_of_frames(), 2)
        self.assertEqual(len(vm.current_method().parameters), 2)
        self.assertEqual(vm.current_method().parameters[1], v)
        self.assertEqual(vm.current_method().parameters[0], r)
Beispiel #3
0
    def __init__(self, data):
        # name
        self.instance_name = data['name']
        # locations
        self.locations = { loc['id'] : Location( loc ) for loc in data['locations']}
        # outgoing transitions from each location
        for t in data['transitions']:
            tid = t['sid']
            transition = Transition(t)
            self.locations[tid].add_exit_transition(transition)

        # current location setup
        self.loc_id = data['initialLocation']['id'] # initial location id
        self.current_location = self.locations[self.loc_id]

        # variable objects
        self.I = { i['name'] : Variable(i) for i in data['I']}
        self.O = { o['name'] : Variable(o) for o in data['O']}
        self.X = { x['name'] : Variable(x) for x in data['X_C']}

        # Simulink has multiple options for initialization
        # 1. reset on the first transition
        for fx in data['initialization']:
            if fx['LHS'] in self.X:
                self.X[fx['LHS']].set_current_value(fx['RHS'])
            elif fx['LHS'] in self.O:
                self.O[fx['LHS']].set_current_value(fx['RHS'])
        # 2. initial location entry actions
        for en in data['initialLocation']['entries']:
            if en['LHS'] in self.X:
                self.X[en['LHS']].set_current_value(fx['RHS'])
            elif en['LHS'] in self.O:
                self.O[en['LHS']].set_current_value(fx['RHS'])

        self.update_O(index = 0)
Beispiel #4
0
    def __init__(self, method):
        self.name = 'call ' + method
        parts = method.split()
        if parts[0] == 'instance':
            self.instance = True
            parts.pop(0)
        else:
            self.instance = False
            
            
        self.method_type = Types.BuiltInTypes[parts[0]]
        self.method_namespace, self.method_name = parts[1].split('::')
        self.method_parameters = []
        if self.method_name[0] == '.':
            self.method_name = self.method_name[1:]
        if len(parts) > 2 and parts[2] == '(':
            index = 3
            while index < len(parts) and parts[index] != ')':
                v = Variable()
                v.type = Types.resolve_type(parts[index])
                self.method_parameters.append(v)
                index += 1
#        if not self.method_name.find('()') != -1: # if we have parameters...
#            parts = self.method_name.split('(')
#            self.method_name = parts[0]
#            if len(parts) > 1:
#                parameters = parts[1][:-1]
#                self.method_parameters.append(Types.resolve_type(parameters))
        #self.method_name = method_name
        #self.method_type = method_type
        self.opcode = 0x28
        self.value = None
Beispiel #5
0
    def execute(self, vm):
        t = Types.resolve_type(self.typeName)
        r = ReferenceType()
        r.type = t

        for f in t.classRef.fieldDefinitions:
            o = Variable()
            o.type = f.type
            o.name = f.name
            o.value = 0  # fixme - reference types?
            r.fields.append(o)
            r.fieldNames.append(f.name)

        vm.stack.push(r)

        namespace = t.namespace + '.' + t.name
        name = 'ctor'

        methodDefinition = vm.find_method_by_signature(
            namespace, name, None, None)  # fixme - should name have a . in it?
        if methodDefinition is None:
            raise Exception("Couldn't find " + name + " method for " +
                            namespace)

        m = methodDefinition.get_method()
        #parameter = Variable()
        #parameter.value = r
        m.parameters = [
            r
        ]  # fixme - create a new method object so we don't overwrite the parameters?
        #fixme - should we even use parameters? or just the stack?
        vm.execute_method(m)
    def createZSVariables(self):
        numVars = 0
        # the z variable is for each scenario
        for scenario in self.scenarios:

            # create zsp variable
            v1 = Variable()
            v1.type = Variable.v_zsp
            v1.name = "zsp_" + scenario.id
            v1.col = self.numCols
            v1.scenario = scenario.id
            self.variables[v1.name] = v1
            self.lp.variables.add(names=[v1.name])
            self.numCols += 1
            numVars += 1

            #create zsn variable
            v2 = Variable()
            v2.type = Variable.v_zsn
            v2.name = "zsn_" + scenario.id
            v2.col = self.numCols
            v2.scenario = scenario.id
            self.variables[v2.name] = v2
            self.lp.variables.add(names=[v2.name])
            self.numCols += 1
            numVars += 1

        return numVars
Beispiel #7
0
    def modelPlots(self,m):

        # some of the plots can be generated using the standard
        # routine, with some modifications
        super(ConfTWSA,self).modelPlots(m)
        for page in self.layout.pages:
            for sec in page.figures.keys():
                for fig in page.figures[sec]:
                    fig.side = fig.side.replace("MEAN","ANOMALY MAGNITUDE")

        # 
        bname = os.path.join(self.output_path,"%s_Benchmark.nc" % (self.name       ))
        fname = os.path.join(self.output_path,"%s_%s.nc"        % (self.name,m.name))
        
        # get the HTML page
        page = [page for page in self.layout.pages if "MeanState" in page.name][0]  

        if not os.path.isfile(bname): return
        if not os.path.isfile(fname): return
        obs = Variable(filename = bname, variable_name = "twsa", groupname = "MeanState")
        mod = Variable(filename = fname, variable_name = "twsa", groupname = "MeanState")
        for i,basin in enumerate(self.basins):

            page.addFigure("Spatially integrated regional mean",
                           basin,
                           "MNAME_global_%s.png" % basin,
                           basin,False,longname=basin)
            
            fig,ax = plt.subplots(figsize=(6.8,2.8),tight_layout=True)
            ax.plot(obs.time/365+1850,obs.data[:,i],lw=2,color='k',alpha=0.5)
            ax.plot(mod.time/365+1850,mod.data[:,i],lw=2,color=m.color      )
            ax.grid()
            ax.set_ylabel(post.UnitStringToMatplotlib(obs.unit))
            fig.savefig(os.path.join(self.output_path,"%s_global_%s.png" % (m.name,basin)))
            plt.close()
Beispiel #8
0
    def __init__(self, kernel_shape=list, input_variable=Variable, name=str, stride=1, padding='SAME'):
        # kernel_shape = [ksize, ksize, input_channels, output_channels]
        for i in kernel_shape:
            if not isinstance(i, int):
                raise Exception("Operator Conv2D name: %s kernel shape is not list of int" % self.name)

        if not isinstance(input_variable, Variable):
            raise Exception("Operator Conv2D name: %s's input_variable is not instance of Variable" % name)

        if len(input_variable.shape)!=4:
            raise Exception("Operator Conv2D name: %s's input_variable's shape != 4d Variable!" % name)

        self.ksize = kernel_shape[0]
        self.stride = stride
        self.output_num = kernel_shape[-1]
        self.padding = padding
        self.col_image = []

        self.weights = Variable(kernel_shape, scope=name, name='weights',learnable=True)
        self.bias = Variable([self.output_num], scope=name, name='bias', learnable=True)
        self.batch_size = input_variable.shape[0]

        if self.padding == 'SAME':
            _output_shape = [self.batch_size, input_variable.shape[1] / stride, input_variable.shape[2] / stride,
                             self.output_num]
        if self.padding == 'VALID':
            _output_shape = [self.batch_size, (input_variable.shape[1] - self.ksize + 1) / stride,
                             (input_variable.shape[2] - self.ksize + 1) / stride, self.output_num]

        self.output_variables = Variable(_output_shape, name='out', scope=name)  # .name
        self.input_variables = input_variable
        Operator.__init__(self, name, self.input_variables, self.output_variables)
Beispiel #9
0
    def stageData(self, m):

        obs = Variable(filename=self.source, variable_name="permafrost_extent")

        # These parameters may be changed from the configure file
        y0 = float(self.keywords.get(
            "y0", 1970.))  # [yr]      beginning year to include in analysis
        yf = float(self.keywords.get(
            "yf", 2000.))  # [yr]      end year to include in analysis
        dmax = float(
            self.keywords.get("dmax", 3.5)
        )  # [m]       consider layers where depth in is the range [0,dmax]
        Teps = float(
            self.keywords.get("Teps", 273.15)
        )  # [K]       temperature below which we assume permafrost occurs

        t0 = (y0 - 1850.) * 365.
        tf = (yf + 1 - 1850.) * 365.
        mod = m.extractTimeSeries(self.variable,
                                  initial_time=t0,
                                  final_time=tf)
        mod.trim(t=[t0, tf],
                 lat=[max(obs.lat.min(), mod.lat.min()), 90],
                 d=[0, dmax])  #.annualCycle()

        alt = _ALTFromTSL(mod)
        mod = Variable(name="permafrost_extent",
                       unit="1",
                       data=np.ma.masked_array((alt >= 0).astype(float),
                                               mask=alt.mask),
                       lat=mod.lat,
                       lon=mod.lon,
                       area=mod.area)
        return obs, mod
Beispiel #10
0
    def __init__(self, dictionary):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        self.private_format = 'gchar *'
        self.public_format = self.private_format

        if 'fixed-size' in dictionary:
            self.is_fixed_size = True
            # Fixed-size strings
            self.needs_dispose = False
            self.length_prefix_size = 0
            self.fixed_size = dictionary['fixed-size']
            self.max_size = ''
        else:
            self.is_fixed_size = False
            # Variable-length strings in heap
            self.needs_dispose = True
            if 'size-prefix-format' in dictionary:
                if dictionary['size-prefix-format'] == 'guint8':
                    self.length_prefix_size = 8
                elif dictionary['size-prefix-format'] == 'guint16':
                    self.length_prefix_size = 16
                else:
                    raise ValueError('Invalid size prefix format (%s): not guint8 or guint16' % dictionary['size-prefix-format'])
            # Strings which are given as the full value of a TLV and which don't have
            # a explicit 'size-prefix-format' will NOT have a length prefix
            elif 'type' in dictionary and dictionary['type'] == 'TLV':
                self.length_prefix_size = 0
            else:
                # Default to UINT8
                self.length_prefix_size = 8
            self.fixed_size = ''
            self.max_size = dictionary['max-size'] if 'max-size' in dictionary else ''
Beispiel #11
0
    def __init__(self, dictionary, struct_type_name, container_type):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        # The public format of the struct is built directly from the suggested
        # struct type name
        self.public_format = utils.build_camelcase_name(struct_type_name)
        self.private_format = self.public_format
        self.container_type = container_type

        # Load members of this struct
        self.members = []
        for member_dictionary in dictionary['contents']:
            member = {}
            member['name'] = utils.build_underscore_name(member_dictionary['name'])
            member['object'] = VariableFactory.create_variable(member_dictionary, struct_type_name + ' ' + member['name'], self.container_type)
            # Specify that the variable will be defined in the public header
            member['object'].flag_public()
            self.members.append(member)

        # We'll need to dispose if at least one of the members needs it
        for member in self.members:
            if member['object'].needs_dispose == True:
                self.needs_dispose = True
Beispiel #12
0
    def __init__(self, dictionary, struct_type_name, container_type):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        # The public format of the struct is built directly from the suggested
        # struct type name
        self.public_format = utils.build_camelcase_name(struct_type_name)
        self.private_format = self.public_format
        self.container_type = container_type

        # Load members of this struct
        self.members = []
        for member_dictionary in dictionary["contents"]:
            member = {}
            member["name"] = utils.build_underscore_name(member_dictionary["name"])
            member["object"] = VariableFactory.create_variable(
                member_dictionary, struct_type_name + " " + member["name"], self.container_type
            )
            self.members.append(member)

        # We'll need to dispose if at least one of the members needs it
        for member in self.members:
            if member["object"].needs_dispose == True:
                self.needs_dispose = True
Beispiel #13
0
    def test_execute_int_parameter(self):
        from VM import VM
        vm = VM()
        
        c = ClassDefinition()
        c.namespace = 'a'
        c.name = 'b'
        v = Variable()
        v.name = 'xyz'
        v.type = Types.Int32
        
        r = ReferenceType()
        t = Types.register_custom_type(c)
        r.type = t

        r.add_field(v)        

        vm.stack.push(r)
        vm.stack.push(Variable(9876))
        
        x = stfld('int32 a.b::xyz')
        
        x.execute(vm)
        self.assertEqual(vm.stack.count(), 0)
        self.assertEqual(r.fields[0].value, 9876)
Beispiel #14
0
    def __init__(self, dictionary, array_element_type, container_type):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        self.private_format = 'GArray *'
        self.public_format = self.private_format
        self.fixed_size = 0
        self.name = dictionary['name']

        # The array and its contents need to get disposed
        self.needs_dispose = True

        # We need to know whether the variable comes in an Input container or in
        # an Output container, as we should not dump the element clear() helper method
        # if the variable is from an Input container.
        self.container_type = container_type

        # Load variable type of this array
        if 'name' in dictionary['array-element']:
            self.array_element = VariableFactory.create_variable(
                dictionary['array-element'],
                array_element_type + ' ' + dictionary['array-element']['name'],
                self.container_type)
        else:
            self.array_element = VariableFactory.create_variable(
                dictionary['array-element'], '', self.container_type)

        # Load variable type for the array size prefix
        if 'size-prefix-format' in dictionary:
            # We do NOT allow 64-bit types as array sizes (GArray won't support them)
            if dictionary['size-prefix-format'] not in [
                    'guint8', 'guint16', 'guint32'
            ]:
                raise ValueError(
                    'Invalid size prefix format (%s): not guint8 or guint16 or guint32'
                    % dictionary['size-prefix-format'])
            default_array_size = {'format': dictionary['size-prefix-format']}
            self.array_size_element = VariableFactory.create_variable(
                default_array_size, '', self.container_type)
        elif 'fixed-size' in dictionary:
            # fixed-size arrays have no size element, obviously
            self.fixed_size = dictionary['fixed-size']
            if int(self.fixed_size) == 0 or int(self.fixed_size) > 512:
                raise ValueError(
                    'Fixed array size %s out of bounds (not between 0 and 512)'
                    % self.fixed_size)
        else:
            # Default to 'guint8' if no explicit array size given
            default_array_size = {'format': 'guint8'}
            self.array_size_element = VariableFactory.create_variable(
                default_array_size, '', self.container_type)

        # Load variable type for the sequence prefix
        if 'sequence-prefix-format' in dictionary:
            sequence = {'format': dictionary['sequence-prefix-format']}
            self.array_sequence_element = VariableFactory.create_variable(
                sequence, '', self.container_type)
        else:
            self.array_sequence_element = ''
Beispiel #15
0
    def test_callvirt_one_parameter_instance_puts_this_pointer_and_parameter_on_stack(self):
        from VM import VM
        from MethodDefinition import MethodDefinition

        vm = VM()

        paramObject = Variable()
        paramObject.type = Types.Int32

        m = MethodDefinition()
        m.name = "TestMethod"
        m.namespace = "A.B"
        m.returnType = Types.Int32
        m.parameters = [paramObject]
        m.names = "A.B"
        m.attributes.append(MethodDefinition.AttributeTypes["instance"])
        vm.methods.append(m)

        r = ReferenceType()
        vm.stack.push(r)
        v = Variable(8888)
        vm.stack.push(v)
        self.assertEqual(vm.currentMethod, None)

        c = callvirt("instance int32 A.B::TestMethod ( int32 )")
        c.execute(vm)

        self.assertEqual(vm.currentMethod.methodDefinition, m)
        self.assertEqual(vm.stack.get_number_of_frames(), 2)
        self.assertEqual(len(vm.current_method().parameters), 2)
        self.assertEqual(vm.current_method().parameters[1], v)
        self.assertEqual(vm.current_method().parameters[0], r)
Beispiel #16
0
    def test_callvirt_one_parameter_int(self):
        from VM import VM
        from MethodDefinition import MethodDefinition

        vm = VM()

        paramObject = Variable()
        paramObject.type = Types.Int32
        m = MethodDefinition()
        m.namespace = "A.B"
        m.name = "TestMethod"
        m.returnType = Types.Int32
        m.parameters = [paramObject]
        vm.methods.append(m)

        param = Variable()
        param.value = 123
        param.type = Types.Int32
        vm.stack.push(param)

        c = callvirt("int32 A.B::TestMethod ( int32 )")
        c.execute(vm)

        self.assertEqual(vm.currentMethod.methodDefinition, m)
        self.assertEqual(vm.stack.get_number_of_frames(), 2)
        self.assertEqual(vm.current_method().parameters[0], param)
Beispiel #17
0
    def createMeteorologicalVariable(self, product, variable_name):
        """
        Returns "meteorological" type `Variable` class objects, for given product variable

        :type product: *snappy.Product*
        :param product: In memory representation of data product

        :type variable_name: str
        :param product: Specified product variable name

        :return:
            :meteorological_variable: *eopy.product.productIO.Variable.Variable*

            ``Variable`` object for given product 'meteorological' variable
        """

        spectral_variable = False
        tiepointgrid = False
        mask = False

        if variable_name in list(product.getBandNames()):
            if product.getBand(variable_name).getSpectralWavelength() > 0.0:
                spectral_variable = True
        elif variable_name not in list(product.getMaskGroup().getNodeNames()):
            tiepointgrid = True
        else:
            mask = True

        if spectral_variable:
            meteorological_variable_dict = {'name': variable_name,
                                            'dtype': 'float',
                                            'vtype': 'meterological',
                                            'units': product.getBand(variable_name).getUnit(),
                                            'ndims': 2,
                                            'shape': (int(product.getBand(variable_name).getRasterWidth()),
                                                      int(product.getBand(variable_name).getRasterHeight())),
                                            'wavelength': product.getBand(variable_name).getSpectralWavelength(),
                                            'bandwidth': product.getBand(variable_name).getSpectralBandwidth(),
                                            'srf': None}
            meteorological_variable = SpectralVariable(meteorological_variable_dict)
        elif not tiepointgrid:
            meteorological_variable_dict = {'name': variable_name,
                                            'dtype': 'float',
                                            'vtype': 'meteorological',
                                            'units': product.getBand(variable_name).getUnit(),
                                            'ndims': 2,
                                            'shape': (int(product.getBand(variable_name).getRasterWidth()),
                                                      int(product.getBand(variable_name).getRasterHeight()))}
            meteorological_variable = Variable(meteorological_variable_dict)
        else:
            meteorological_variable_dict = {'name': variable_name,
                                            'dtype': 'float',
                                            'vtype': 'meteorological',
                                            'units': product.getTiePointGrid(variable_name).getUnit(),
                                            'ndims': 2,
                                            'shape': (int(product.getSceneRasterWidth()), int(product.getSceneRasterHeight()))}
            meteorological_variable = Variable(meteorological_variable_dict)

        return meteorological_variable
Beispiel #18
0
def numerical_diff(f, x, eps=1e-4):
    x0 = Variable(x.data - eps)
    x1 = Variable(x.data + eps)

    y0 = f(x0)
    y1 = f(x1)

    return (y1.data - y0.data) / (2 * eps)
Beispiel #19
0
 def __init__(self, name, type, variables, virtual_direction=None, size=0):
     """
     Name: Group
     Description: Used to hold a group with several literals with information.
     """
     self.variables = variables
     self.size = size
     Variable.__init__(self, name, type, virtual_direction, None)
Beispiel #20
0
class Collection(AbcModel):
    attributeInfos = list(AbcModel.attributeInfos)
    attributeInfos.extend([("X", (Variable, Variable, Variable(), "var X")),
                           ("Y", (Variable, Variable, Variable(), "var Y")),
                           ("color", (str, COLOR, "blue", "artist color")),
                           ("linestyle", (str, STRING, "-", "linestyle")),
                           ("animation", (bool, BOOL, False,
                                          "enable artist animation"))])
Beispiel #21
0
 def __call__(self, input):
     self.input = input
     x = input.data
     y = self.forward(x)
     output = Variable(as_ndarray(y))
     output.set_creator(self)
     self.output = output
     return output
Beispiel #22
0
    def add_function_parameter(self, var: Variable) -> bool:
        if self.function.top() == "global":
            var.direction = avail.get_next_global(var.type, False, var.name)
        else:
            var.direction = avail.get_next_local(var.type, False, var.name)

        if self.function_table.add_parameter(self.function.top(), var):
            return True
        return False
Beispiel #23
0
    def test___repr__(self):
        var = Variable()
        var.name = "test_name"

        expected_repr = 'eopy.product.productIO.Variable.Variable "test_name"'

        output_repr = var.__repr__()

        self.assertEqual(output_repr, expected_repr)
Beispiel #24
0
    def createVariablesAndDomains(self):
        self.variables = []
        for t in range(0, self.time_steps):
            var = Variable("p_sp_{}".format(t), self)
            dom = DomainUpperBound(var, self, t)

            var.setDomain(dom)

            self.variables.append(var)
Beispiel #25
0
    def create_child_state(var_num, already_assigned_variables,
                           unsat_clause_list, truth_assignement):
        """
        Generates the child states to be put in the queue.
        :param var_num:
        :param already_assigned_variables:
        :param unsat_clause_list:
        :param truth_assignement:
        :return:
        """
        var_position_child = Variable.where_is_the_variable(
            unsat_clause_list, var_num)
        # if len(var_position_child) == 0:
        # return None
        unsat_clause_list_child = copy.deepcopy(unsat_clause_list)

        if truth_assignement:
            while len(var_position_child) > 0:
                position = var_position_child[0]
                if unsat_clause_list_child[position[0]][position[1]] > 0:
                    unsat_clause_list_child.pop(position[0])
                else:
                    unsat_clause_list_child[position[0]].pop(position[1])
                    if len(unsat_clause_list_child[position[0]]) == 0:
                        return None

                var_position_child = Variable.where_is_the_variable(
                    unsat_clause_list_child, var_num)
        else:
            while len(var_position_child) > 0:
                position = var_position_child[0]
                if unsat_clause_list_child[position[0]][position[1]] < 0:
                    unsat_clause_list_child.pop(position[0])
                else:
                    unsat_clause_list_child[position[0]].pop(position[1])
                    if len(unsat_clause_list_child[position[0]]) == 0:
                        return None

                var_position_child = Variable.where_is_the_variable(
                    unsat_clause_list_child, var_num)

        for i in range(0, len(unsat_clause_list_child)):
            if len(unsat_clause_list_child[i]) == 1:
                for j in range(i + 1, len(unsat_clause_list_child)):
                    if (len(unsat_clause_list_child[j])
                            == 1) and (unsat_clause_list_child[j][0]
                                       == -1 * unsat_clause_list_child[i][0]):
                        return None

        already_assigned_variables_child = copy.deepcopy(
            already_assigned_variables)
        already_assigned_variables_child.append((var_num, truth_assignement))

        child = State(var_num, truth_assignement,
                      already_assigned_variables_child,
                      unsat_clause_list_child)
        return child
Beispiel #26
0
    def __init__(self, predict = Variable, label=Variable, name=str):
        self.batch_size = predict.shape[0]
        self.input_variables = [predict, label]
        self.loss = Variable([1], name='loss', scope=name, init='None')
        self.prediction = Variable(predict.shape, name='prediction', scope=name)
        self.softmax = np.zeros(self.prediction.shape)

        self.output_variables = [self.loss, self.prediction]
        Operator.__init__(self, name, self.input_variables, self.output_variables)
Beispiel #27
0
 def test_divide_bsl_expr(self):
     assert exp_parser(['/', 1, 1]) == \
            (FuncApplication(Variable('/'), [Num(1), Num(1)]))
     assert exp_parser(['/', 1,['-', 1]]) ==\
            (FuncApplication
             (Variable('/'),
              [Num(1), FuncApplication(Variable('-'), [Num(1)])]))
     assert exp_parser(['/', 1]) == \
            (FuncApplication(Variable('/'), [Num(1)]))
Beispiel #28
0
    def test_execute_int_variables(self):
        from VM import VM
        vm = VM()
        vm.stack.push(Variable(5))
        vm.stack.push(Variable(999))
        x = mul()
        x.execute(vm)

        self.assertEqual(vm.stack.count(), 1)
        self.assertEqual(vm.stack.pop().value, 999 * 5)
Beispiel #29
0
    def test_execute_floats(self):
        from VM import VM
        vm = VM()
        vm.stack.push(Variable(123.4))
        vm.stack.push(Variable(0.01))
        x = mul()
        x.execute(vm)

        self.assertEqual(vm.stack.count(), 1)
        self.assertEqual(vm.stack.pop().value, 123.4 * 0.01)
Beispiel #30
0
    def test_execute_v1_greater_than_v2_ints(self):
        from VM import VM
        vm = VM()
        vm.stack.push(Variable(777))
        vm.stack.push(Variable(44))
        x = ceq()
        x.execute(vm)

        self.assertEqual(vm.stack.count(), 1)
        self.assertEqual(vm.stack.pop().value, 0)
Beispiel #31
0
    def execute(self, vm):
        stack = vm.stack
        m = vm.current_method()
        if stack.get_frame_count() < 1:
            raise StackStateException('Not enough values on the stack')

        array = vm.stack.pop()
        result = Variable(array.length)
        result.type = Types.UInt32
        vm.stack.push(result)
 def _init_variables(self):
     self._site_variables = {}
     for i in xrange(1, 21):
         var_id = 'x' + str(i)
         value = 10 * i
         if i % 2 == 0:  # Even indexed variables are at all sites
             self._site_variables[var_id] = Variable(var_id, value)
         elif 1 + (
             i % 10) == self._id:  # The odd indexed variables are at one site each (i.e. 1 + index number mod 10 )
             self._site_variables[var_id] = Variable(var_id, value)
Beispiel #33
0
 def convert_to_i4(self, input):
     type = Types.Int32
     value = None
     if input.type == Types.UInt32:
         value = input.value # fixme - truncate/overflow/etc
     else:
         raise Exception('Unimplemented i4 conversion ' + input.type)
     result = Variable(value)
     result.type = type
     return result
Beispiel #34
0
    def test_execute_v1_less_than_v2_ints(self):
        from VM import VM
        vm = VM()
        vm.stack.push(Variable(5))
        vm.stack.push(Variable(999))
        x = clt()
        x.execute(vm)

        self.assertEqual(vm.stack.count(), 1)
        self.assertEqual(vm.stack.pop().value, 1)
Beispiel #35
0
 def __init__(self, tag, nombre, descripcion, valorMuyBajo, valorBajo,
              valorAlto, valorMuyAlto):
     Variable.__init__(self,
                       tag=tag,
                       nombre=nombre,
                       descripcion=descripcion)
     self.__valorMuyAlto = valorMuyAlto
     self.__valorAlto = valorAlto
     self.__valorBajo = valorBajo
     self.__valorMuyBajo = valorMuyBajo
Beispiel #36
0
    def test_execute_equal_ints(self):
        from VM import VM
        vm = VM()
        vm.stack.push(Variable(555))
        vm.stack.push(Variable(555))
        x = ceq()
        x.execute(vm)

        self.assertEqual(vm.stack.count(), 1)
        self.assertEqual(vm.stack.pop().value, 1)
Beispiel #37
0
 def execute(self, vm):
     stack = vm.stack
     m = vm.current_method()
     if stack.get_frame_count() < 1:
         raise StackStateException('Not enough values on the stack')
     
     array = vm.stack.pop()
     result = Variable(array.length)
     result.type = Types.UInt32
     vm.stack.push(result)
Beispiel #38
0
 def test_create_method_references_definition(self):
     import Types
     md = MethodDefinition()
     md.name = 'foobar'
     v = Variable()
     v.name = 'asdf'
     v.type = Types.Int32
     
     md.locals.append(v)
     
     m = md.get_method()
     self.assertEqual(m.methodDefinition, md)
Beispiel #39
0
 def parse_parameters(self, method):
     token = self.context.get_next_token()
     while token != ')' and token != '':
         if token != '(':
             type = Types.resolve_type(token)
             name = self.context.get_next_token()
             v = Variable()
             v.type = type
             v.name = name
             method.parameters.append(v)
             
         token = self.context.get_next_token()
Beispiel #40
0
    def create_child_state_heuristic_min_unsat_clauses(var_num, already_assigned_variables, unsat_clause_list,
                                                       truth_assignement):
        """
        Generates the child states to be put in the queue.
        The heuristic minimizes the number of unsatisfied clauses.
        :param var_num:
        :param already_assigned_variables:
        :param unsat_clause_list:
        :param truth_assignement:
        :return:
        """
        var_position_child = Variable.where_is_the_variable(unsat_clause_list, var_num)

        unsat_clause_list_child = copy.deepcopy(unsat_clause_list)

        if truth_assignement:
            while len(var_position_child) > 0:
                position = var_position_child[0]
                if unsat_clause_list_child[position[0]][position[1]] > 0:
                    unsat_clause_list_child.pop(position[0])
                else:
                    unsat_clause_list_child[position[0]].pop(position[1])
                    if len(unsat_clause_list_child[position[0]]) == 0:
                        return None

                var_position_child = Variable.where_is_the_variable(unsat_clause_list_child, var_num)
        else:
            while len(var_position_child) > 0:
                position = var_position_child[0]
                if unsat_clause_list_child[position[0]][position[1]] < 0:
                    unsat_clause_list_child.pop(position[0])
                else:
                    unsat_clause_list_child[position[0]].pop(position[1])
                    if len(unsat_clause_list_child[position[0]]) == 0:
                        return None

                var_position_child = Variable.where_is_the_variable(unsat_clause_list_child, var_num)

        for i in range(0, len(unsat_clause_list_child)):
            if len(unsat_clause_list_child[i]) == 1:
                for j in range(i + 1, len(unsat_clause_list_child)):
                    if (len(unsat_clause_list_child[j]) == 1) and (
                                unsat_clause_list_child[j][0] == -1 * unsat_clause_list_child[i][0]):
                        return None

        already_assigned_variables_child = copy.deepcopy(already_assigned_variables)
        already_assigned_variables_child.append((var_num, truth_assignement))

        child = StateHeuristic(var_num, truth_assignement, already_assigned_variables_child, unsat_clause_list_child)
        child.g = len(already_assigned_variables_child)
        child.h = len(unsat_clause_list_child)
        return child
Beispiel #41
0
    def test_execute_i4_no_overflow(self):
        from VM import VM
        vm = VM()
        v = Variable(999)
        v.type = Types.UInt32
        vm.stack.push(v)
        x = conv('i4')
        x.execute(vm)

        self.assertEqual(vm.stack.count(), 1)
        result = vm.stack.pop()
        self.assertEqual(result.value, 999)
        self.assertEqual(result.type, Types.Int32)
Beispiel #42
0
 def test_create_method_has_new_copy_of_locals(self):
     import Types
     md = MethodDefinition()
     md.name = 'foobar'
     v = Variable()
     v.name = 'asdf'
     v.type = Types.Int32
     
     md.parameters.append(v)
     
     m = md.get_method()
     self.assertEqual(len(m.parameters), 1)
     self.assertNotEqual(v, m.parameters[0])
 def createTimeIndexedVariable(self, name, v_type, coefficient=0.0, interval=1, lb=0.0, ub=1000000):
     numVars = 0
     for i in range(self.currentDay, self.finalDay, interval):
         v = Variable()
         v.name = name + str(i)
         v.col = self.numCols
         v.instant = i
         v.type = v_type
         self.variables[v.name] = v
         self.lp.variables.add(obj=[coefficient], lb=[lb], ub=[ub], names=[v.name])
         self.numCols += 1
         numVars += 1
     return numVars
Beispiel #44
0
 def test_create_method_has_reference_type_instead_of_value(self):
     from Instructions.Ret import Ret
     import Types
     md = MethodDefinition()
     md.name = 'foobar'
     v = Variable()
     v.name = 'asdf'
     v.type = Types.Int32
     md.instructions.append(Ret())
     md.instructions.append(Ret())
     md.instructions.append(Ret())
     
     m = md.get_method()
     self.assertEqual(m.instructions, md.instructions)
Beispiel #45
0
 def test_create_method_has_pointer_to_instructions(self):
     import Types
     from Instructions.Ret import Ret
     md = MethodDefinition()
     md.name = 'foobar'
     v = Variable()
     v.name = 'asdf'
     v.type = Types.Int32
     md.instructions.append(Ret())
     md.instructions.append(Ret())
     md.instructions.append(Ret())
     
     m = md.get_method()
     self.assertEqual(m.instructions, md.instructions)
    def createRepositionVariable(self):
        numVars = 0
        repositionDays = [rDay for rDay in self.pData.repositionDays if rDay >= self.currentDay and rDay < self.finalDay]

        for i in repositionDays:
            v = Variable()
            v.name = "r" + str(i)
            v.col = self.numCols
            v.instant = i
            v.type = Variable.v_reposition
            self.variables[v.name] = v
            self.lp.variables.add(obj=[-params.unitCost], names=[v.name])
            self.numCols += 1
            numVars += 1
        return numVars
 def createDemandVariable(self):
     numVars = 0
     t0 = self.currentDay
     for t in range(self.currentDay, self.finalDay):
         v = Variable()
         v.name = "d" + str(t)
         v.col = self.numCols
         v.instant = t
         v.type = Variable.v_demand
         demand = self.pData.getForecast(t0, t)
         self.variables[v.name] = v
         self.lp.variables.add(obj=[params.unitPrice], lb=[demand], ub=[demand], names=[v.name])
         self.numCols += 1
         numVars += 1
     return numVars
Beispiel #48
0
    def __init__(self, dictionary):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        self.guint_sized_size = ''
        if self.format == "guint-sized":
            if 'guint-size' not in dictionary:
                raise RuntimeError('Format \'guint-sized\' requires \'guint-size\' parameter')
            else:
                self.guint_sized_size = dictionary['guint-size']
            self.private_format = 'guint64'
            self.public_format  = 'guint64'
        else:
            self.private_format = self.format
            self.public_format = dictionary['public-format'] if 'public-format' in dictionary else self.private_format
Beispiel #49
0
    def __init__(self, dictionary, array_element_type, container_type):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        self.private_format  = 'GArray *'
        self.public_format = self.private_format
        self.fixed_size = 0
        self.name = dictionary['name']

        # The array and its contents need to get disposed
        self.needs_dispose = True

        # We need to know whether the variable comes in an Input container or in
        # an Output container, as we should not dump the element clear() helper method
        # if the variable is from an Input container.
        self.container_type = container_type

        # Load variable type of this array
        if 'name' in dictionary['array-element']:
            self.array_element = VariableFactory.create_variable(dictionary['array-element'], array_element_type + ' ' + dictionary['array-element']['name'], self.container_type)
        else:
            self.array_element = VariableFactory.create_variable(dictionary['array-element'], '', self.container_type)

        # Load variable type for the array size prefix
        if 'size-prefix-format' in dictionary:
            # We do NOT allow 64-bit types as array sizes (GArray won't support them)
            if dictionary['size-prefix-format'] not in [ 'guint8', 'guint16', 'guint32' ]:
                raise ValueError('Invalid size prefix format (%s): not guint8 or guint16 or guint32' % dictionary['size-prefix-format'])
            default_array_size = { 'format' : dictionary['size-prefix-format'] }
            self.array_size_element = VariableFactory.create_variable(default_array_size, '', self.container_type)
        elif 'fixed-size' in dictionary:
            # fixed-size arrays have no size element, obviously
            self.fixed_size = dictionary['fixed-size']
            if int(self.fixed_size) == 0 or int(self.fixed_size) > 512:
                raise ValueError('Fixed array size %s out of bounds (not between 0 and 512)' % self.fixed_size)
        else:
            # Default to 'guint8' if no explicit array size given
            default_array_size = { 'format' : 'guint8' }
            self.array_size_element = VariableFactory.create_variable(default_array_size, '', self.container_type)

        # Load variable type for the sequence prefix
        if 'sequence-prefix-format' in dictionary:
            sequence = { 'format' : dictionary['sequence-prefix-format'] }
            self.array_sequence_element = VariableFactory.create_variable(sequence, '', self.container_type)
        else:
            self.array_sequence_element = ''
Beispiel #50
0
    def __init__(self, dictionary, sequence_type_name, container_type):

        # Call the parent constructor
        Variable.__init__(self, dictionary)

        self.container_type = container_type

        # Load members of this sequence
        self.members = []
        for member_dictionary in dictionary['contents']:
            member = {}
            member['name'] = utils.build_underscore_name(member_dictionary['name'])
            member['object'] = VariableFactory.create_variable(member_dictionary, sequence_type_name + ' ' + member['name'], self.container_type)
            self.members.append(member)

        # TODO: do we need this?
        # We'll need to dispose if at least one of the members needs it
        for member in self.members:
            if member['object'].needs_dispose == True:
                self.needs_dispose = True
    def createRepositionVariable(self):
        numVars = 0
        repositionDays = [rDay for rDay in self.pData.repositionDays if rDay >= self.currentDay and rDay < self.finalDay]

        # get the maximum forecast for using it as upper bound for r variable
        maxDemand = max(s.maxForecast for s in self.scenarios)

        # The reposition variable remains only for each t in the current planning horizon
        for i in repositionDays:
            v = Variable()
            v.type = Variable.v_reposition
            v.name = "r_" + str(i)
            v.col = self.numCols
            v.instant = i
            self.variables[v.name] = v
            self.lp.variables.add(names=[v.name])
            self.numCols += 1
            numVars += 1

        return numVars
Beispiel #52
0
    def test_execute_multiple_fields(self):
        from VM import VM
        vm = VM()
        
        c = ClassDefinition()
        c.namespace = 'a'
        c.name = 'b'
        
        v = Variable()
        v.name = 'abc'
        v.type = Types.Int32
 
        v2 = Variable()
        v2.name = 'def'
        v2.type = Types.Int32
        c.fieldDefinitions.append(v2)
        
        r = ReferenceType()
        t = Types.register_custom_type(c)
        r.type = t
        
        r.add_field(v)
        r.add_field(v2)
        vm.stack.push(r)
        
        x = ldfld('int32 ConsoleApplication1.foo::def')
        
        x.execute(vm)
        self.assertEqual(vm.stack.count(), 1)
        self.assertEqual(r.fields[1], vm.stack.pop())
Beispiel #53
0
    def parse(self, parserContext):
        c = ClassDefinition()

        while True:
            token = parserContext.get_next_token()
            if token in ClassFlags:
                c.flags.append(token)
            elif token == 'extends':
                c.base = parserContext.get_next_token()
            elif token == '.method':
                m = MethodParser().parse(parserContext)
                m.namespace = c.namespace + '.' + c.name       
                c.methods.append(m)
                parserContext.methods.append(m)
                # fixme - should i add to both?
            elif token == '.field':
                v = Variable()
                visibility = parserContext.get_next_token()
                type = parserContext.get_next_token() # fixme - type, visibility
                if type == 'class':
                    type = parserContext.get_next_token()
                
                if Types.BuiltInTypes.has_key(type):
                    v.type = Types.BuiltInTypes[type]
                else:
                    v.type = Types.resolve_type(type)
                    
                name = parserContext.get_next_token()
               
                v.name = name
                c.fieldDefinitions.append(v)
            elif token == '}':
                break
            elif token != '{':
                fullyQualifiedName = token.split('.')
                c.name = fullyQualifiedName[-1]
                c.namespace = '.'.join(fullyQualifiedName[:-1])
                
        Types.register_custom_type(c)
        return c
Beispiel #54
0
 def test_execute_single_field(self):
     from VM import VM
     vm = VM()
     
     c = ClassDefinition()
     c.namespace = 'ConsoleApplication1'
     c.name = 'foo'
     
     v = Variable()
     v.name = 'z'
     v.type = Types.Int32
     
     r = ReferenceType()
     t = Types.register_custom_type(c)
     r.type = t
     r.add_field(v)
     vm.stack.push(r)
     
     x = ldfld('int32 ConsoleApplication1.foo::z')
     
     x.execute(vm)
     self.assertEqual(vm.stack.count(), 1)
     self.assertEqual(r.fields[0], vm.stack.pop())
Beispiel #55
0
class Test(unittest.TestCase):
	"""Currently tests Variable only with the AsnDmcEventSystem"""
	def setUp(self):
		self.pq = deque()
		self.es = AsnDmcEventSystem(self.pq)
		self.es.subscribe('p0', 0)
		self.es.subscribe('p1', 1)
		self.v = Variable([0,1,2,3], self.es)
		
	def testVariableInit(self):
		self.assertEquals(self.v._domain, set([0,1,2,3]))
		self.assertFalse(self.v.assigned)
		self.assertIs(self.v._eventSystem, self.es)
		
	def testRemoveFromDomain(self):
		self.v.removeFromDomain([0,1,3])
		
		self.assertEquals(self.v._domain, set([2]))
		self.assertTrue(self.v.assigned)
		
		self.assertTrue(len(self.pq) == 2)
		self.assertIn('p1', self.pq)
		self.assertIn('p0', self.pq)
    def createZVariables(self):
        numVars = 0

        # create the zp variable
        v1 = Variable()
        v1.type = Variable.v_zp
        v1.name = "zp"
        v1.col = self.numCols
        self.variables[v1.name] = v1
        self.lp.variables.add(obj=[1.0], names=[v1.name])
        self.numCols += 1
        numVars += 1

        # create the zn variable
        v2 = Variable()
        v2.type = Variable.v_zn
        v2.name = "zn"
        v2.col = self.numCols
        self.variables[v2.name] = v2
        self.lp.variables.add(obj=[-1.0], names=[v2.name])
        self.numCols += 1
        numVars += 1

        return numVars
    def createStockVariable(self):
        numVars = 0

        # the s variables are for each scenario and for each t of current horizon
        for scenario in self.scenarios:
            for t in range(self.currentDay, self.finalDay):
                # create the variable
                v = Variable()
                v.type = Variable.v_stock
                v.name = "s_" + scenario.id + "_" + str(t)
                v.col = self.numCols
                v.instant = t
                v.scenario = scenario.id
                self.variables[v.name] = v
                self.lp.variables.add(names=[v.name])
                self.numCols += 1
                numVars += 1

        return numVars
Beispiel #58
0
 def parse_locals(self, context):
     from ParserContext import ParseException
     locals = []
     token = context.get_next_token()
     if token != 'init':
         raise ParseException('Expected init, found ' + token) # fixme - only required for verifiable methods
     token = context.get_next_token()
     if token != '(':
         raise ParseException('Expected (, found' + token)
     token = context.get_next_token()
     lastToken = ''
     while not token.endswith(')'):
         v = Variable()
         if token.startswith('['):
             v.alias = token[1:-1]
             lastToken = token
             token = context.get_next_token()
         if token == 'class':
             v2 = ReferenceType()
             v2.alias = v.alias
             v2.type = Types.resolve_type(context.get_next_token())
             v = v2
         elif token.endswith('[]'): # array
             v.type = Types.Array
             v.arrayType = Types.resolve_type(token[:-2])
         else:
             v.type = Types.BuiltInTypes[token] # fixme - non-builtin types
         
         locals.append(v)
         lastToken = token
         token = context.get_next_token()
         #if token.endswith(')'):
         #    v.name = token[:-1]
         #    token = ')'
         #else:
         v.name = token
         lastToken= token
         token = context.get_next_token()
         
     return locals