Error : Unable to use a value of type optim.prob​lemdef.Opt​imizationV​ariable as an index.

7 次查看(过去 30 天)
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 4x4 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!

回答(1 个)

Matt J
Matt J 2023-12-7
编辑:Matt J 2023-12-7
It sounds like the better approach would be to have the 4x4 binary matrix AssignedMatrix be the fundamental unknown optimization variable.
AssignedMatrix = optimvar('AssignedMatrix', [4,4], 'Type', 'integer', 'LowerBound', 0, 'UpperBound', 0);
Once you've done so, the taskAssignmentVector is easily expressed as a linear expression of the AssignedMatrix:
prob=optimproblem;
prob.Constraints.OneToOne1=sum(AssignedMatrix,1)==1;
prob.Constraints.OneToOne2=sum(AssignedMatrix,2)==1;
taskAssignmentVector=AssignedMatrix*(1:4)';
  69 个评论
Maria
Maria 2023-12-20
I'm talking about the result just for the satisfaction rate it didn't improve compared with other solutions, I thought that with optimization I will get more satisfied tasks but I get less.
Matt J
Matt J 2023-12-20
Two remarks,
(1) You are not using my alternative objective function. I recommended that function because it is continuous and might be easier to optimize.
(2) Your call to solve() does not specify an initialAssignment as an initial point. Therefore, the optimization does not know what it is supposed to try to improve upon.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by