Search code examples
wolfram-mathematicamathematical-optimizationdiscrete-mathematics

Mathematica, maximize element extraction from list


I think this is a simple question for mathematica experts. How can I maximize the extracted value from a list given a index that has to respect some constrains?

For example:

S = {4,2,3,5}

Maximize[{Extract[S,x], x<= 3, x>=1},{x}]

I would like 4 is returned instead of this error:

Extract::psl: "Position specification x in Extract[{4,2,3,5},x] is not an integer or a list of integers."

Does someone know like solve this?

Thanks a lot.


Thanks a lot!! The last approach shown is what I was looking for but applied to my real problem does not work.

I have the following problem:

I have to maximize the satisfaction of an employee with respect to a certain shift in an certain day of a month. I have the matrix satisfaction (Employees,shifts) and is something like this:

S= {{4,3,5,2},{3,4,5,1}}

Each element represents the satisfaction of an employee with respect to a certain shift so employee 1 has satisfaction 4 with respect shift 1.

My model has to choose the right shift for all month days in order to maximize the employee satisfaction by respecting certain constraints.

My greatest problem is relate satisfaction matrix with chosen shift.
I am not able to use in method NMaximize a function that takes the chosen shifts and employee and returns the satisfaction and so doing a summation over all month days. I need to maximize something like this:

Summation(from j=1 to j=31) getSatisfaction[1,chosenShift for that day)

Do you know how can I write this in mathematica?
I am struggling to this problem for several days but I am not able to solve this problem. I need the input to relate chosen shift with satisfaction matrix.

Thanks a lot!!


Solution

  • If you don't need to find the value of x then I suggest you merely extract the acceptable range of the list and then find the Max of that:

    s = {4,2,3,5};
    
    s[[1 ;; 3]] // Max
    
    4
    

    If you have particularly hairy constraints then you may need something like Pick:

    list = {5, 7, 1, 9, 3, 6, 2, 8, 4};
    
    Pick[list, Range@Length@list, x_ /; x <= 7 && x >= 3 && Mod[7, x] == 1]
    
    {1, 6} 
    

    You can then use Max on the returned list.


    For completeness, if you need the value of x or other details from the process, here is an approach:

    list = {6, 5, 7, 3, 4, 2, 1, 8, 9};
    
    pos = Cases[Range@Length@list, x_ /; x <= 7 && x >= 3 && Mod[7, x] == 1]
    
    values = Part[list, pos]
    
    maxpos = Part[pos, Ordering[values, -1]]
    
    {3, 6}
    
    {7, 2}
    
    {3}