Ярлыки

пятница, 9 августа 2013 г.

mongodb replicaset localhost

How I set "localhost" to replicaSet host (where "rs0" is name of my replicaSet):
use local
cfg = db.system.replset.findOne({_id:"rs0"})
cfg.members[0].host="localhost:27017"
db.system.replset.update({_id:"rs0"},cfg)
then restart mongo

понедельник, 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 i, m(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 sum(X(i,j)) for all j with follow constraints:
1)    upper bound for elements count from each group:
sumj(X(i,j))<=n(i) 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 (like matrix n by m).


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 mainWindow(QtGui.QMainWindow, UIclass.Ui_MainWindow):
    def __init__(self, win_parent = None):
        QtGui.QMainWindow.__init__(self, win_parent)
        self.setupUi(self)
        #create QGridLayout on the first start
        self.scrollArea.widget().layout = QtGui.QGridLayout()
        self.scrollArea.widget().layout.objectName='inputLayout'
        # create cells for input data
        self.connect(self.createCells, QtCore.SIGNAL("clicked()"), self.createCellsFunc)

    # function for create cells:
    def createCellsFunc(self):
        # all was doing if we know number columns and number rows
        if str(self.text1.toPlainText()).isdigit() and str(self.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.setParent(self)
                
            # number of columns
            hCount=int(self.text1.toPlainText())
            # number of rows
            vCount=int(self.text2.toPlainText())

            for i in range(0,hCount):
                for j in range(0,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'+str(i)+'-'+str(j)
                    # dimentions
                    textArea.setMinimumSize(l,h)
                    textArea.setMaximumSize(l,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.addWidget(textArea,j+1,i+1)
            # put layer to widget
            self.scrollArea.widget().setLayout(self.scrollArea.widget().layout)
            self.scrollArea.widget().layout

3) get data from cells:

            #horizontal cells count
            hCount=int(self.text1.toPlainText())
            #vertical cells count
            vCount=int(self.text2.toPlainText())
            #check for all child objects "ScrollBox": did all data is number?
            for u in self.scrollArea.findChildren(QtGui.QTextEdit):
                try:
                    float(str(u.toPlainText()))
                except:
                    self.labelWarning.setText(QtCore.QString(u'Some data is not number'))
                    return None
            
            #put data to array
            a,a0=list(),list()
            for j in range(0,vCount):
                for i in range(0,hCount):
                    for u in self.scrollArea.findChildren(QtGui.QTextEdit):
                        if u.objectName=='a'+str(i)+'-'+str(j):
                            a0.append(float(u.toPlainText()))
                a.append(a0)
                a0=list()

четверг, 21 марта 2013 г.

use mosek for solve LP in Python


#import module
import mosek
#import numpy arrays or mosek arraystry:    from numpy import array,zeros,ones
except ImportError:
    from mosek.array import array, zeros, ones
# stream for collect data from MOSEKdef streamprinter(text):    sys.stdout.flush()     #main functiondef main (bkc,blc,buc,bkx,blx,bux,csub,cval,asub,acof,ptrb,ptre,niv,ntc):  # create mosek environment  env = mosek.Env ()  # add stream to environment  env.set_Stream (mosek.streamtype.log, streamprinter)    # create task  task = env.Task(0,0)  # add output-data stream to task  task.set_Stream (mosek.streamtype.log, streamprinter)
#there are examples of creation right part of constraints and variable constraints#  bkc = [ mosek.boundkey.up, mosek.boundkey.lo  ]#  blc = [              -inf,              -4.0  ]
#  buc = [             250.0,               inf  ]
#  bkx = [ mosek.boundkey.lo, mosek.boundkey.lo  ]
#  blx = [               0.0,               0.0  ]
#  bux = [               inf,               inf  ]
#coefficients of goal function#  c   = [               1.0,               0.64 ]
#coeff numbers in constraints
#  asub = [  array([0,   1]),    array([0,    1])   ]
#...and this values
#  aval = [ array([50.0, 3.0]), array([31.0, -2.0]) ]
#number of variables  numvar = len(bkx)
#number of constraints  numcon = len(bkc)
  # create constraints
  task.append(mosek.accmode.con,numcon)
  # create variables
  task.append(mosek.accmode.var,numvar)
  # The goal function adding (csub - numbers of variables, cval - coefficients of variables)
  task.putcfix(0.0)  task.putclist(csub,cval)
  # add constraints  task.putboundslice(mosek.accmode.con,                     0, numcon,
                     bkc, blc, buc)
  # add variable constraints  task.putboundslice(mosek.accmode.var,                     0, numvar,
                     bkx, blx, bux)
  # Input non-zero elements of matrix A (by column)  for j in range(numcon):
      aptrb,aptre = ptrb[j],ptre[j]
      task.putavec(mosek.accmode.con,j,
                   asub[aptrb:aptre],
                   acof[aptrb:aptre])
  # input optimize criterion (minimize/maximize)  task.putobjsense(mosek.objsense.minimize)    # integer variables  numlist=list()
  typelist=list()
  for i in range(niv,numvar):
      numlist.append(i)
      typelist.append(mosek.variabletype.type_int)
  task.putvartypelist(numlist,typelist)
   
  # constants of begin solve   task.putintparam(mosek.iparam.mio_construct_sol,                   mosek.onoffkey.on);
  # all variable are unknown in begin of solve   task.makesolutionstatusunknown(mosek.soltype.itg);
 #we can save task in some file #task.writedata("test.lp")   # solve task  task.optimize()
  # output solution
  if task.solutiondef(mosek.soltype.itg):
      ...