Hi everyone,
I have a function with an input of 6 values (6 periods of power) and only one output, the price. I would like to optimize the output value (minimize it) changing the input, but I have found no information about how to optimize with a vector as an imput instead of an scalar. I already tried with fminsearch or optimproblem, but none worked.
Thank you in advance!

 채택된 답변

Matt J
Matt J 2021년 5월 18일
There is no reason why an OptimizationVariable can't be vector-valued, e.g.,
P=optimvar('P',[6,1]);
size(P)
ans = 1×2
6 1

댓글 수: 5

Víctor García
Víctor García 2021년 5월 19일
편집: Víctor García 2021년 5월 19일
Then the problem should be in how I call the function: (being P a vector 1x6)
prob.Objective = function(P);
but I don't see why there should be a problem there
the error is the following:
The following error occurred converting from optim.problemdef.OptimizationExpression to single:
Conversion to single from optim.problemdef.OptimizationExpression is not possible.
I don't know what your revised code looks like, but you definitely can't do things like this:
P=optimvar('P',[6,1]);
single(P)
Error using single
Conversion to single from optim.problemdef.OptimizationVariable is not possible.
Matt J
Matt J 2021년 5월 19일
편집: Matt J 2021년 5월 19일
You also can't insert OptimizationVariables into numeric matrices, as in:
P=optimvar('P');
C=zeros(1,3,'single');
C(1)=P;
Unable to perform assignment because value of type 'optim.problemdef.OptimizationVariable' is not convertible to 'single'.

Caused by:
Error using single
Conversion to single from optim.problemdef.OptimizationVariable is not possible.
To be honest, I'm not sure why you are using optimvar at all, as opposed to the solver-based framework. Your constraints are not very complicated, and your objective function is. That's exactly the opposite of when the problem-based framework is useful.
How can I write then the prob.Objective function? Let's say you want to minimize a function f output (which is a scalar), and the input is that: P=optimvar('P',[6,1]); Wouldn't it be:
prob = optimproblem('Objective',@function(P);
[x, fval] = solve(prob)
Thank you Matt!
Matt J
Matt J 2021년 5월 19일
편집: Matt J 2021년 5월 19일
You have much more freedom in the way you write the objective function if you use the solver-based framework

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

질문:

2021년 5월 18일

편집:

2021년 5월 19일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by