Skip to content

Commit

Permalink
Update cvx_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dchassin committed Nov 9, 2024
1 parent 85bef21 commit bff43d0
Show file tree
Hide file tree
Showing 4 changed files with 1,053 additions and 72 deletions.
123 changes: 116 additions & 7 deletions module/optimize/autotest/cvx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,130 @@
import numpy as np
import scipy as sp

class Model:

def __init__(self,source=None):
assert(source==gld or isinstance(source,dict))

class Property:

def __init__(self,model,*args):
assert isinstance(model,dict),"model type invalid"
assert model["application"]=="gridlabd","not a gridlabd model"
self.model = model
self.obj = args[0]
self.name = args[1]

def get_object(self,*args):

return self.obj

def set_object(self,*args):

self.obj = args[0]

def get_name(self,*args):

return self.name

def get_initial(self,*args):

try:
return model["classes"][model["objects"]["class"]]["initial"]
except:
return None

def get_value(self,*args):

return model["objects"][self.obj][self.name]

def set_value(self,*args):

model["objects"][self.obj][self.name] = args[0]

def rlock(self,*args):

return None

def wlock(self,*args):

return None

def unlock(self,*args):

return None

def convert_unit(self,*args):

raise RuntimeError("cannot convert units in static models")


class GldModel(Model):

def __init__(self):
self.cache = {}
super().__init__(gld)

def objects(self):
return gld.objects

def classes(self):
return gld.classes

def globals(self):
return gld.globals

def property(self,*args):
key = "#".join(args)
try:
return self.cache[key]
except:
prop = gld.property(*args)
self.cache[key] = prop
return prop

class JsonModel(Model):

def __init__(self,jsonfile):
import json
self.json = json.load(open(jsonfile,"r"))
super().__init__(self.json)

def objects(self):
return self.json["objects"]

def classes(self):
return self.json["classes"]

def globals(self):
return self.json["globals"]

def property(self,*args):
return Property(model,*args)


class Network:
"""Network accessor
"""Network model accessor
"""
def __init__(self):

self.update()

def update(self,auto=None):

model = GldModel()
now = model.property("clock").get_value()
if auto is None:

now = gld.property("clock").get_value()

if not hasattr(self,'last') or now > self.last:

update = True
self.last = now

else:

update = False

elif isinstance(auto,bool):

update = auto
Expand All @@ -29,16 +137,17 @@ def update(self,auto=None):

if not hasattr(self,'nodes') or not hasattr(self,'lines') or update:

self.last = now
self.lines = []
self.nodes = {}
self.Y = []
for name,data in gld.objects.items():
for name,data in model.objects().items():

oclass = gld.classes[data["class"]]
oclass = model.classes()[data["class"]]
if "from" in oclass and "to" in oclass:
Z = gld.property(name,"R").get_value()
Z = model.property(name,"R").get_value()
if Z > 0:
fobj,tobj = gld.property(name,"from").get_value(),gld.property(name,"to").get_value()
fobj,tobj = model.property(name,"from").get_value(),model.property(name,"to").get_value()
for obj in [fobj,tobj]:
if not obj in self.nodes:
self.nodes[obj] = len(self.nodes)
Expand Down
2 changes: 1 addition & 1 deletion module/optimize/autotest/test_cvx.glm
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ x = Variable(n)
object cvx
{
name "problem-x";
event INIT;
event PRECOMMIT;
presolve "G = utils.Network(); print(G.W.todense(),G.L.todense(),file=sys.stderr)";
//presolve "print(gld.objects,gld.globals,gld.classes,gld.property('problem-y','value'),gld.property('clock'),file=sys.stderr)";
//presolve "m=20; n=15; np.random.seed(1); A=np.random.rand(m,n); b=np.random.rand(m); x=Variable(n)";
Expand Down
Loading

0 comments on commit bff43d0

Please sign in to comment.