Constraints used but not applied (beginner)

1 次查看(过去 30 天)
Hello,
I'm trying to find out answer of a problem which is having equation like mentioned below,
Now adding these contraints (1 and 2) into if-else is the only way to use them or is it good practice? while using the constraints after formula is kind of doubt whether these are applied to the formula or not or writing constraints in this way a good practice or not (specially 2nd constraints). Kindly let me know if you think there is better way or to improve the things. Thanks in advance
E_pv = npv * Apv * Iir(1 - 0:005(Ta((t) - 25)); %all the varibles have scalar values
R_ct=min(Energy_p, E_pv ); %Constraint 1
0 <= R_Pv <= (E_pv -R_ct); %Constraint 2
  3 个评论
Adam Danz
Adam Danz 2021-7-28
@Walter Roberson's answer summarizes the issues well. I wonder if Ta() is a function or a non-scalar variable.
shane watson
shane watson 2021-7-28
编辑:shane watson 2021-7-28
@Adam Danz, Yes, Ta() is non scalar variable, to rewrite the equation it could be like E_pv (t) = npv * Apv * Iir(t)(1 - 0.005(Ta((t) - 25)); wehre t=1:24, thank for being here, yes, walter response is nice and detailed one, however, my confusion is still alive which is how to bound formula within given range using coding. Basically, I was using "if-else" before to bound the formula but I'm afraid whether it is right way to do that or not?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-7-28
E_pv = npv * Apv * Iir(1 - 0:005(Ta((t) - 25)); %all the varibles have scalar values
That is invalid syntax. 0:005(Ta((t) - 25)) would be an attempt to index the numeric constant "005", and to use the result of the indexing as the upper bound on a colon operator whose lower bound was 1-0 (which is 1). MATLAB has absolutely no implied multiplication, so if you wanted multiplication you need to code it, such as
E_pv = npv * Apv * Iir(1 - 0:005*(Ta((t) - 25)); %all the varibles have scalar values
After you fix that, you have the expression 1 minus and so on, acting as an index to the variable Iir, but you remarked that all of the variables are scalars, so indexing is very likely to be a problem. Again, there is no implied multiplication in MATLAB.
Once you have calculated the (possibly vector) E_pv you do
R_ct=min(Energy_p, E_pv ); %Constraint 1
That is a calculation, not an constraint as such. You calculate the element-for-element minimum of Energy_p and E_pv and store the result into R_ct . It is an operation like any other arithmetic operation, it does not in itself constrain anything.
0 <= R_Pv <= (E_pv -R_ct); %Constraint 2
That code means that (0 <= R_Pv) should be calculated first, and a logical result the same size as R_Pv should be created, with 0 for false and 1 for location the relationship holds.
Then after that, E_pv -R_ct is calculated. Then the result of the calculation will be compared to the 0 and 1 results from the 0 <= R_Pv part. R_Pv is not being tested as being between 0 and (E_pv -R_ct) by this code! MATLAB does not have chains of comparisons for numeric values (there is one context where it permits chains of relationships for symbolic values.) If you want to test whether a variable is in range, use lb <= VARIABLE & VARIABLE <= ub
After the logical result of the comparisons has been made, your code then sets the variable ans to the result, and then throws away the result otherwise: you are not assigning the result to a variable, and you are not testing the result in an if or while, and you are not displaying the result. So MATLAB is just going to throw it away. It is not going to constrain anything.
If you want constaints for an optimization, then the way to implement them depends upon whether you are using "solver based" solutions or "problem based solutions". Problem-based solutions is a newer technique for expressing relationships between variables that must be satisfied, and then telling MATLAB to figure out how to solve it; the older "solver based" requires that you know how to code fmincon() or similar functions.
  3 个评论
Walter Roberson
Walter Roberson 2021-7-29
When you talk about putting in a constraint, then normally that means one of a few things:
  • in a situation where new numbers are to be generated (perhaps random numbers), that the values are to be created in such a way that they are inherently going to be within a particular range (bounds constraints, sometimes linear constraints)
  • in a situation where new numbers are to be generated, that the values are to be created in such a way that if a particular set of relationships between the values is not satisfied, that the generating routine is to go back and try again until it finds values that satisfy the relationships (nonlinear constraints, sometimes linear constraints), and so the next stage of the calculation always receives values that satisfy the relationships because the code went back and tried again until it found some
  • in a situation involving symbolic formulas, that the formulas are to be manipulated and available parameters are to be adjusted in such a way that the result of the extended formulas inherently satisfies required relationships, so that once the formulas or parameters are determined, the program does not need to worry about the relationships afterwards
It is not clear which of these meanings you want.
I suspect that you have a different meaning entirely, one that would not normally be called putting in a constraint. I suspect that you mean one of the following:
  • that a set of values is to be tested, and that any value that is outside of a particular range is to be replaced with the closest endpoint; for example, "if x < 5 then use 5 instead of x"; OR
  • perhaps instead you want to test a set of values and extract only the ones that satisfy a particular relationship, and possibly corresponding positions in a different array as well, with the values that are outside the range just not being used for the next step of the calculations. For example, if you were finding roots of a polynomial, your application might be such that you want all of the real roots that are greater than zero and you want to ignore all complex roots and all negative roots
What do you want to have happen if E_Pv_min <= E_Pv <= E_Pv_max is not true for some E_Pv value?
Do you want to have your code go back and generate new E_Pv values for those locations, not continuing on until you have a set of E_Pv values of particular size, all of which satisfy the restrictions?
Do you want to manipulate symbolic formulas to arrive at new formulas such that the new formulas will automatically satisfy the restrictions, with no retries required?
Do you want assistance from us as to how you could have coded previous steps so that numeric values that do not meet the retrictions simply cannot be generated?
Do you want to select out the subset of values that meet the restrictions (and possibly corresponding positions in a different array) and go ahead and process only the subset, that might be smaller than the original set of data?
shane watson
shane watson 2021-8-6
I'm sorry for the late response, but I must say thank you for a very comprehensive response @Walter Roberson, I really appreciate it. Now to summarize what I really need is, I've two scenarios as follows:
NO 1: I'm using greedy optimization algorithm/metaheuristics
NO 2: Problem-specific formulas bound by constraints.
For NO 1: I do need the situation that you talked about in the answer (First and Second point) and
For NO 2: Yes, I agreed with what you suspect (actually, this is what I meant in my question).
After your questions (1-4), things are very much summarized and cleared, however, I really appreciate, if you assist/guide me about the first three questions (1,2,3) a little bit. Since my whole problem (NO 1 and NO 2) goes around these questions. Thanks in advance.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by