필터 지우기
필터 지우기

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

조회 수: 10 (최근 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에서 Nonlinear Least Squares (Curve Fitting)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by