Processing math: 41%

Ярлыки

понедельник, 22 июля 2013 г.

How we can get sets from elements of different groups?


This task may be present as LP problem.
So, let n(i) - count of elements in group im(j) - volume of cell number j. Our goal is to fill all calls with condition: there are elements from different groups in each cell.
Let X(i,j) - count of element that from group  i and put into cell j.
Our main goal is maximize sumX(i,j) for all j with follow constraints:
1)    upper bound for elements count from each group:
sumjX(i,j)<=ni for all i;
2)    upper bound for volume of each cell:
sumi(X(i,j))<=m(j) for all j;
3) there are elements from the same group no more than t in each cell :
X(i,j)<=t for all i и j.

PyQt: create components dinamically


What to do: create some number of cells likematrixnbym.


1) we create ScrollArea in *.ui file, and QWidget under it:

<widget class="QScrollArea" name="scrollArea">
          <property name="mouseTracking">
           <bool>true</bool>
          </property>
          <property name="verticalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOn</enum>
          </property>
          <property name="horizontalScrollBarPolicy">
           <enum>Qt::ScrollBarAlwaysOn</enum>
          </property>
          <property name="widgetResizable">
           <bool>true</bool>
          </property>

          <widget class="QWidget" name="contents">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>788</width>
             <height>223</height>
            </rect>
           </property>
          </widget>
         </widget>


2) my code:

class mainWindowQtGui.QMainWindow,UIclass.UiMainWindow:
    def __init__self,winparent=None:
        QtGui.QMainWindow.__init__self,winparent
        self.setupUiself
        #create QGridLayout on the first start
        self.scrollArea.widget.layout = QtGui.QGridLayout
        self.scrollArea.widget.layout.objectName='inputLayout'
        # create cells for input data
        self.connectself.createCells, QtCore.SIGNAL("clicked("), self.createCellsFunc)

    # function for create cells:
    def createCellsFuncself:
        # all was doing if we know number columns and number rows
        if strself.text1.toPlainText().isdigit and strself.text2.toPlainText().isdigit:
            # height and length for each cell
            h=25
            l=50
            # delete all old objects exclude layout
            for u in self.scrollArea.widget.children:
                if u.objectName!='inputLayout':
                    # "delete some later"
                    u.deleteLater
                    # hide now
                    u.hide
                    # set window as parent not ScrollArea
                    u.setParentself
                
            # number of columns
            hCount=intself.text1.toPlainText()
            # number of rows
            vCount=intself.text2.toPlainText()

            for i in range0,hCount:
                for j in range0,vCount:
                    # while creating insert text '0' and determine parent
                    textArea = QtGui.QTextEdit'0', self.scrollArea
                    # we give name to the element for fast find him
                    textArea.objectName = 'a'+stri+'-'+strj
                    # dimentions
                    textArea.setMinimumSizel,h
                    textArea.setMaximumSizel,h
                    # add created element to layer of widget textArea - adding object, (j + 1 and i + 1 - coordinates on layer grid of adding object)
                    self.scrollArea.widget.layout.addWidgettextArea,j+1,i+1
            # put layer to widget
            self.scrollArea.widget.setLayoutself.scrollArea.widget(.layout)
            self.scrollArea.widget.layout

3) get data from cells:

            #horizontal cells count
            hCount=intself.text1.toPlainText()
            #vertical cells count
            vCount=intself.text2.toPlainText()
            #check for all child objects "ScrollBox": did all data is number?
            for u in self.scrollArea.findChildrenQtGui.QTextEdit:
                try:
                    floatstr(u.toPlainText())
                except:
                    self.labelWarning.setTextQtCore.QString(u'Some data is not number')
                    return None
            
            #put data to array
            a,a0=list,list
            for j in range0,vCount:
                for i in range0,hCount:
                    for u in self.scrollArea.findChildrenQtGui.QTextEdit:
                        if u.objectName=='a'+stri+'-'+strj:
                            a0.appendfloat(u.toPlainText())
                a.appenda0
                a0=list