Dynamical array increment at a certain index without knowing the increment value

In this program, I want to update an array of task_table(M) dynamically, i.e. I search the whole min_dist array exhaustively and if min_dist_p(n,k) is the minimum among min_dist(n,k,p), of which I do not know the value, I increment task_table(p) by one. Specifically, because I don’t know the value of a(n,k,p) before optimization, I want to increment it as a cvx object. However, I get this error when I write “task_table(last_node) = task_table(last_node) + a(n,k,p);”:

Incorrect number or types of inputs
or outputs for function sign.

Error in brute_force (line 124)
a(n,k,p) = ~sign(min_dist(n,k,p) - min_dist_p(n,k));

Do you know another way to write this program? Thanks….
cvx_begin

% --- Recursive Helper (Propagation Logic) ---
function path_data = build_propagation_paths(n, dist_matrix, task_table, r_max, L, M, depth, acc_dist, prev_uav, current_seq)
    %if 
    expr = (depth > 1 && task_table(prev_uav) < r_max)
    node.total_dist = acc_dist * ~expr;
    node.last_uav = prev_uav * ~expr;
    node.sequence = current_seq  * ~expr;
    if ~expr
        path_data =  node;
        return;
    end
    %end
    if depth > L
        node.total_dist = acc_dist;
        node.last_uav = prev_uav;
        node.sequence = current_seq;
        path_data = node;
        return;
    end
    path_data = [];
    for uav_idx = 1:M
        if uav_idx == prev_uav, continue; end
        step_dist = dist_matrix(prev_uav, uav_idx);
        child_paths = build_propagation_paths(n, dist_matrix, task_table, r_max, L, M, ...
                                               depth + 1, acc_dist + step_dist, ...
                                               uav_idx, [current_seq, uav_idx]);
        path_data = [path_data, child_paths];
    end
end
    cvx_begin
        variable alpha_switch(M,N,K)
        variable node_val
        variable beta_switch(N,M,M)
        clear rate_raw_first
        variable min_dist_p(N,num_tasks)
        
        variable index_min(N,num_tasks)
        variable task_table(M)
        expression a(N, num_tasks)
        total_sum_time_n = 0.0;
        for n=1:N
            for i = 1:M
                for j = 1:M
                    [trans_time rate]= transfer_time(M, n, i, j, sigma_squ, rho_0, P, H, Q, w, D); 
                    dist_matrix(n,i,j) = trans_time * GREAT_NUMBER * (1 - beta_switch(n,i,j));
                    trans_time_x_x(n,i,j) = trans_time;
                end
            end
        end
        
        for n=1:N
            trans_x_time = squeeze(trans_time_x_x(n,:,:));
            for k = 1:num_tasks
                    % 2. Discovery: Enumerate valid propagation paths
                paths = build_propagation_paths(n, squeeze(dist_matrix(n,:,:)), task_table, r_max, L, M, 1, 0, 1, 1);
                
                total_sum_time = 0.0;
               
                first_uav = 1;
                r_max = 1;
                f=1e+9;
                C = 1e+7;
                num_paths(n,k) = length(paths);
                % clear min_dist
                % variable min_dist(N,num_tasks,num_paths)
                D_travel = [];
                last_node = 0;
                for p = 1:num_paths(n,k)
                    min_dist(n,k,p) = paths(p).total_dist;
                    min_sequence = paths(p).sequence;
                    last_node = min_sequence(end);
                    task_table(last_node) = task_table(last_node) + a(n,k,p);
                    
                end

                total_sum_time = total_sum_time +  min_dist_p(n,k);
                 
            end
            total_sum_time_n = total_sum_time_n + total_sum_time;
        end
        minimize( total_sum_time_n)
        subject to
            for n=1:N
                 for k = 1:num_tasks
                     for p = 1:num_paths
                        min_dist(n,k,p) - min_dist_p(n,k) >= 0 
                        a(n,k,p) = ~sign(min_dist(n,k,p) - min_dist_p(n,k));
                     end
                     
                 end
            end
             for n=1:N
                 %total_beta = 0.0;
                 for i=1:M
                    sum(transpose(squeeze(beta_switch(n,i,:)))) <= 1;
                    %total_beta = total_beta + transpose(squeeze(beta_switch(n,i,:)));
                    for j=1:M
                        0<= beta_switch(n,i,j)<=1;
                    end
                 end
                 %total_beta <= 1;
             end    
    cvx_end
    

I don’t understand exactly what you’re trying to d. But if you’re trying to pick out a specific index which is a CVX variable or expression, or branching something based on a CVX variable or expression, you’re probably going to need to use binary or integer variables to do logic programming.

Browsing through Q&A at https://or.stackexchange.com/search?q=logic+ or https://or.stackexchange.com/search?q=index+is+decision+variable+ . might help you more clearly define what you are trying to do, and find an optimization problem formulation involving binary or integer variables which can be implemented following CVX MIDCP rules.

Thank you for your response. I am trying to do the following:

I have two “cvx” objects or a “cvx” and a “constant”. I compare both and try to get the result of comparison, which I don’t know the result yet. If the first argument is greater than the second, then increment the task array by one, which I don’t know yet numerically because I want to do every calculation is in cvx objects and I check every time if the task table overflows, again, I don’t know the value. Everything will have a value after cvx_end. Is there any way to accomplish this? I get always the same error:

Incorrect number or types of
inputs or outputs for function
sparse.

Thanks…

Baris Koc

You’ll need to do logic programming. Look at the links I provided. You may also find Logics and integer-programming representations - YALMIP to be useful; the ideas in it can be used in CVX MIDCP.

Thank you very much…

This time I tried logic programming, but I still have problems:

I have written this piece of code:

task_table(i) >= r_max_table(i) - M_big * lower_than_r_max(i)

task_table(i) <= r_max_table(i) + M_big * (1 - lower_than_r_max(i))

1)Here, “task_table” is a vector of variables and “r_max_table” is vector of constants and I use lower_than_r_max in the program for decision. If I define “variable lower_than_r_max(M) binary“, I get this error:

The following error occurred converting from cvx to
logical:
Conversion to logical from cvx is not possible.

expr = depth > 1 && lower_than_r_max(prev_uav);(I get error from this line)

  1. If I don’t define “variable lower_than_r_max(M) binary“, it cannot recognize the variable, then I initialize “lower_than_r_max”, but this time, CVX fixes the variable and the optimization doesn’t work.

In summary, I want “lower_than_r_max” to be a logical variable but not a CVX object. Is there any way to make this variable a logical variable.