Error : Failure in initial user-supplied nonlinear constraint function evaluation.
118 次查看(过去 30 天)
显示 更早的评论
Maria
2024-8-12,18:42
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 个评论
Maria
2024-8-12,19:35
Hello @Walter Roberson
N,numNodes,Devices are variables, we can set them (4,4,3) as example
Walter Roberson
2024-8-12,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
2024-8-12,20:48
编辑:Torsten
2024-8-12,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
2024-8-12,20:50
编辑:Torsten
2024-8-12,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
2024-8-12,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)
Walter Roberson
2024-8-12,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
2024-8-12,22:36
It is a useless expression, because it does not assign to any output variable!
Torsten
2024-8-13,8:50
编辑:Torsten
2024-8-13,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.
回答(1 个)
Walter Roberson
2024-8-13,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 个评论
Maria
2024-8-13,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
2024-8-13,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
2024-8-13,9:29
in this case i didn't use "fcn2optimexpr". I removed 'find' and i call the function as above .
Torsten
2024-8-13,18:44
编辑:Torsten
2024-8-13,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
2024-8-13,19:14
As said: we need the code to reproduce the error message in order to say anything.
Torsten
2024-8-13,19:50
编辑:Torsten
2024-8-13,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
2024-8-13,21:03
编辑:Torsten
2024-8-13,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
2024-8-13,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
2024-8-14,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
2024-8-14,12:05
编辑:Torsten
2024-8-14,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
2024-8-14,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
2024-8-14,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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Problem-Based Optimization and Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)