当生成更复杂的目标函数时,Gurobi误差因子必须是常数

2024-05-19 03:20:34 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用一个相当直接的成本函数作为优化目标函数时,gurobi给出了一个答案,但是当我用math.log()函数或者甚至用i**2而不是{}使事情复杂化时,会产生一个与以下类似的错误:

GurobiError: Divisor must be a constant

TypeError: a float is required

TypeError: unsupported operand type(s) for ** or pow(): 'Var' and 'int'

我试图将math.log((m-i)/i)重新格式化为math.log(m-i)- math.log(i),这会产生float is required错误。将i*i更改为i**2会产生不受支持的错误。在

现在我的问题是:在古罗比体内,要做一个更复杂的功能是不可能的吗?或者我在别的地方犯了错误。在

这是我的模特的剪影

from gurobipy import *
import pandas as pd
import numpy as np
import time
import math

start_time = time.time()


# example NL (i, 20, 0.08, -6.7, 301)
def cost(i, j, k, l, m):
    cost = (j - l)*i + k*i*i - l*(m - i) * (math.log((m - i) / i ))
    return cost


def utility(i, j, k, l):
    utility = j + k*i + l*i*i
    return utility

"""
def cost(i, j, k, l):
    cost = j + k*i + .5*l*i*i
    return cost
"""

# assign files to use as input and as output
outputfile = 'model1nodeoutput.csv'
inputfile = 'marketclearinginput.xlsx'

# define dataframes
dfdemand = pd.read_excel(inputfile, sheetname="demand", encoding='utf8')
dfproducer = pd.read_excel(inputfile, sheetname="producer", encoding='utf8')

m = Model("1NodeMultiPeriod")

dofprod = [m.addVar(lb=3.0, ub=300, name=h) for h in dfproducer['name']]
dofdem = [m.addVar(lb=3.0, ub=300, name=h) for h in dfdemand['name']]

# Integrate new variables
m.update()

# Set objective
m.setObjective(quicksum([utility(i, j, k, l) for i, j, k, l
                        in zip(dofdem, dfdemand['c'], dfdemand['a'], dfdemand['b'])]) -
               quicksum([cost(i, j, k, l, m) for i, j, k, l, m
                        in zip(dofprod, dfproducer['c'], dfproducer['a'], dfproducer['b'], dfproducer['Pmax'])]),
               GRB.MAXIMIZE)

# Set constraints
# Set constraints for producers
for i, j, k in zip(dofprod, dfproducer['Pmin'], dfproducer['Pmax']):
    m.addConstr(i >= j)
    m.addConstr(i <= k)
# Set constraints for demand
for i, j, k in zip(dofdem, dfdemand['Pmin'], dfdemand['Pmax']):
    m.addConstr(i >= j)
    m.addConstr(i <= k)
# Build the timestamp list, pd or np unique both possible, pd faster and preserves order
# Timestamps skips the first 3 symbols (example L1T2034 becomes 2034)
timestamps = pd.unique([i.varName[3:] for i in dofprod])
# Set constraint produced >= demanded (this should be te last constraint added for shadow variables)
for h in timestamps:
    m.addConstr(quicksum([i for i in dofprod if i.varName.endswith(h)]) >=
                quicksum([i for i in dofdem if i.varName.endswith(h)]))


m.optimize()

Tags: inimportlogfortimeas错误math

热门问题