Unable to perform assignment because value of type 'optim.pro​blemdef.Op​timization​Expression​' is not convertible to 'double'.

4 次查看(过去 30 天)
Hello!
I'm working on an optimization problem where I need to manage the availability of multiple cores for task execution. My current approach involves finding the earliest available core and updating its availability after assigning a task. However, I'm encountering an issue when trying to update the hapsCoreAvailability array with the new start time and execution time.
the error is
Unable to perform assignment because value of type 'optim.problemdef.OptimizationExpression' is not convertible to
'double'.
Error in Solve_Linear_Problem (line 355)
hapsCoreAvailability(coreIdx) = T_start + execTime;
Caused by:
Error using double
Conversion to double from optim.problemdef.OptimizationExpression is not possible.
here is the code
% Initialize the queue time matrix
Queue_time = optimexpr(N, numNodes, num_vehicles);
% Define variables
T_start = optimvar('T_start', 1, 'Type', 'continuous', 'LowerBound', 0);
% Constraints
Queue_constraints1 = [];
Queue_constraints2 = [];
% Track availability of each HAPS core
hapsCoreAvailability = zeros(1, hapsCapacity);
nodeQueues = cell(numNodes, 1); % Queue times for each node
% Process tasks by generation time
for taskIdx = 1:num_vehicles
for subtaskIdx = 1:N
for nodeIdx = 1:numNodes
% Extract generation time, execution time, and uplink time
taskGenTime = generation_times_matrix(subtaskIdx, nodeIdx, taskIdx);
execTime = Execution_time(subtaskIdx, nodeIdx, taskIdx);
uplinkTime = uplink_time(subtaskIdx, nodeIdx, taskIdx);
% Calculate task arrival time considering uplink time
taskArrivalTime = taskGenTime + uplinkTime;
if nodeIdx == 4 % If assigned to HAPS
% Find the earliest available core
% [earliestCoreTime, coreIdx] = min(hapsCoreAvailability);
coreIdx = -1;
earliestCoreTime = inf;
% Iterate through each core's availability time
for i = 1:length(hapsCoreAvailability)
% Check if the current core's availability is earlier than the current earliest time
if hapsCoreAvailability(i) < earliestCoreTime
% Update earliest core time and index
earliestCoreTime = hapsCoreAvailability(i);
coreIdx = i;
end
end
% Start time is the maximum of arrival time and core availability
% T_start should be greater than or equal to both T_arrival and T_core
Queue_constraints1 = [Queue_constraints1, T_start >= taskArrivalTime];
Queue_constraints1 = [Queue_constraints1, T_start >= earliestCoreTime];
Queue_time(subtaskIdx, nodeIdx, taskIdx) = T_start - taskArrivalTime;
% Update the core availability time
hapsCoreAvailability(coreIdx) = T_start + execTime; %% here is the error
else % For UAVs
% Queue tasks based on previous completion
if isempty(nodeQueues{nodeIdx})
startTime = taskArrivalTime;
else
T_queue = nodeQueues{nodeIdx}(end);
% T_start should be greater than or equal to both T_arrival and T_queue
Queue_constraints2 = [Queue_constraints2, T_start >= taskArrivalTime];
Queue_constraints2 = [Queue_constraints2, T_start >= T_queue];
end
% Calculate the queue time for the current subtask
Queue_time(subtaskIdx, nodeIdx, taskIdx) = T_start - taskArrivalTime;
% Update departure time for the current subtask
departureTime = T_start + execTime;
nodeQueues{nodeIdx} = [nodeQueues{nodeIdx}, departureTime];
end
end
end
end
prob.Constraints.queue_time_constraints = Queue_constraints1;
prob.Constraints.queue_time_constraints = Queue_constraints2;
How can I correctly update the hapsCoreAvailability array with the new start time and execution time in the context of my optimization problem? Is there an alternative way to manage and update core availability when dealing with OptimizationExpression objects?
Any advice or suggestions would be greatly appreciated!
Thank you!

回答(1 个)

Torsten
Torsten 2024-8-21
编辑:Torsten 2024-8-21
If you use optimvars on the right-hand side of the assignment
hapsCoreAvailability(coreIdx) = T_start + execTime;
(T_start is an optimvar !), hapsCoreAvailability must be an optimexpr, not a double array:
hapsCoreAvailability = zeros(1, hapsCapacity);
Consider using functions as fcn2optimexpr. Inputs can be optimvars or optimexpr, but in the function, they can be used as if they were doubles. Outputs from the function can again be doubles, and constraints using the outputs can be defined using the outputs in the calling program. So in the function itself, all operations are performed using doubles.
  2 个评论
Maria
Maria 2024-8-21
Thank you for your response.
I tried to replace the following part with function and i used as fcn2optimexpr, but i get this error :
Unable to use a value of type optim.problemdef.OptimizationNumeric as an index.
Error in Solve_Linear_Problem (line 343)
hapsCoreAvailability(coreIdx) = T_start + execTime;
coreIdx = -1;
earliestCoreTime = inf;
% Iterate through each core's availability time
for i = 1:length(hapsCoreAvailability)
% Check if the current core's availability is earlier than the current earliest time
if hapsCoreAvailability(i) < earliestCoreTime
% Update earliest core time and index
earliestCoreTime = hapsCoreAvailability(i);
coreIdx = i;
end
end
[earliestCoreTime, coreIdx] = fcn2optimexpr(@findEarliestCore,hapsCoreAvailability);
Torsten
Torsten 2024-8-21
编辑:Torsten 2024-8-21
Not enough information to give an answer. I do not even see the line where MATLAB complains in the piece of code you included.
If you try to use an optimvar as an array index, I doubt that this is allowed:
x = [1 2 3 4 5 6 7 8];
index = optimvar('index','Type','integer','LowerBound',1,'UpperBound',8);
obj = x(index);
Unable to use a value of type optim.problemdef.OptimizationVariable as an index.
prob = optimproblem('Objective',obj);
solve(prob)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Representation in Generated Code 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by