You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Error : Failure in initial user-supplied nonlinear constraint function evaluation.
118 views (last 30 days)
Show older comments
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 Comments
Maria
on 12 Aug 2024 at 19:35
Hello @Walter Roberson
N,numNodes,Devices are variables, we can set them (4,4,3) as example
Walter Roberson
on 12 Aug 2024 at 19:37
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
on 12 Aug 2024 at 20:48
Edited: Torsten
on 12 Aug 2024 at 20:55
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
on 12 Aug 2024 at 20:50
Edited: Torsten
on 12 Aug 2024 at 20:53
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
Maria
on 12 Aug 2024 at 21:00
@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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1751329/image.png)
Torsten
on 12 Aug 2024 at 21:09
I put all posted functions in the code above. It seems complete now.
Walter Roberson
on 12 Aug 2024 at 21:28
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.
Walter Roberson
on 12 Aug 2024 at 22:36
It is a useless expression, because it does not assign to any output variable!
Torsten
on 13 Aug 2024 at 8:50
Edited: Torsten
on 13 Aug 2024 at 9:12
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.
Answers (1)
Walter Roberson
on 13 Aug 2024 at 5:46
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 Comments
Maria
on 13 Aug 2024 at 9:14
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
on 13 Aug 2024 at 9:25
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
on 13 Aug 2024 at 9:29
in this case i didn't use "fcn2optimexpr". I removed 'find' and i call the function as above .
Torsten
on 13 Aug 2024 at 18:44
Edited: Torsten
on 13 Aug 2024 at 18:47
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);
Torsten
on 13 Aug 2024 at 19:14
As said: we need the code to reproduce the error message in order to say anything.
Torsten
on 13 Aug 2024 at 19:50
Edited: Torsten
on 13 Aug 2024 at 20:14
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);
Torsten
on 13 Aug 2024 at 21:03
Edited: Torsten
on 13 Aug 2024 at 21:09
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
on 13 Aug 2024 at 22:12
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
on 14 Aug 2024 at 10:09
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
on 14 Aug 2024 at 12:05
Edited: Torsten
on 14 Aug 2024 at 12:10
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.
Maria
on 14 Aug 2024 at 12:11
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
Torsten
on 14 Aug 2024 at 12:36
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
See Also
Categories
Find more on Get Started with Problem-Based Optimization and Equations in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)