Skip to content

Commit

Permalink
LaTeX output
Browse files Browse the repository at this point in the history
  • Loading branch information
mghasemi committed Jan 6, 2017
1 parent f8c2e24 commit 7ed1e5f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
16 changes: 12 additions & 4 deletions Irene/relaxations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SDPRelaxations(base):
def __init__(self, gens, relations=[]):
assert type(gens) is list, self.GensError
assert type(gens) is list, self.RelsError
from sympy import Function, Symbol, QQ, RR, groebner
from sympy import Function, Symbol, QQ, RR, groebner, Poly
from sympy.core.relational import Equality, GreaterThan, LessThan, StrictGreaterThan, StrictLessThan
import multiprocessing
try:
Expand Down Expand Up @@ -96,6 +96,7 @@ def __init__(self, gens, relations=[]):
self.AuxSyms.append(t_sym)
else:
raise TypeError(self.GensError)
self.RedObjective = Poly(0, *self.AuxSyms)
# check the relations
# TBI
for r in relations:
Expand Down Expand Up @@ -670,6 +671,7 @@ def __str__(self):

def __latex__(self):
r"""
Generates LaTeX code of the optimization problem.
"""
from sympy import latex
latexcode = "\\left\\lbrace\n"
Expand Down Expand Up @@ -782,8 +784,8 @@ def SetScipySolver(self, solver):
self.ScipySolver = solver.lower()

def Pivot(self, arr):
"""
Get the leading term of arr > tau
r"""
Get the leading term of each column.
"""
if arr.ndim == 1:
idxs, = arr.nonzero()
Expand All @@ -802,7 +804,7 @@ def Pivot(self, arr):
return 0, arr[0]

def StblRedEch(self, A):
"""
r"""
Compute the stabilized row reduced echelon form.
"""
from numpy import array, sqrt, zeros
Expand Down Expand Up @@ -1033,6 +1035,9 @@ def ExtractSolution(self, mthd='LH', card=0):
raise Exception("Unsupported solver.")

def __latex__(self):
r"""
Generates LaTeX code for the moment matrix.
"""
a = self.MomentMatrix
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{bmatrix}']
Expand Down Expand Up @@ -1154,6 +1159,9 @@ def __str__(self):
return strng

def __latex__(self, external=False):
r"""
Generates LaTeX code for the moment term.
"""
from sympy import latex
symbs = {'lt': '<', 'le': '\\leq', 'gt': '>', 'ge': '\\geq', 'eq': '='}
latexcode = "\\textrm{Moment of }"
Expand Down
13 changes: 11 additions & 2 deletions Irene/sdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def Option(self, param, val):
r"""
Sets the `param` option of the solver to `val` if the solver accepts
such an option. The following options are supported by solvers:
+ ``CVXOPT``:
+ ``show_progress``: ``True`` or ``False``, turns the output to the screen on or off (default: ``True``);
Expand Down Expand Up @@ -418,7 +418,7 @@ def CvxOpt(self):
start2 = clock()

try:
#if True:
# if True:
sol = solvers.sdp(acvxopt, Gs=Acvxopt, hs=Ccvxopt,
solver=self.solver.lower())
elapsed1 = (time() - start1)
Expand Down Expand Up @@ -484,3 +484,12 @@ def solve(self):
self.sdpa()
elif self.solver == 'CSDP':
self.csdp()

def __str__(self):
out_text = "Semidefinite program with\n"
out_text += " # variables:" + str(len(self.C)) + "\n"
out_text += " # constraints:" + str(len(self.A)) + "\n"
out_text += " with solver:" + self.solver

def __latex__(self):
return "SDP(%d, %d, %s)"%(len(self.C), len(self.A), self.solver)
Binary file modified doc/Irene.pdf
Binary file not shown.
3 changes: 1 addition & 2 deletions doc/optim.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ The following code does the job::
# initiate the Relaxation object
Rlx = SDPRelaxations([x, y])
# set the objective
Rlx.SetObjective(0)
# add support constraints
Rlx.AddConstraint(1 - x**2 >= 0)
Rlx.AddConstraint(1 - y**2 >= 0)
Expand All @@ -285,7 +284,7 @@ The following code does the job::
# solve the SDP
Rlx.Minimize()
# output
Rlx.Solution.ExtractSolution('lh', 3)
Rlx.Solution.ExtractSolution('lh', 2)
print Rlx.Solution

and the result is::
Expand Down
6 changes: 4 additions & 2 deletions doc/rev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
Revision History
=============================

**Version 1.1.5 ()**
**Version 1.2.0 (Jan 5, 2017)**

- LaTeX representation of Irene's objects.
- ``SDRelaxSol`` can be called as an iterable.
- Default objective function is set to a ``sympy`` object for truncated moment problems.

**Version 1.1.0 (December 25, 2016 - Merry Christmas)**
**Version 1.1.0 (Dec 25, 2016 - Merry Christmas)**

- Extracting minimizers via ``SDRelaxSol.ExtractSolution()`` and help of ``scipy``,
- Extracting minimizers implementing Lasserre-Henrion algorithm,
Expand Down
2 changes: 1 addition & 1 deletion doc/sdp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ and
This simplifies the :math:`k` constraints of the primal form in to one constraint
:math:`\sum_{i=1}^m A_i x_i - C \succeq 0` and the objective and constraints of the
dual form as :math:`tr(C\times Y)` and :math:`tr(A_i\times Z_i) = b_i` for :math:`i=1,dots,m`.
dual form as :math:`tr(C\times Y)` and :math:`tr(A_i\times Z_i) = b_i` for :math:`i=1,\dots,m`.


The ``sdp`` class
Expand Down
4 changes: 2 additions & 2 deletions doc/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ To Do
Based on the current implementation, the followings seems to be implemented/modified:

+ Reduce dependency on SymPy.
+ Keep track of original expressions before reduction.
+ Include sdp solvers installation (subject to copyright limitations).
+ Error handling for CSDP and SDPA failure.

Expand All @@ -17,4 +16,5 @@ The following to-dos were implemented:
+ Extract solutions (at least for polynomials)- in v.1.1.0.
+ SOS decomposition- in v.1.1.0.
+ Write a ``__str__`` method for ``SDPRelaxations`` printing- in v.1.1.0.
+ Write a LaTeX method.
+ Write a LaTeX method- in v.1.2.0.
+ Keep track of original expressions before reduction- in v.1.2.0.

0 comments on commit 7ed1e5f

Please sign in to comment.