简体   繁体   中英

How to set cvxpy n-dim variable first value?

I'm a beginner at python and I try to use cvxpy library for my optimization project. I try to change the first value of my n dimensional variable But I get an AttributeError

import cvxpy as cp
S = cp.Variable(100)
S[0].value=320000

output:AttributeError: can't set attribute

It works for 1-dim variable

import cvxpy as cp
S = cp.Variable()
S.value=320000

Thanks in advance

The 'Variable' object does not support item assignment. You may enforce your requirement as a constraint:

import cvxpy as cp
S = cp.Variable(100) # Define your variables

objective = ... # Define your objective function

constraints = [] # Create an array of constraints
constraints.append(S[0]==320000) # Make your requirement a constraint

# Add more constraints

prob = Problem(objective, constraints) # Define your optimization problem
prob.solve(verbose=True) # Solve the problem

print(S.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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