Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
gidden committed Aug 10, 2017
1 parent 46a011a commit fa11054
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ build
dist
*.egg-info
.cache
.*
.*
tmp
46 changes: 41 additions & 5 deletions salamanca/models/calibrate_ineq.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def position_rule(m, idx):
return m.t[idx - 1] <= m.t[idx]


def diff_rule(m, idx):
def diff_hi_rule(m, idx):
r"""|diff_hi|
.. |diff_hi| replace:: :math:`t_r - t_{r-1} \geq 0.8 (t^*_r - t^*_{r-1}) \forall r \in {r_2 \ldots r_N}`
Expand All @@ -21,7 +21,7 @@ def diff_rule(m, idx):
return m.t[idx] - m.t[idx - 1] >= 0.8 * (m.data['t'][idx] - m.data['t'][idx - 1])


def diff_rule2(m, idx):
def diff_lo_rule(m, idx):
if idx == 0:
return mo.Constraint.Skip
return m.t[idx] - m.t[idx - 1] <= 1.2 * (m.data['t'][idx] - m.data['t'][idx - 1])
Expand All @@ -32,12 +32,32 @@ def theil_sum_rule(m):
m.data['T_w'] * m.data['G']


def obj_rule(m):
def min_diff_obj(m):
return sum((m.t[idx] - m.data['t'][idx]) ** 2
for idx in m.idxs)


class Model(object):
"""Base class for Inequality Calibration Models"""

def __init__(self, data):
self.data = data

def construct(self):
raise NotImplementedError()

def solve(self):
m = self.model
solver = mo.SolverFactory('ipopt')
result = solver.solve(m) # , tee=True)
result.write()
m.solutions.load_from(result)
t = pd.Series(m.t.get_values().values(),
index=m.data['orig_idx'], name='thiel')
return t


class Model1(Model):
"""
Comprised of
Expand All @@ -46,5 +66,21 @@ class Model(object):
"""

def __init__(self):
pass
def construct(self):
self.model = m = mo.ConcreteModel()
# Model Data
m.data = data
# Sets
m.idxs = mo.Set(initialize=m.data['idxs'])
# Variables
m.t = mo.Var(m.idxs)
# Constraints
m.position = mo.Constraint(m.idxs, rule=position_rule,
doc='ordering between provinces must be maintained')
m.diff = mo.Constraint(m.idxs, rule=diff_rule,
doc='difference between values should be about the same')
m.diff2 = mo.Constraint(m.idxs, rule=diff_rule2,
doc='difference between values should be about the same')
m.theil_sum = mo.Constraint(rule=theil_sum_rule, doc='factor ordering')
m.obj = mo.Objective(rule=obj_rule, sense=mo.minimize)
return self

0 comments on commit fa11054

Please sign in to comment.