Exemplo n.º 1
0
 def test_union(self):
   self.message("Sparsity union")
   nza = set([  (0,0),
            (0,1),
            (2,0),
            (3,1)])
   nzb = set([  (0,2),
            (0,0),
            (2,2)])
   
   a = Sparsity(4,5)
   for i in nza:
     a.addNZ(i[0],i[1])
     
   b = Sparsity(4,5)  
   for i in nzb:
     b.addNZ(i[0],i[1])
     
   c,w =a.patternUnion(b)
   self.assertEquals(len(w),len(nza.union(nzb)))
   for k in range(len(w)):
     ind = (c.row(k),c.getCol()[k])
     if (ind in nza and ind in nzb):
       self.assertEquals(w[k],1 | 2)
     elif (ind in nza):
       self.assertEquals(w[k],1)
     elif (ind in nzb):
       self.assertEquals(w[k],2)
       
   c = a + b
   self.assertEquals(c.nnz(),len(nza.union(nzb)))
   for k in range(c.nnz()):
     ind = (c.row(k),c.getCol()[k])
     self.assertTrue(ind in nza or ind in nzb)
Exemplo n.º 2
0
  def test_intersection(self):
    self.message("Sparsity intersection")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (3,1),
             (2,3)])
    nzb = set([  (0,2),
             (0,0),
             (2,2),
             (2,3)])

    a = Sparsity(4,5)
    for i in nza:
      a.add_nz(i[0],i[1])

    b = Sparsity(4,5)
    for i in nzb:
      b.add_nz(i[0],i[1])

    c=a.intersect(b)
    for k in range(c.nnz()):
      ind = (c.row(k),c.get_col()[k])
      self.assertTrue(ind in nza and ind in nzb)

    c = a * b
    self.assertEqual(c.nnz(),len(nza.intersection(nzb)))
    for k in range(c.nnz()):
      ind = (c.row(k),c.get_col()[k])
      self.assertTrue(ind in nza and ind in nzb)
Exemplo n.º 3
0
  def test_intersection(self):
    self.message("Sparsity intersection")
    nza = set([  (0,0),
             (0,1),
             (2,0),
             (3,1),
             (2,3)])
    nzb = set([  (0,2),
             (0,0),
             (2,2),
             (2,3)])

    a = Sparsity(4,5)
    for i in nza:
      a.add_nz(i[0],i[1])

    b = Sparsity(4,5)
    for i in nzb:
      b.add_nz(i[0],i[1])

    c=a.intersect(b)
    for k in range(c.nnz()):
      ind = (c.row(k),c.get_col()[k])
      self.assertTrue(ind in nza and ind in nzb)

    c = a * b
    self.assertEqual(c.nnz(),len(nza.intersection(nzb)))
    for k in range(c.nnz()):
      ind = (c.row(k),c.get_col()[k])
      self.assertTrue(ind in nza and ind in nzb)
Exemplo n.º 4
0
 def test_union(self):
   self.message("Sparsity union")
   nza = set([  (0,0),
            (0,1),
            (2,0),
            (3,1)])
   nzb = set([  (0,2),
            (0,0),
            (2,2)])
   
   a = Sparsity(4,5)
   for i in nza:
     a.add_nz(i[0],i[1])
     
   b = Sparsity(4,5)  
   for i in nzb:
     b.add_nz(i[0],i[1])
     
   c =a.unite(b)
       
   c = a + b
   self.assertEquals(c.nnz(),len(nza.union(nzb)))
   for k in range(c.nnz()):
     ind = (c.row(k),c.get_col()[k])
     self.assertTrue(ind in nza or ind in nzb)
Exemplo n.º 5
0
  def test_sparsity_operation(self):
    L = [DM(1), DM(Sparsity(1,1),1), DM(Sparsity(2,1),1), DM(Sparsity.dense(2,1),1)]

    for a in L:
      for b in L:
        c = a*b

        if a.nnz()==0 or b.nnz()==0:
          self.assertTrue(c.nnz()==0)
        else:
          self.assertTrue(c.nnz()>0)

    self.assertTrue(sum2(IM(Sparsity(1,1),1)).nnz()==0)
Exemplo n.º 6
0
  def test_sparsity_operation(self):
    L = [DM(1), DM(Sparsity(1,1),1), DM(Sparsity(2,1),1), DM(Sparsity.dense(2,1),1)]

    for a in L:
      for b in L:
        c = a*b

        if a.nnz()==0 or b.nnz()==0:
          self.assertTrue(c.nnz()==0)
        else:
          self.assertTrue(c.nnz()>0)

    self.assertTrue(sum2(IM(Sparsity(1,1),1)).nnz()==0)
Exemplo n.º 7
0
  def test_mxnull(self):
     a = SX(5,0)
     b = SX(0,3)

     c = mtimes(a,b)

     self.assertEqual(c.nnz(),0)

     a = SX(5,3)
     b = SX(3,4)

     c = mtimes(a,b)

     self.assertEqual(c.nnz(),0)
Exemplo n.º 8
0
  def test_mxnull(self):
     a = SX(5,0)
     b = SX(0,3)

     c = mtimes(a,b)

     self.assertEqual(c.nnz(),0)

     a = SX(5,3)
     b = SX(3,4)

     c = mtimes(a,b)

     self.assertEqual(c.nnz(),0)
Exemplo n.º 9
0
    def test_union(self):
        self.message("Sparsity union")
        nza = set([(0, 0), (0, 1), (2, 0), (3, 1)])
        nzb = set([(0, 2), (0, 0), (2, 2)])

        a = Sparsity(4, 5)
        for i in nza:
            a.add_nz(i[0], i[1])

        b = Sparsity(4, 5)
        for i in nzb:
            b.add_nz(i[0], i[1])

        c = a.unite(b)

        c = a + b
        self.assertEquals(c.nnz(), len(nza.union(nzb)))
        for k in range(c.nnz()):
            ind = (c.row(k), c.get_col()[k])
            self.assertTrue(ind in nza or ind in nzb)
Exemplo n.º 10
0
Arquivo: sdp.py Projeto: mzanon/casadi
    def test_example1(self):
        self.message("Example1")
        # Originates from http://sdpa.indsys.chuo-u.ac.jp/sdpa/files/sdpa-c.6.2.0.manual.pdf
        c = DMatrix([48, -8, 20])

        A = DMatrix(0, 3)

        Fi = [-DMatrix([[10, 4], [4, 0]]), -DMatrix([[0, 0], [0, -8]]), -DMatrix([[0, -8], [-8, -2]])]

        F = horzcat(Fi)
        F = sparsify(F)

        print F

        G = -DMatrix([[-11, 0], [0, 23]])
        G = sparsify(G)

        for sdpsolver, sdp_options in sdpsolvers:
            sdp = SdpSolver(sdpsolver, sdpStruct(a=A.sparsity(), g=G.sparsity(), f=F.sparsity()))
            sdp.setOption(sdp_options)
            sdp.init()

            sdp.setInput(G, "g")
            sdp.setInput(c, "c")
            sdp.setInput(F, "f")

            sdp.evaluate()

            self.checkarray(sdp.getOutput("cost"), DMatrix(-41.9), digits=5)
            self.checkarray(sdp.getOutput("dual_cost"), DMatrix(-41.9), digits=5)
            self.checkarray(sdp.getOutput("x"), DMatrix([-1.1, -2.7375, -0.55]), digits=5)

            self.checkarray(sdp.getOutput("dual"), DMatrix([[5.9, -1.375], [-1.375, 1]]), digits=5)
            self.checkarray(sdp.getOutput("p"), DMatrix.zeros(2, 2), digits=5)

            try:
                NlpSolver.loadPlugin("ipopt")
            except:
                return

            V = struct_symSX([entry("L", shape=G.shape), entry("x", shape=c.nnz())])
            L = V["L"]
            x = V["x"]

            P = mul(L, L.T)

            g = []
            g.append(sum([-Fi[i] * x[i] for i in range(3)]) + G - P)

            nlp = SXFunction(nlpIn(x=V), nlpOut(f=mul(c.T, x), g=veccat(g)))

            sol = NlpSolver("ipopt", nlp)
            sol.init()
            sol.setInput(0, "lbg")
            sol.setInput(0, "ubg")
            V(sol.input("x0"))["x"] = -1
            V(sol.input("x0"))["L"] = 0.1

            sol.evaluate()

            sol_ = V(sol.getOutput())

            self.checkarray(sol_["x"], DMatrix([-1.1, -2.7375, -0.55]), digits=5)