def main(argv=None): if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "h", ["help"]) except getopt.GetoptError, msg: raise Usage(msg) try: #inputShapeFile = argv[1] inputShapeFile = r"C:\Users\Selin\Documents\test\random100.shp" #outputDir = argv[2] outputDir = r"C:\Users\Selin\Documents\test\random100" #algorithm = argv[3] searchRadius = 6000 # in meters except IndexError: raise Error("Not enough arguments provided to script.") nodes = generateDictsFromShp(inputShapeFile, outputDir) numNodes = len(nodes) segments = generateSegments(nodes) tree = primsAlg(segments, numNodes, firstNodeID=0, excludedNodes=[], nodeDict=None) fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MST.shp")
def main(cur_file): #searchRadius = 100000 # meters # Cost parameters: MV = 25 # Cost of MV per meter LV = 10 # Cost of LV per meter TCost = 2000 # Transformer Cost distFromT = 500 # Dmax, direct distance from transformers searchRadius = 10000 # Reducing the distance to search locally searchRadius_LV = distFromT maxLVLenghtInCluster = 500 # Lmax # read shape file outputDir = cur_file[:-4] access_rate = float(sys.argv[1]) # penetration rate investment = 600000 roi_years = 5 cost_per_kwh = 0.05 demand_weight = float(sys.argv[2]) if os.path.isdir(outputDir): shutil.rmtree(outputDir) print(outputDir) logfilename = outputDir + 'modelStatus.txt' startTime = time.time() try: print("Generating Dictionaries") nodesByClusterID, clusterByNode, nodes, centers, LVCostDict, node_weights_output = generateDictsFromShp( cur_file, outputDir) connected = int(access_rate * len(nodes)) node_weights_output.to_csv(os.path.join(outputDir, 'node_weights.csv')) print("Run function starts...") # totalCost, tree, centers, nodesByClusterID, LVCostSum = run(centers, nodesByClusterID, clusterByNode, # LVCostDict, searchRadius, MV, LV, TCost, # distFromT, # maxLVLenghtInCluster, outputDir, logfilename,connected) #import ipdb; ipdb.set_trace() totalCost, tree, centers, nodesByClusterID, LVCostSum = run( centers, nodesByClusterID, clusterByNode, LVCostDict, searchRadius, searchRadius_LV, MV, LV, TCost, distFromT, investment, cost_per_kwh, roi_years, maxLVLenghtInCluster, outputDir, logfilename, connected, demand_weight, access_rate) #import ipdb; ipdb.set_trace() # import ipdb;ipdb.set_trace() fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MV.shp") statsFile1 = outputDir + os.sep + "LVCostDict.txt" statsFile2 = outputDir + os.sep + "CenterSize.txt" writeLVDictToText(statsFile1, LVCostDict) writeCenterSizeToText(statsFile2, centers) MVLength = tree.getTotalEdgeWeight() MVCost = MVLength * MV numTransformer = len(centers) try: netID = tree.getNetID([*centers.values()][0]) except: netID = 0 tree._nodesByNetID[0] = [] tree._network[netID] = [] my_lv = 0 connected_nodes = [len(nodesByClusterID[k]) for k in centers.keys()] # finding the revenue, demand = 0 for k in centers.keys(): #import ipdb; ipdb.set_trace() # selecting the nodes for that particular transformer selectedNodes = nodesByClusterID[k] # getting demands of each of them for i in selectedNodes: demand = demand + i.getDemand() revenue = demand * cost_per_kwh * 12 #import ipdb; ipdb.set_trace() connected_nodes = sum(connected_nodes) for ID in centers.keys(): nodesByNodeID = {} segments, lvCost = CMST_dfs_OLD.CMST(nodesByClusterID[ID], maxLVLenghtInCluster, centers[ID]) my_lv += lvCost for segment in segments.values(): node1 = segment.getNode1() node2 = segment.getNode2() if node1.getID() not in nodesByNodeID.keys(): nodesByNodeID[node1.getID()] = node1 if node2.getID() not in nodesByNodeID.keys(): nodesByNodeID[node2.getID()] = node2 for node in nodesByNodeID.values(): tree._netIDByNode[node] = netID tree._nodesByNetID[netID].append(node) for segment in segments.values(): tree._network[netID].append(segment) fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "FinalGrid.shp") with open(outputDir + 'modelOutput.txt', 'w') as dst: #dst.write("NumStructures:" + str(len(nodes)) + "\n") dst.write("NumConnectedStructures:" + str(connected_nodes) + "\n") #dst.write("LVLength:" + str(my_lv) + "\n") dst.write("LVPerCustomer:" + str(float(my_lv) / connected_nodes) + "\n") #dst.write("MVLength:" + str(MVLength) + "\n") dst.write("MVPerCustomer:" + str(MVLength / connected_nodes) + "\n") #dst.write("Num Transformers:" + str(numTransformer) + "\n") dst.write("Customers Per Tx:" + str(connected_nodes / float(numTransformer)) + "\n") #dst.write("Total LV Cost:" + str(my_lv * float(LV)) + "\n") #dst.write("Total MV Cost:" + str(MVCost) + "\n") transformerCost = numTransformer * TCost #dst.write("Transformer Cost:" + str(transformerCost) + "\n") total_cost = MVCost + my_lv * float(LV) + transformerCost #dst.write("Total Grid Cost:" + str(total_cost) + "\n") dst.write("GridCostPerCustomer:" + str(total_cost / connected_nodes) + "\n") # dst.write("Offgrid Cost:" + str(offgrid_cost*(len(nodes)-connected_nodes)) + "\n") runningT = time.time() - startTime #dst.write("Total Running Time:" + str(runningT) + "\n") runningT1 = time.time() - startTime #dst.write("Final Running Time:" + str(runningT1) + "\n") dst.write("Total demand:" + str(demand) + "\n") dst.write("Revenue per year:" + str(revenue) + "\n") #dst.write("NumStructures:" + str(len(nodes)) + "\n") print("NumConnectedStructures:" + str(connected_nodes) + "\n") #dst.write("LVLength:" + str(my_lv) + "\n") print("LVPerCustomer:" + str(float(my_lv) / connected_nodes) + "\n") #dst.write("MVLength:" + str(MVLength) + "\n") print("MVPerCustomer:" + str(MVLength / connected_nodes) + "\n") #dst.write("Num Transformers:" + str(numTransformer) + "\n") print("Customers Per Tx:" + str(connected_nodes / float(numTransformer)) + "\n") #dst.write("Total LV Cost:" + str(my_lv * float(LV)) + "\n") #dst.write("Total MV Cost:" + str(MVCost) + "\n") transformerCost = numTransformer * TCost #dst.write("Transformer Cost:" + str(transformerCost) + "\n") total_cost = MVCost + my_lv * float(LV) + transformerCost #dst.write("Total Grid Cost:" + str(total_cost) + "\n") print("GridCostPerCustomer:" + str(total_cost / connected_nodes) + "\n") # dst.write("Offgrid Cost:" + str(offgrid_cost*(len(nodes)-connected_nodes)) + "\n") runningT = time.time() - startTime #dst.write("Total Running Time:" + str(runningT) + "\n") runningT1 = time.time() - startTime #dst.write("Final Running Time:" + str(runningT1) + "\n") print("Total demand:" + str(demand) + "\n") print("Revenue per year:" + str(revenue) + "\n") # with open(outputDir + 'modelOutput.txt', 'w') as dst: # dst.write("NumStructures:" + str(len(nodes)) + "\n") # dst.write("LVLength:" + str(my_lv) + "\n") # dst.write("LVPerCustomer:" + str(float(my_lv) / len(nodes)) + "\n") # dst.write("MVLength:" + str(MVLength) + "\n") # dst.write("MVPerCustomer:" + str(MVLength / len(nodes)) + "\n") # dst.write("Num Transformers:" + str(numTransformer) + "\n") # dst.write("Customers Per Tx:" + str(len(nodes) / float(numTransformer)) + "\n") # dst.write("Total LV Cost:" + str(my_lv * float(LV)) + "\n") # dst.write("Total MV Cost:" + str(MVCost) + "\n") # transformerCost = numTransformer * TCost # dst.write("Transformer Cost:" + str(transformerCost) + "\n") # total_cost = MVCost + my_lv * float(LV) + transformerCost # dst.write("Total Cost:" + str(total_cost) + "\n") # runningT = time.time() - startTime # dst.write("Total Running Time:" + str(runningT) + "\n") # # with open(outputDir+'modelOutput.txt', 'a') as dst: # runningT1 = time.time() - startTime # dst.write("Final Running Time:" + str(runningT1)) except Exception as error_with_grid: print("Error with file:", error_with_grid)
def main(cur_file): searchRadius = 1000 # meters # Cost parameters: MV = 25 # Cost of MV per meter LV = 10 # Cost of LV per meter TCost = 2000 / 8800.0 # Transformer Cost per watt distFromT = 500 # Dmax, direct distance from transformers maxLVLenghtInCluster = 500 # Lmax txCap = 8.8 # assuming 11kVA TX at a pf 0.8 to give 8.8 kW # investment_per_hh = 300 # cost per HH investment = 600000 roi_years = 5 cost_per_kwh = 0.05 # although it is 0.20 a kwh recovery only about 5 cents fraction_covered_by_subsidy = 0 fraction_to_be_recovered = 1 - fraction_covered_by_subsidy offgrid_cost = 500 ## assume offgrid cost # read shape file outputDir = cur_file[:-4] if os.path.isdir(outputDir): shutil.rmtree(outputDir) print(outputDir) logfilename = outputDir + 'modelStatus.txt' startTime = time.time() try: print("Generating Dictionaries") nodesByClusterID, clusterByNode, nodes, centers, LVCostDict, node_weights_output = generateDictsFromShp( cur_file, outputDir) node_weights_output.to_csv(os.path.join(outputDir, 'node_weights.csv')) print("Run function starts...") totalCost, tree, centers, connected_nodesByClusterID, LVCostSum, nodesByClusterID = run( centers, nodesByClusterID, clusterByNode, LVCostDict, searchRadius, MV, LV, TCost, distFromT, investment, cost_per_kwh, roi_years, fraction_to_be_recovered, maxLVLenghtInCluster, outputDir, logfilename) fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MV.shp") # import ipdb;ipdb.set_trace() statsFile1 = outputDir + os.sep + "LVCostDict.txt" statsFile2 = outputDir + os.sep + "CenterSize.txt" writeLVDictToText(statsFile1, LVCostDict) writeCenterSizeToText(statsFile2, centers) MVLength = tree.getTotalEdgeWeight() MVCost = MVLength * MV numTransformer = len(centers) try: netID = tree.getNetID([*centers.values()][0]) except: netID = 0 tree._nodesByNetID[0] = [] tree._network[netID] = [] my_lv = 0 connected_nodes = [len(nodesByClusterID[k]) for k in centers.keys()] connected_nodes = sum(connected_nodes) for ID in centers.keys(): nodesByNodeID = {} segments, lvCost = CMST_dfs_OLD.CMST(nodesByClusterID[ID], maxLVLenghtInCluster, centers[ID]) my_lv += lvCost for segment in segments.values(): node1 = segment.getNode1() node2 = segment.getNode2() if node1.getID() not in nodesByNodeID.keys(): nodesByNodeID[node1.getID()] = node1 if node2.getID() not in nodesByNodeID.keys(): nodesByNodeID[node2.getID()] = node2 for node in nodesByNodeID.values(): tree._netIDByNode[node] = netID tree._nodesByNetID[netID].append(node) for segment in segments.values(): tree._network[netID].append(segment) fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "FinalGrid.shp") with open(outputDir + 'modelOutput.txt', 'w') as dst: dst.write("NumStructures:" + str(len(nodes)) + "\n") dst.write("NumConnectedStructures:" + str(connected_nodes) + "\n") dst.write("LVLength:" + str(my_lv) + "\n") dst.write("LVPerCustomer:" + str(float(my_lv) / connected_nodes) + "\n") dst.write("MVLength:" + str(MVLength) + "\n") dst.write("MVPerCustomer:" + str(MVLength / connected_nodes) + "\n") dst.write("Num Transformers:" + str(numTransformer) + "\n") dst.write("Customers Per Tx:" + str(connected_nodes / float(numTransformer)) + "\n") dst.write("Total LV Cost:" + str(my_lv * float(LV)) + "\n") dst.write("Total MV Cost:" + str(MVCost) + "\n") transformerCost = numTransformer * TCost dst.write("Transformer Cost:" + str(transformerCost) + "\n") total_cost = MVCost + my_lv * float(LV) + transformerCost dst.write("Total Grid Cost:" + str(total_cost) + "\n") dst.write("Offgrid Cost:" + str(offgrid_cost * (len(nodes) - connected_nodes)) + "\n") runningT = time.time() - startTime dst.write("Total Running Time:" + str(runningT) + "\n") runningT1 = time.time() - startTime dst.write("Final Running Time:" + str(runningT1)) except Exception as error_with_grid: print("Error with file:", error_with_grid)
def main(cur_file): searchRadius = 100000 # meters # Cost parameters: MV = 25 # Cost of MV per meter LV = 10 # Cost of LV per meter TCost = 2000 # Transformer Cost distFromT = 500 # Dmax, direct distance from transformers maxLVLenghtInCluster = 500 # Lmax # read shape file outputDir = cur_file[:-4] if os.path.isdir(outputDir): shutil.rmtree(outputDir) print(outputDir) logfilename = outputDir + 'modelStatus.txt' startTime = time.time() try: print("Generating Dictionaries") nodesByClusterID, clusterByNode, nodes, centers, LVCostDict, node_weights_output = generateDictsFromShp( cur_file, outputDir) node_weights_output.to_csv(os.path.join(outputDir, 'node_weights.csv')) print("Run function starts...") totalCost, tree, centers, nodesByClusterID, LVCostSum = run( centers, nodesByClusterID, clusterByNode, LVCostDict, searchRadius, MV, LV, TCost, distFromT, maxLVLenghtInCluster, outputDir, logfilename) fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MV.shp") statsFile1 = outputDir + os.sep + "LVCostDict.txt" statsFile2 = outputDir + os.sep + "CenterSize.txt" writeLVDictToText(statsFile1, LVCostDict) writeCenterSizeToText(statsFile2, centers) MVLength = tree.getTotalEdgeWeight() MVCost = MVLength * MV numTransformer = len(centers) try: netID = tree.getNetID(centers.values()[0]) except: netID = 0 tree._nodesByNetID[0] = [] tree._network[netID] = [] my_lv = 0 for ID in centers.keys(): nodesByNodeID = {} segments, lvCost = CMST_dfs_OLD.CMST(nodesByClusterID[ID], maxLVLenghtInCluster, centers[ID]) my_lv += lvCost for segment in segments.values(): node1 = segment.getNode1() node2 = segment.getNode2() if node1.getID() not in nodesByNodeID.keys(): nodesByNodeID[node1.getID()] = node1 if node2.getID() not in nodesByNodeID.keys(): nodesByNodeID[node2.getID()] = node2 for node in nodesByNodeID.values(): tree._netIDByNode[node] = netID tree._nodesByNetID[netID].append(node) for segment in segments.values(): tree._network[netID].append(segment) fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "FinalGrid.shp") with open(outputDir + 'modelOutput.txt', 'w') as dst: dst.write("NumStructures:" + str(len(nodes)) + "\n") dst.write("LVLength:" + str(my_lv) + "\n") dst.write("LVPerCustomer:" + str(float(my_lv) / len(nodes)) + "\n") dst.write("MVLength:" + str(MVLength) + "\n") dst.write("MVPerCustomer:" + str(MVLength / len(nodes)) + "\n") dst.write("Num Transformers:" + str(numTransformer) + "\n") dst.write("Customers Per Tx:" + str(len(nodes) / float(numTransformer)) + "\n") dst.write("Total LV Cost:" + str(my_lv * float(LV)) + "\n") dst.write("Total MV Cost:" + str(MVCost) + "\n") transformerCost = numTransformer * TCost dst.write("Transformer Cost:" + str(transformerCost) + "\n") total_cost = MVCost + my_lv * float(LV) + transformerCost dst.write("Total Cost:" + str(total_cost) + "\n") runningT = time.time() - startTime dst.write("Total Running Time:" + str(runningT) + "\n") # with open(outputDir+'modelOutput.txt', 'a') as dst: runningT1 = time.time() - startTime dst.write("Final Running Time:" + str(runningT1)) except Exception as error_with_grid: print("Error with file:", error_with_grid)