Skip to content

Commit

Permalink
Merge pull request #357 from convexopt/py3
Browse files Browse the repository at this point in the history
get to a low level of python3 compatibility
  • Loading branch information
bqpd committed Aug 15, 2015
2 parents 107499b + 68eddfc commit eb29a3a
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gpkit/_mosek/cli_expopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def imize(c, A, p_idxs, *args, **kwargs):
for x in t_j_Atj])

log = check_output(["mskexpopt", filename])
for logline in log.split("\n"):
print logline
for logline in log.split(b"\n"):
print(logline)

with open(filename+".sol") as f:
assert_line(f, "PROBLEM STATUS : PRIMAL_AND_DUAL_FEASIBLE\n")
Expand Down
4 changes: 2 additions & 2 deletions gpkit/geometric_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, cost, constraints, verbosity=1):
p_idxs = []
m_idxs = []
for i, p_len in enumerate(self.k):
m_idxs.append(range(len(p_idxs), len(p_idxs) + p_len))
m_idxs.append(list(range(len(p_idxs), len(p_idxs) + p_len)))
p_idxs += [i]*p_len
self.p_idxs = np.array(p_idxs)
self.m_idxs = m_idxs
Expand Down Expand Up @@ -215,7 +215,7 @@ def _almost_equal(num1, num2):
"local almost equal test"
return num1 == num2 or abs((num1 - num2) / (num1 + num2)) < tol
cost = sol["cost"]
primal_sol = np.log(sol["variables"].values())
primal_sol = np.log(list(sol["variables"].values()))
nu = sol["sensitivities"]["monomials"]
la = sol["sensitivities"]["posynomials"]
A = self.A.tocsr()
Expand Down
10 changes: 5 additions & 5 deletions gpkit/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def _solve(self, programType, solver, verbosity, skipfailures,

if sweep:
if len(sweep) == 1:
sweep_grids = np.array(sweep.values())
sweep_grids = np.array(list(sweep.values()))
else:
sweep_grids = np.meshgrid(*list(sweep.values()))

Expand Down Expand Up @@ -351,7 +351,7 @@ def solve_pass(i):
solution.toarray()
self.solution = solution # NOTE: SIDE EFFECTS
if verbosity > 0:
print solution.table()
print(solution.table())
return solution

# TODO: add sweepgp(index)?
Expand Down Expand Up @@ -433,7 +433,7 @@ def feasibility(self,
max_gp = self.gp().feasibility_search("max")
infeasibility = max_gp.solve(verbosity=verbosity-1)["cost"]
if verbosity > 0:
print " overall : %.2f" % infeasibility
print(" overall : %.2f" % infeasibility)
feasibilities["overall"] = infeasibility

if "constraints" in search:
Expand All @@ -442,7 +442,7 @@ def feasibility(self,
result = prod_gp.solve(verbosity=verbosity-1)
con_infeas = [result["variables"][sv] for sv in slackvars]
if verbosity > 0:
print " constraints : %s" % con_infeas
print(" constraints : %s" % con_infeas)
feasibilities["constraints"] = con_infeas

constants = get_constants(unsubbed, allsubs)
Expand Down Expand Up @@ -479,7 +479,7 @@ def feasibility(self,
var_infeas = {addvalue[constvarkeys[i]]: feasible_constvalues[i]
for i in np.where(changed_vals)}
if verbosity > 0:
print " constants : %s" % var_infeas
print(" constants : %s" % var_infeas)
feasibilities["constants"] = var_infeas

if verbosity > 0:
Expand Down
7 changes: 4 additions & 3 deletions gpkit/nomial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ def __init__(self, exps=None, cs=None, nomials=None, simplify=True):
self.varlocs, self.varstrs = locate_vars(self.exps)
self.values = {vk: vk.descr["value"] for vk in self.varlocs
if "value" in vk.descr}
# confirm lengths before calling zip
assert len(self.exps) == len(self.cs)
self._hash = hash(tuple(zip(self.exps, tuple(self.cs))))

def __hash__(self):
if not hasattr(self, "_hash"):
# confirm lengths before calling zip
assert len(self.exps) == len(self.cs)
self._hash = hash(tuple(zip(self.exps, tuple(self.cs))))
return self._hash

def __repr__(self):
Expand Down
2 changes: 2 additions & 0 deletions gpkit/nomials.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def __init__(self, exps=None, cs=1, require_positive=True, simplify=True,
self.exp = self.exps[0]
self.c = self.cs[0]

__hash__ = NomialData.__hash__

@property
def value(self):
"""Self, with values substituted for variables that have values
Expand Down
3 changes: 2 additions & 1 deletion gpkit/solution_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def results_table(data, title, minval=0, printunits=True, fixedcols=True,
if not fixedcols:
maxlens = [maxlens[0], 0, 0, 0]
dirs = ['>', '<', '<', '<']
assert len(dirs) == len(maxlens) # check lengths before using zip
# check lengths before using zip
assert len(list(dirs)) == len(list(maxlens))
fmts = ['{0:%s%s}' % (direc, L) for direc, L in zip(dirs, maxlens)]
lines = [[fmt.format(s) for fmt, s in zip(fmts, line)] for line in lines]
lines = [title] + ["-"*len(title)] + [''.join(l) for l in lines] + [""]
Expand Down
2 changes: 1 addition & 1 deletion gpkit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def te_exp_minus1(posy, nterm):
raise ValueError("Unexpected number of terms, nterm=%s" % nterm)
res = 0
factorial_denom = 1
for i in xrange(1, nterm + 1):
for i in range(1, nterm + 1):
factorial_denom *= i
res += posy**i / factorial_denom
return res
Expand Down
3 changes: 3 additions & 0 deletions gpkit/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .varkey import VarKey
from .nomials import Monomial
from .nomial_data import NomialData
from .posyarray import PosyArray
from .small_classes import Strings, Numbers, Quantity
from .small_scripts import is_sweepvar
Expand Down Expand Up @@ -46,6 +47,8 @@ def __init__(self, *args, **descr):
Monomial.__init__(self, **descr)
self.__class__ = Variable

__hash__ = NomialData.__hash__

@property
def varkey(self):
"""Get the VarKey associated with this Variable"""
Expand Down

0 comments on commit eb29a3a

Please sign in to comment.