'How can I convert expression and save to excel?
I'm trying to extract my data into excel, but I'm having a problem.
in this part
sheet.cell(row=i+2, column=2).value = Power_sell[0]
I am trying to extract the result of an expression into Excel, but the following error occurs.
raise ValueError("Cannot convert {0!r} to Excel".format(value)) ValueError: Cannot convert Expression(AFFINE, UNKNOWN, (1,)) to Excel
If print above array -> [[59075.2439795]], why double [ ]
and, this is my whole code. Thanks to read
import pandas as pd
import cvxpy as cp
import numpy as np
import openpyxl
Demand_Excel = pd.read_excel('Input/Demands.xlsx',index_col=0)
data = pd.DataFrame(Demand_Excel)
wb = openpyxl.Workbook()
sheet = wb.active
sheet.cell(row=1, column=1).value = "총 수익"
sheet.cell(row=1, column=2).value = "그리드 판매 수익"
sheet.cell(row=1, column=3).value = "연료 가격"
sheet.cell(row=1, column=4).value = "건물 전기 절약값"
sheet.cell(row=1, column=5).value = "전기차 전기 절약값"
sheet.cell(row=1, column=6).value = "수소차 탱크 절약값"
sheet.cell(row=1, column=7).value = "FC1 그리드 판매량"
sheet.cell(row=1, column=8).value = "FC1 건물 사용량"
sheet.cell(row=1, column=9).value = "FC1 전기차 사용량"
sheet.cell(row=1, column=10).value = "FC2 발전량"
sheet.cell(row=1, column=11).value = "CHP 발전량"
sheet.cell(row=1, column=12).value = "PV 그리드 판매량"
sheet.cell(row=1, column=13).value = "PV 건물 사용량"
sheet.cell(row=1, column=14).value = "PV 전기차 사용량"
for i in range(0,23): # 시간 조절 (0,8759)
OeFC1g = cp.Variable()
OeFC1b = cp.Variable()
OeFC1e = cp.Variable()
OeFC2 = cp.Variable()
OeCHP = cp.Variable()
OePVg = cp.Variable()
OePVb = cp.Variable()
OePVe = cp.Variable()
BD = np.array(data.iloc[[i],[0]])
EV = np.array(data.iloc[[i],[1]])
HD = np.array(data.iloc[[i],[2]])
PFC1 = 9.33863636
PFC2 = 12.94857
PCHP = 14.22143
hFC1 = 0.50818182
hFC2 = 0.393143
hCHP = 2.297429
HFC2 = 0.025714
PGAS = 19.3870
PP = np.array(data.iloc[[i],[3]])
heat=396.76
Hprice=8000
constraints = [
0<=OeFC1g,
OeFC1g<=440,
0<=OeFC1b,
OeFC1b<=440,
0<=OeFC1e,
OeFC1e<=440,
OeFC1g + OeFC1b + OeFC1e <= 440,
0<=OePVg,
OePVg<=50,
0<=OePVb,
OePVb<=50,
0<=OePVe,
OePVe<=50,
OePVg + OePVb + OePVe == 50,
0<=OeFC2,
OeFC2<=350,
0<=OeCHP,
OeCHP<=140,
OeFC1b + OePVb == BD,
OeFC1e + OePVe == EV
]
# Korean Money Unit Won
heat_profit = ((OeFC1e + OeFC1g + OeFC1b) * hFC1 + OeFC2 * hFC2 +OeCHP * hCHP) * heat
Power_sell = PP * (OeFC2 + OeCHP + OeFC1g + OePVg )
Fuel_Price = ((OeFC1e + OeFC1g + OeFC1b) * PFC1 + OeFC2 * PFC2 +OeCHP * PCHP) * PGAS
Building_profit = BD * 100 + 6160 # 100 is buying price for electric power, other is base price
EV_profit = EV*100 + 2390 # 100 is buying price for electric power, other is base price
H2_profit = Hprice * ( OeFC2 * HFC2 - HD) # We have H2 tanks
obj = cp.Maximize( Power_sell - Fuel_Price + EV_profit + H2_profit + Building_profit + heat_profit )
prob = cp.Problem(obj, constraints)
prob.solve()
sheet = wb.active
sheet.cell(row=i+2, column=1).value = obj.value
sheet.cell(row=i+2, column=2).value = Power_sell[0]
sheet.cell(row=i+2, column=3).value = Fuel_Price[0]
sheet.cell(row=i+2, column=4).value = Building_profit[0]
sheet.cell(row=i+2, column=5).value = EV_profit[0]
sheet.cell(row=i+2, column=6).value = H2_profit[0]
sheet.cell(row=i+2, column=7).value = OeFC1g[0]
sheet.cell(row=i+2, column=9).value = OeFC1e[0]
sheet.cell(row=i+2, column=10).value = OeFC2[0]
sheet.cell(row=i+2, column=11).value = OeCHP[0]
sheet.cell(row=i+2, column=12).value = OePVg[0]
sheet.cell(row=i+2, column=13).value = OePVb[0]
sheet.cell(row=i+2, column=14).value = OePVe[0]
# print("상태:", prob.status)
# print("최적값:", obj.value)#, heating.value, H2.value)
# print("FC1 발전", OeFC1g.value, OeFC1e.value, OeFC1b.value, OeFC1g.value+ OeFC1e.value+ OeFC1b.value)
# print("FC2 & CHP 발전", OeFC2.value, OeCHP.value)
# print("PV 발전", OePVg.value, OePVe.value, OePVb.value, OePVg.value+ OePVe.value+ OePVb.value)
# print("수소:", H2.value)
wb.save('complete_test.xlsx')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
