简体   繁体   English

Python PULP数组乘法约束

[英]Python PULP Array multiplication constraint

I am trying to run an optimization problem, which i can't seem to solve. 我正在尝试运行一个优化问题,但似乎无法解决。 I am a rookie in Python. 我是Python的新秀。

I have a dataframe with 8760 numbers ( 0 - 1). 我有一个8760数字(0-1)的数据框。 I need to multiply each row in this array by a factor and then sum this array. 我需要将此数组中的每一行乘以一个因子,然后将该数组求和。 This sum of this should equal x, 50,000 for example. 这个总数应等于x,例如50,000。 A sample of my code is. 我的代码示例是。

pp.prob = pp.LpProblem("P2g", pp.LpMinimize)
SolarCap = pp.LpVariable("SolarCap", lowBound=0)

pp.prob +=  SolarCap * 5

for i,j in enumerate(SolverWS['Solar']):
    x = j * SolarCap
    x = x + x 
    pp.prob += x == P2gprod

status = pp.prob.solve()

If I understood correctly you need to multiply a pandas dataframe consisting of n entries by the same factor per each row and then sum the resulting dataframe. 如果我理解正确,则需要将由n个条目组成的熊猫数据帧乘以每行相同的因子,然后将结果数据帧求和。

You could use the mul() method of Pandas Dataframes (documentation here ) 您可以使用Pandas Dataframes的mul()方法( 此处的文档)

As for summing the elements, all you have to do is call sum() on the dataframe .values property 至于元素的求和,您要做的就是在dataframe .values属性上调用sum()

Here's a simple example 这是一个简单的例子

import numpy as np
import pandas as pd
# Create a random dataframe of 10 columns and 876 rows with random float values between 0 and 1
df = pd.DataFrame(np.random.uniform(0,1,size=(876, 10)))
print(df)
print(df.values.sum())
other = 10
# See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.multiply.html
df1 = df.mul(other)
# Alternatively, for just multiplying the whole dataframe
#df1 = df * 10
print(df1)
print(df1.values.sum())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM