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 = CRSSparsity(4,5) for i in nza: a.getNZ(i[0],i[1]) b = CRSSparsity(4,5) for i in nzb: b.getNZ(i[0],i[1]) w = UCharVector() c=a.patternUnion(b,w) self.assertEquals(w.size(),len(nza.union(nzb))) for k in range(w.size()): ind = (c.getRow()[k],c.col(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.size(),len(nza.union(nzb))) for k in range(c.size()): ind = (c.getRow()[k],c.col(k)) self.assertTrue(ind in nza or ind in nzb)
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 = CRSSparsity(4,5) for i in nza: a.getNZ(i[0],i[1]) b = CRSSparsity(4,5) for i in nzb: b.getNZ(i[0],i[1]) w = UCharVector() c=a.patternUnion(b,w,True,True,True) for k in range(c.size()): ind = (c.getRow()[k],c.col(k)) self.assertTrue(ind in nza and ind in nzb) c = a * b self.assertEquals(c.size(),len(nza.intersection(nzb))) for k in range(c.size()): ind = (c.getRow()[k],c.col(k)) self.assertTrue(ind in nza and ind in nzb)
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.sparse(4, 5) for i in nza: a.getNZ(i[0], i[1]) b = Sparsity.sparse(4, 5) for i in nzb: b.getNZ(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.size(), len(nza.union(nzb))) for k in range(c.size()): ind = (c.row(k), c.getCol()[k]) self.assertTrue(ind in nza or ind in nzb)
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.sparse(4,5) for i in nza: a.getNZ(i[0],i[1]) b = Sparsity.sparse(4,5) for i in nzb: b.getNZ(i[0],i[1]) c,_=a.patternIntersection(b) for k in range(c.size()): ind = (c.row(k),c.getCol()[k]) self.assertTrue(ind in nza and ind in nzb) c = a * b self.assertEquals(c.size(),len(nza.intersection(nzb))) for k in range(c.size()): ind = (c.row(k),c.getCol()[k]) self.assertTrue(ind in nza and ind in nzb)
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.sparse(4,5) for i in nza: a.getNZ(i[0],i[1]) b = Sparsity.sparse(4,5) for i in nzb: b.getNZ(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.size(),len(nza.union(nzb))) for k in range(c.size()): ind = (c.row(k),c.getCol()[k]) self.assertTrue(ind in nza or ind in nzb)
def test_mxnull(self): a = SX.sparse(5,0) b = SX.sparse(0,3) c = mul(a,b) self.assertEqual(c.size(),0) a = SX.sparse(5,3) b = SX.sparse(3,4) c = mul(a,b) self.assertEqual(c.size(),0)
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.sparse(0,3) Fi = [-DMatrix([[10,4],[4,0]]),-DMatrix([[0,0],[0,-8]]),-DMatrix([[0,-8],[-8,-2]])] F = horzcat(Fi) F = sparse(F) print F G = -DMatrix([[-11,0],[0,23]]) G = sparse(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.size()) ]) 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)
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.sparse(0, 3) Fi = [ -DMatrix([[10, 4], [4, 0]]), -DMatrix([[0, 0], [0, -8]]), -DMatrix([[0, -8], [-8, -2]]) ] F = horzcat(Fi) F = sparse(F) print F G = -DMatrix([[-11, 0], [0, 23]]) G = sparse(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.size())]) 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)