Error : Unable to use a value of type optim.problemdef.OptimizationVariable as an index.
Hello !
I am working on a problem-based optimization task where I need to assign 4 subtasks to 4 different nodes. My approach involves defining an optimization variable as a vector with integer elements ranging from 1 to 4, each representing the node assigned to a subtask.
Here’s a snippet of my current setup:
numSubtasks = 4;
numNodes = 4;
taskAssignmentVector = optimvar(‘taskAssignmentVector’, numSubtasks, ‘Type’, ‘integer’, ‘LowerBound’, 1, ‘UpperBound’, numSubtasks);
My goal is to reshape this vector into a 4×4 binary assignment matrix within the optimization framework. Each row of this matrix should correspond to a subtask, and each column to a node, with ‘1’ indicating the assignment.
I attempted to implement this by creating a function to generate the assignment matrix based on the vector, but I’m facing challenges in using the optimization variable as an index, leading to errors.
AssignedMatrix = zeros(numSubtasks, numNodes);
for s = 1:numSubtasks
nodeAssigned = taskAssignmentVector(s); % Node assigned for each subtask
AssignedMatrix(s, nodeAssigned) = 1;
end
Could you please advise on the best approach to reshape this vector into a matrix form within the problem-based optimization framework? I am looking for a way to link the task assignments in the vector with a binary matrix that I can use in my objective function and constraints.
Thank you for your assistance!Hello !
I am working on a problem-based optimization task where I need to assign 4 subtasks to 4 different nodes. My approach involves defining an optimization variable as a vector with integer elements ranging from 1 to 4, each representing the node assigned to a subtask.
Here’s a snippet of my current setup:
numSubtasks = 4;
numNodes = 4;
taskAssignmentVector = optimvar(‘taskAssignmentVector’, numSubtasks, ‘Type’, ‘integer’, ‘LowerBound’, 1, ‘UpperBound’, numSubtasks);
My goal is to reshape this vector into a 4×4 binary assignment matrix within the optimization framework. Each row of this matrix should correspond to a subtask, and each column to a node, with ‘1’ indicating the assignment.
I attempted to implement this by creating a function to generate the assignment matrix based on the vector, but I’m facing challenges in using the optimization variable as an index, leading to errors.
AssignedMatrix = zeros(numSubtasks, numNodes);
for s = 1:numSubtasks
nodeAssigned = taskAssignmentVector(s); % Node assigned for each subtask
AssignedMatrix(s, nodeAssigned) = 1;
end
Could you please advise on the best approach to reshape this vector into a matrix form within the problem-based optimization framework? I am looking for a way to link the task assignments in the vector with a binary matrix that I can use in my objective function and constraints.
Thank you for your assistance! Hello !
I am working on a problem-based optimization task where I need to assign 4 subtasks to 4 different nodes. My approach involves defining an optimization variable as a vector with integer elements ranging from 1 to 4, each representing the node assigned to a subtask.
Here’s a snippet of my current setup:
numSubtasks = 4;
numNodes = 4;
taskAssignmentVector = optimvar(‘taskAssignmentVector’, numSubtasks, ‘Type’, ‘integer’, ‘LowerBound’, 1, ‘UpperBound’, numSubtasks);
My goal is to reshape this vector into a 4×4 binary assignment matrix within the optimization framework. Each row of this matrix should correspond to a subtask, and each column to a node, with ‘1’ indicating the assignment.
I attempted to implement this by creating a function to generate the assignment matrix based on the vector, but I’m facing challenges in using the optimization variable as an index, leading to errors.
AssignedMatrix = zeros(numSubtasks, numNodes);
for s = 1:numSubtasks
nodeAssigned = taskAssignmentVector(s); % Node assigned for each subtask
AssignedMatrix(s, nodeAssigned) = 1;
end
Could you please advise on the best approach to reshape this vector into a matrix form within the problem-based optimization framework? I am looking for a way to link the task assignments in the vector with a binary matrix that I can use in my objective function and constraints.
Thank you for your assistance! optimization, solve, error MATLAB Answers — New Questions