Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect sgdot network #3

Merged
merged 3 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions fastapi_app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///./nodes.db"
# nodes database
SQLALCHEMY_NODES_DATABASE_URL = "sqlite:///./nodes.db"

engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
SQLALCHEMY_NODES_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
Base = declarative_base()
167 changes: 146 additions & 21 deletions fastapi_app/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from fastapi.param_functions import Query
import models
from fastapi import FastAPI, Request, Depends, BackgroundTasks
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from database import SessionLocal, engine
from pydantic import BaseModel
from models import Nodes
import models
from models import Nodes, Links, AddNodeRequest, OptimizeGridRequest
from sqlalchemy.orm import Session
from typing import Optional
import sqlite3
from sgdot.grids import Grid
from sgdot.tools.grid_optimizer import GridOptimizer
import math


app = FastAPI()
Expand All @@ -20,20 +21,6 @@
templates = Jinja2Templates(directory="templates")


class AddNodeRequest(BaseModel):
latitude: float
longitude: float
node_type: str
fixed_type: bool


class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None


def get_db():
try:
db = SessionLocal()
Expand All @@ -57,6 +44,13 @@ async def get_nodes(request: Request, db: Session = Depends(get_db)):
return result


@app.get("/links_db_html")
async def get_links(request: Request, db: Session = Depends(get_db)):
res = db.execute("select * from links")
result = res.fetchall()
return result


def compute_links():
pass

Expand All @@ -75,17 +69,124 @@ async def add_node(add_node_request: AddNodeRequest,
db.add(nodes)
db.commit()

background_tasks.add_task(compute_links)
# background_tasks.add_task(compute_links)

return {
"code": "success",
"message": "node added to db"
}


@app.post("/clear_node_db/")
async def add_node():
@app.post("/optimize_grid/")
async def optimize_grid(optimize_grid_request: OptimizeGridRequest,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)):
res = db.execute("select * from nodes")
nodes = res.fetchall()

grid = Grid(price_meterhub=optimize_grid_request.price_meterhub,
price_household=optimize_grid_request.price_household,
price_interhub_cable_per_meter=optimize_grid_request.price_interhub_cable,
price_distribution_cable_per_meter=optimize_grid_request.price_distribution_cable)
opt = GridOptimizer(sa_runtime=20)
r = 6371000 # Radius of the earth [m]
# use latitude of the node that is the most west to set origin of x coordinates
latitude_0 = math.radians(min([node[1] for node in nodes]))
# use latitude of the node that is the most south to set origin of y coordinates
longitude_0 = math.radians(min([node[2] for node in nodes]))
for node in nodes:
latitude = math.radians(node[1])
longitude = math.radians(node[2])

x = r * (longitude - longitude_0) * math.cos(latitude_0)
y = r * (latitude - latitude_0)

longitude_recomputed = longitude_0 + x / (r * math.cos(latitude_0))
latitude_recomputed = latitude_0 + y / r

grid.add_node(label=str(node[0]),
pixel_x_axis=x,
pixel_y_axis=y,
node_type="household",
type_fixed=bool(node[4]))
number_of_hubs = opt.get_expected_hub_number_from_k_means(grid=grid)
opt.nr_optimization(grid=grid, number_of_hubs=number_of_hubs, number_of_relaxation_step=10,
save_output=False, save_opt_video=False, plot_price_evolution=False)

sqliteConnection = sqlite3.connect('nodes.db')
cursor = sqliteConnection.cursor()

# Empty links table
sql_delete_query = """DELETE from links"""
cursor.execute(sql_delete_query)
sqliteConnection.commit()

# Update nodes types in database
for index in grid.get_nodes().index:
sql_delete_query = (
f"""UPDATE nodes
SET node_type = '{grid.get_nodes().at[index, "node_type"]}'
WHERE id = {index};
""")
cursor.execute(sql_delete_query)
sqliteConnection.commit()

for index in grid.get_hubs().index:
sql_delete_query = (
f"""UPDATE nodes
SET node_type = 'meterhub'
WHERE id = {index};
""")
cursor.execute(sql_delete_query)
sqliteConnection.commit()
cursor.close()

conn = sqlite3.connect('nodes.db')
cursor = conn.cursor()
records = []
count = 1
for index, row in grid.get_links().iterrows():
x_from = grid.get_nodes().loc[row['from']]['pixel_x_axis']
y_from = grid.get_nodes().loc[row['from']]['pixel_y_axis']

x_to = grid.get_nodes().loc[row['to']]['pixel_x_axis']
y_to = grid.get_nodes().loc[row['to']]['pixel_y_axis']

long_from = math.degrees(longitude_0 + x_from / (r * math.cos(latitude_0)))

lat_from = math.degrees(latitude_0 + y_from / r)

long_to = math.degrees(longitude_0 + x_to / (r * math.cos(latitude_0)))

lat_to = math.degrees(latitude_0 + y_to / r)

cable_type = row['type']
distance = row['distance']

records.append((count,
lat_from,
long_from,
lat_to,
long_to,
cable_type,
distance))
count += 1

cursor.executemany('INSERT INTO links VALUES(?, ?, ?, ?, ?, ?, ?)', records)

# commit the changes to db
conn.commit()
# close the connection
conn.close()

return {
"code": "success",
"message": "grid optimized"
}


@ app.post("/clear_node_db/")
async def clear_nodes():
sqliteConnection = sqlite3.connect('nodes.db')
cursor = sqliteConnection.cursor()

Expand All @@ -94,6 +195,30 @@ async def add_node():
sqliteConnection.commit()
cursor.close()

sqliteConnection = sqlite3.connect('nodes.db')
cursor = sqliteConnection.cursor()

sql_delete_query = """DELETE from links"""
cursor.execute(sql_delete_query)
sqliteConnection.commit()
cursor.close()

return {
"code": "success",
"message": "nodes cleared"
}


@ app.post("/clear_link_db/")
async def clear_links():
sqliteConnection = sqlite3.connect('nodes.db')
cursor = sqliteConnection.cursor()

sql_delete_query = """DELETE from links"""
cursor.execute(sql_delete_query)
sqliteConnection.commit()
cursor.close()

return {
"code": "success",
"message": "nodes cleared"
Expand Down
40 changes: 38 additions & 2 deletions fastapi_app/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
from sqlalchemy import Boolean, Column, Integer, String, Numeric
from sqlalchemy.orm import relationship

from pydantic import BaseModel
from typing import Optional
from database import Base

# Models


class Nodes(Base):
__tablename__ = "nodes"

id = Column(Integer, primary_key=True, index=True)

latitude = Column(Numeric(10, 4))
longitude = Column(Numeric(10, 4))
node_type = Column(String)
fixed_type = Column(Boolean)


class Links(Base):
__tablename__ = "links"

id = Column(Integer, primary_key=True, index=True)
lat_from = Column(Numeric(10, 4))
long_from = Column(Numeric(10, 4))
lat_to = Column(Numeric(10, 4))
long_to = Column(Numeric(10, 4))
cable_type = Column(String)
distance = Column(Numeric(10, 4))


class AddNodeRequest(BaseModel):
latitude: float
longitude: float
node_type: str
fixed_type: bool


class OptimizeGridRequest(BaseModel):
price_meterhub: float
price_household: float
price_interhub_cable: float
price_distribution_cable: float


class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
Binary file removed fastapi_app/nodes.db
Binary file not shown.
1 change: 1 addition & 0 deletions fastapi_app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pytz==2021.1
pyzmq==22.0.2
requests==2.25.1
six==1.15.0
sgdot==1.13
SQLAlchemy==1.3.23
starlette==0.13.6
toml==0.10.2
Expand Down
4 changes: 3 additions & 1 deletion fastapi_app/run
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
source fastenv/bin/activate

source venv/bin/activate
cd fastapi_app
uvicorn main:app --reload
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fastapi_app/static/images/markers/marker-hub.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading