Ejemplo n.º 1
0
    def _read_block(self, stoch_file, line):
        fields = line.split()
        if len(fields) < 4:
            raise FormatError('Missing information in block header',
                              'BLOCK header BL must have at least 4 fields')
        name = fields[1]
        period = fields[2]
        prob = float(fields[3])
        elements = []
        realization = []

        line = stoch_file.readline()
        fields = line.split()
        while fields[0] not in _STOCH_FILE_SECTION_NAMES and fields[0] != 'BL':
            column_name = fields[0]
            column = self._column_by_name.get(column_name, None)
            if column is None and column_name != self._rhs_name:
                raise FormatError('Column not found',
                                  "Column '%s' was not found" % column_name)
            for i in range(1, len(fields), 2):
                row_name = fields[i]
                coef = float(fields[i + 1])
                row = self._row_by_name.get(row_name, None)
                if row is None:
                    raise FormatError('Row not found',
                                      "Row %s was not found" % row_name)
                elements.append((column_name, row_name))
                realization.append(coef)
            line = stoch_file.readline()
            fields = line.split()

        return (name, period, elements, prob, realization, line)
Ejemplo n.º 2
0
    def _read_name_section(self, core_file):
        line = core_file.readline()
        fields = line.split()
        if len(fields) < 2:
            raise FormatError('Name section missing',
                              'Less than 2 fields in first line of file')
        if fields[0] != 'NAME':
            raise FormatError('Name section missing',
                              'Wrong name of the section')
        name = fields[1]
        #print 'NAME =', name
        self._name = name

        return line
Ejemplo n.º 3
0
    def _read_time_section(self, time_file):
        line = time_file.readline()
        fields = line.split()
        if len(fields) < 2:
            raise FormatError('Time section missing',
                              'Less than 2 fields in first line of file')
        if fields[0] != 'TIME':
            raise FormatError('Time section missing',
                              'Wrong name of the section')
        instance_name = fields[1]
        #print 'TIME =', instance_name
        #if instance_name != self._name:
        #    raise WrongFileError('Time file for different problem instance', "Core file describes problem %s, while time file describes problem %s" % (self._name, instance_name))

        return line
Ejemplo n.º 4
0
    def _read_rhs_section(self, core_file, last_read_line):
        fields = last_read_line.split()
        rhs_name = 'RHS'
        if len(fields) > 1:
            rhs_name = fields[1]
        #print 'RHS Name = ', rhs_name
        self._rhs_name = rhs_name

        line = core_file.readline()
        fields = line.split()
        while len(fields) >= 3:
            rhs_name = fields[0]
            self._rhs_name = rhs_name
            for i in range(1, len(fields), 2):
                row_name = fields[i]
                coef = float(fields[i + 1])
                #print 'Coefficient of rhs', rhs_name, 'in row', row_name, '=', coef
                row = self._row_by_name.get(row_name, None)
                if row is None:
                    raise FormatError('Row not found',
                                      "Row %s was not found" % row_name)
                row.set_rhs(rhs_name, coef)

            line = core_file.readline()
            fields = line.split()

        return line
Ejemplo n.º 5
0
 def add_realization(self, elements, prob, realization):
     if elements != self._coef_elements:
         raise FormatError(
             'Wrong block specification',
             'Elements of realization different from original block elements or in different order'
         )
     self._coef_realizations.append(realization)
     self._realization_prob.append(prob)
Ejemplo n.º 6
0
    def _read_indep_section(self, stoch_file, last_read_line):
        fields = last_read_line.split()
        if len(fields) > 1 and fields[1] != 'DISCRETE':
            raise NotSupportedError(
                'INDEP section type not supported',
                "SMPSReader only support DISCRETE INDEP section, received %s" %
                fields[1])

        line = stoch_file.readline()
        fields = line.split()
        while fields[0] not in _STOCH_FILE_SECTION_NAMES:
            col_name = fields[0]
            row_name = fields[1]

            column = self._column_by_name.get(col_name, None)
            if column is None and col_name != self._rhs_name:
                raise FormatError('Column not found',
                                  "Column '%s' was not found" % col_name)
            row = self._row_by_name.get(row_name, None)
            if row is None:
                raise FormatError('Row not found',
                                  "Row %s was not found" % row_name)

            period = None
            value = float(fields[2])
            if len(fields) == 5:
                period = fields[3]
                prob = float(fields[4])
            else:
                prob = float(fields[3])

            indep = self._indeps_by_col_row.get((col_name, row_name), None)
            if indep is None:
                indep = Indep(col_name, row_name, period)
                self._indeps.append(indep)
                self._indeps_by_col_row[(col_name, row_name)] = indep
            indep.add_realization(prob, value)

            line = stoch_file.readline()
            fields = line.split()

        return line
Ejemplo n.º 7
0
    def _read_columns_section(self, core_file, last_read_line):
        fields = last_read_line.split()
        if len(fields) < 1:
            raise FormatError('COLUMNS section missing', 'Empty line')
        section_name = fields[0]
        if section_name != 'COLUMNS':
            raise FormatError(
                'COLUMNS section missing',
                "Expecting COLUMNS section received '%s'" % section_name)

        line = core_file.readline()
        fields = line.split()
        id = 0
        while len(fields) >= 3:
            column_name = fields[0]
            column = self._column_by_name.get(column_name, None)
            if column is None:
                column = Column(id, column_name)
                self._columns.append(column)
                self._column_by_name[column_name] = column
                #print 'Column[', id, ']:', column_name
                id = id + 1

            for i in range(1, len(fields), 2):
                row_name = fields[i]
                coef = float(fields[i + 1])
                #print 'Coefficient of column', column_name, 'in row', row_name, '=', coef
                row = self._row_by_name.get(row_name, None)
                if row is None:
                    raise FormatError('Row not found',
                                      "Row %s was not found" % row_name)
                row.set_coef_to_column(column, coef)

            line = core_file.readline()
            fields = line.split()

        return line
Ejemplo n.º 8
0
    def _read_rows_section(self, core_file):
        fields = core_file.readline().split()
        if len(fields) < 1:
            raise FormatError('Rows section missing', 'Empty line')
        if fields[0] != 'ROWS':
            raise FormatError('Rows section missing',
                              "Expecting ROWS section received %s" % fields[0])

        line = core_file.readline()
        fields = line.split()
        id = 0
        while len(fields) >= 2:
            type_name = fields[0]
            name = fields[1]
            type = RowType.type_name_to_row_type(type_name)
            if type is None:
                raise FormatError('Wrong type name',
                                  "Invalid Type: %s" % type_name)
            row = Row(id, name, type)
            if type == RowType.N and self._obj is None:
                #print 'Objective Function =', name
                self._obj = row
            self._rows.append(row)
            self._row_by_name[name] = row
            #print 'Row[',id, ']:', type_name, name
            id = id + 1
            line = core_file.readline()
            fields = line.split()

        if self._obj is None:
            raise FormatError(
                'Missing Objective Function',
                'The file must have at least one ROW of type N, representing the objective function'
            )

        return line
Ejemplo n.º 9
0
    def _read_periods_section(self, time_file):
        fields = time_file.readline().split()
        if len(fields) < 1:
            raise FormatError('Periods section missing', 'Empty line')
        if fields[0] != 'PERIODS':
            raise FormatError(
                'Periods section missing',
                "Expecting PERIODS section received %s" % fields[0])
        if len(fields) > 1 and fields[1] != 'IMPLICIT':
            raise NotSupportedError(
                'Time section type not supported',
                "SMPSReader only support IMPLICIT time section, received %s" %
                fields[1])

        line = time_file.readline()
        fields = line.split()

        period_names = []
        row_indexes = []
        column_indexes = []
        while len(fields) >= 3:
            column_name = fields[0]
            row_name = fields[1]
            period_name = fields[2]

            column = self._column_by_name.get(column_name, None)
            if column is None:
                raise FormatError('Column not found',
                                  "Column %s was not found" % column_name)
            row = self._row_by_name.get(row_name, None)
            if row is None:
                raise FormatError('Row not found',
                                  "Row %s was not found" % row_name)

            period_names.append(period_name)
            column_indexes.append(column.get_id())
            if row.get_id(
            ) == 0:  # Assuming that objective function is the first row
                row_indexes.append(1)  # skip objective function
            else:
                row_indexes.append(row.get_id())
            #print 'Reading period col/row begins [', period_name, column_name, row_name, ']'

            line = time_file.readline()
            fields = line.split()

        n_periods = len(period_names)
        for i in range(n_periods - 1):
            column_range = (column_indexes[i], column_indexes[i + 1])
            row_range = (row_indexes[i], row_indexes[i + 1])
            period = Period(period_names[i], column_range, row_range)
            self._periods.append(period)
        column_range = (column_indexes[n_periods - 1], len(self._columns))
        row_range = (row_indexes[n_periods - 1], len(self._rows))
        period = Period(period_names[n_periods - 1], column_range, row_range)
        self._periods.append(period)

        for p in self._periods:
            column_range = p.get_column_range()
            row_range = p.get_row_range()
            #print 'Period', p.get_name(), '= col ', column_range, ', row', row_range

        return line