Error : Failure in initial user-supplied nonlinear constraint function evaluation.

Hello everyone!
I'm encountering an issue with my MATLAB code and could use some assistance. Specifically, I'm getting the following error message:
[sol, fval, exitflag, output] = solve(prob);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
It seems to be related to the evaluation of the nonlinear constraint function. I've attached the relevant part of my code below. I'm particularly confused because I linearised all my constraint terms, so I wasn't expecting any non-linearity.
Could you please help me identify the issue and suggest any potential fixes?
Thank you!
best Regards!
MyProblem()
Unrecognized function or variable 'N'.

Error in MyProblem (line 2)
A = optimvar('A', N, numNodes, Devices, 'Type', 'integer', 'LowerBound', 0, 'UpperBound', 1);

12 件のコメント

Walter Roberson
Walter Roberson 2024 年 8 月 12 日
Variable N is not known.
Maria
Maria 2024 年 8 月 12 日
N,numNodes,Devices are variables, we can set them (4,4,3) as example
N = 4;
numNodes = 4;
Devices = 3;
MyProblem()
Unrecognized function or variable 'tasks_generated'.

Error in MyProblem (line 5)
SR = sum(chi .* sum(tasks_generated, 2)) / sum(tasks_generated(:));
Torsten
Torsten 2024 年 8 月 12 日
編集済み: Torsten 2024 年 8 月 12 日
We must be able to reproduce the error - otherwise we can't help.
Please add the missing routines @Walter Roberson listed to the code above and use the >RUN button to check whether the data and functions are complete.
Walter Roberson
Walter Roberson 2024 年 8 月 12 日
編集済み: Torsten 2024 年 8 月 12 日
We are missing:
  • Compute_data_rate
  • Compute_uplink_time_linear
Also, the lines
if dependency_matrix(k, prev_subtask, m) == 1
t_wait_aux >= Completion_Time(prev_subtask, prev_node, m) - uplink_time(k, n, m);
end
are not doing anything useful
@Walter Roberson i've used this
if dependency_matrix(k, prev_subtask, m) == 1
t_wait_aux >= Completion_Time(prev_subtask, prev_node, m) - uplink_time(k, n, m);
end
because i have this non linear expression (max function)
Torsten
Torsten 2024 年 8 月 12 日
I put all posted functions in the code above. It seems complete now.
t_wait_aux >= Completion_Time(prev_subtask, prev_node, m) - uplink_time(k, n, m);
calculates the result of the >= comparison, and assigns the result to the variable ans and surpresses display of ans because of the semi-colon.
Notice that you do not assign any output.
Maria
Maria 2024 年 8 月 12 日
I found that it was the adequate expression to avoid use max function
Walter Roberson
Walter Roberson 2024 年 8 月 12 日
It is a useless expression, because it does not assign to any output variable!
Maria
Maria 2024 年 8 月 13 日
Does the issue come from this expression!!
Torsten
Torsten 2024 年 8 月 13 日
編集済み: Torsten 2024 年 8 月 13 日
No. You just didn't set how t_wait_aux is defined.
Further, it's not necessarily true that setting
m >= x(1),m >= x(2),...,m >= x(n)
will give m = max(x) in an optimization. All values for "m" that are bigger than max(x) also satisfy the constraints.
It must be the case that "m" influences the objective function such that the lower its value the lower the value of the objective function (if your aim is to minimize). If this is the case, "m" will be chosen as max(x) by the optimizer.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 8 月 13 日
assigned_node = find(A(sb, :, v), 1);
covering_uav = find(zeta_m_n(v, :), 1);
if assigned_node == covering_uav
That code has a problem: assigned_node can come out empty. That leads to problems with the statement
arg2_covering_to_assigned(k) = arg2(covering_uav, assigned_node, k)
as arg2() comes out empty when assigned_node is empty, but an empty result cannot be assigned to a definite output location.
When I add in some tracing lines, I get
no assigned node at v = 1, sb = 4, node = 1
no assigned node at v = 1, sb = 4, node = 2
no assigned node at v = 1, sb = 4, node = 3
no assigned node at v = 1, sb = 4, node = 4
no assigned node at v = 2, sb = 1, node = 1
no assigned node at v = 2, sb = 1, node = 2
no assigned node at v = 2, sb = 1, node = 3
no assigned node at v = 2, sb = 1, node = 4
repeated over and over again.

20 件のコメント

When i call find on A, it's looking for actual binary values (0 or 1), but since it's an optimization variable, it doesn’t contain concrete values until after the optimization is solved(that's why i used fcn2optimexpr).
Here is the reformulated SNR calculation without using find and instead directly express the SNR in terms of A.
function SNR_matrix = Compute_SNR(N, numNodes, Devices, A, zeta_m_n, h0, distances, alpha, RIS_elements, arg1, arg2, Theta_node, P_v, Noise_up)
SNR_matrix = optimexpr(N, numNodes, Devices);
for v = 1:Devices
for sb = 1:N
for node = 1:numNodes
for node1 = 1:numNodes-1
direct_SNR = sqrt(h0 * distances(v, node1)^(-alpha));
direct_SNR = P_v * norm(direct_SNR)^2 / Noise_up;
effective_power_sum = optimexpr(1);
for k = 1:RIS_elements
effective_channel = Theta_node{node1} * arg1(v, node1, k) .* arg2(node1, node, k);
effective_power_sum = effective_power_sum + norm(effective_channel)^2;
end
ris_SNR = P_v * effective_power_sum / Noise_up;
% Combine both contributions based on assignment and covering UAV
SNR_matrix(sb, node, v) = A(sb, node, v) * (zeta_m_n(v, node1) * direct_SNR + (1 - zeta_m_n(v, node1)) * ris_SNR);
end
end
end
end
end
Torsten
Torsten 2024 年 8 月 13 日
As far as I know, in an "fcn2optimexpr", you can do all computations as if they were numerical. So no need to define the two "optimexpr" for "SNR_matrix" and "effective_power_sum".
Maria
Maria 2024 年 8 月 13 日
in this case i didn't use "fcn2optimexpr". I removed 'find' and i call the function as above .
Torsten
Torsten 2024 年 8 月 13 日
And the code works now ?
Maria
Maria 2024 年 8 月 13 日
No, i'm still trying.
Torsten
Torsten 2024 年 8 月 13 日
編集済み: Torsten 2024 年 8 月 13 日
Please supply the complete code you are using.
Are you sure to define SNR_matrix after computing it ?
SNR_matrix = Compute_SNR(N, numNodes, Devices, A, zeta_m_n, h0, distances, alpha, RIS_elements, arg1, arg2, Theta_node, P_v, Noise_up)
SNR_matrix = optimexpr(N, numNodes, Devices);
Maria
Maria 2024 年 8 月 13 日
@Torsten it was just a mistake of copy paste, i already fixed it above.
Torsten
Torsten 2024 年 8 月 13 日
As said: we need the code to reproduce the error message in order to say anything.
Maria
Maria 2024 年 8 月 13 日
Already fixed!
Torsten
Torsten 2024 年 8 月 13 日
編集済み: Torsten 2024 年 8 月 13 日
Scroll through your constraints in the code above, and you will see that some of them cannot be fulfilled : they are of the form 0 = 1.
Maria
Maria 2024 年 8 月 13 日
yes this is the problem, i didn't understand from where it comes 0.
Torsten
Torsten 2024 年 8 月 13 日
編集済み: Torsten 2024 年 8 月 13 日
Data_rate must be an optimvar, not an optimexpr, I guess.
And be careful:
In the loop over i, it could happen that you assign conflicting constraints to Data_rate(sb, node, v).
Or you only want to assign an optimexpr to Data_rate(sb,node,v).
Then simply use
Data_rate(sb, node, v) = B_Uplink_MHz * (slope * SNR_matrix(sb, node, v) + intercept);
instead of imposing constraints:
prob.Constraints.(['approximation_', num2str(v), '_', num2str(sb), '_', num2str(node), '_', num2str(i)]) = ...
Data_rate(sb, node, v) == B_Uplink_MHz * (slope * SNR_matrix(sb, node, v) + intercept);
Maria
Maria 2024 年 8 月 13 日
I did, but something went wrong.
Torsten
Torsten 2024 年 8 月 13 日
編集済み: Torsten 2024 年 8 月 13 日
You must think about which equalities in your model are simple definitions of new variables and what are real constraints for optimization. I could bet that Data_rate(sb,node,v)=..., e.g., is a simple definition of a new variable as a combination of optimvars.
I changed the setting in the code above and MATLAB tells you that you got a nonlinear problem with equality constraints that ga cannot solve (besides other strange constraints of the form 0 <= 1).
Maria
Maria 2024 年 8 月 13 日
I don't know if this can work, for example i solve nonlinear problem related to uplink time using fmincon and use the result to solve the original problem.
Torsten
Torsten 2024 年 8 月 14 日
I don't know if this can work, for example i solve nonlinear problem related to uplink time using fmincon and use the result to solve the original problem.
What do you mean by "this" ?
Torsten
Torsten 2024 年 8 月 14 日
編集済み: Torsten 2024 年 8 月 14 日
I thought I could use different solvers to solve different parts of my problem, but it doesn't seem possible.
Usually it's possible, but I don't see an attempt where you try it in the code above.
And what is the problem with the code ? "intlinprog" claims having found the optimal solution.
There is no calculation done for SNR, Data rate and uplink time , so it was 0.
subject to uplinkConstraint:
0 <= 0.3039
0 <= 0.31376
0 <= 0.40509
If you define
Vehicle_Data = zeros(N, numNodes, Devices); % Ensure Vehicle_Data is defined
you automatically get uplink_time = 0, thus T_off = 0 and finally the constraints
subject to uplinkConstraint:
0 <= 0.3039
0 <= 0.31376
0 <= 0.40509
Maria
Maria 2024 年 8 月 14 日
Yes i adjusted above.

サインインしてコメントする。

カテゴリ

質問済み:

2024 年 8 月 12 日

コメント済み:

2024 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by