Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica, NMinimize extract data from list

  1. Jan 22, 2012 #1
    Hi guys,

    I am struggling again NMinimize.

    This is my problem:

    I am creating a model by using goal programming, my problem now is given a list of list i.e a matrix defined in this way

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

    extract from the first list {5,2,7,9} a number by passing a variable in extract method.

    I mean, this is my model:

    NMinimize [{

    n1+p1, (this is my objective function)
    x1j == 2 (Now i am using a stupid number but this will be modified)
    Ʃ Extract[Extract[S,1],x1j]+n1-p1 == 10, (The summation start from j=1 to 2)
    n1>= 0,


    In this case, on my side the result should be x11 = 5, x12 = 5, n1=0, p1=0, but unfortunately this not happen.

    This error is launched:

    Extract::psl: Position specification Subscript[x, 1,1] in Extract[{5,2,7,9},Subscript[x, 1,1]] is not an integer or a list of integers. >>
    NMinimize::bcons: The constraints are not valid. >>

    Someone can help me?

    Thanks a lot!
  2. jcsd
  3. Jan 22, 2012 #2
    If you could stop writing things that are not Mathematica code and instead write a very simple understandable description of what you want to accomplish then it might be possible for someone to translate that into Mathematica for you.
  4. Jan 22, 2012 #3
    I try to be more clear.

    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!!
  5. Jan 22, 2012 #4
    Try to forget for the moment that NMinimize exists, there is no such function. You perhaps cannot use NMinimize on functions in the way you think so this may be for the best.

    Consider using instead First[Sort[vector,myShiftSortingFunction]]

    To use this you must have two things.
    1: a vector containing all the possible shifts, not a matrix.
    2: define myShiftSortingFunction which is given two shifts and determines which of the two is preferable.

    Here is an example:
    In[1]:= shifts={{5,2,5,4},{2,5,2,8},{1,7,3,4},{4,5,6,2}};

    Out[3]= {1,7,3,4}

    That has found the shift with the smallest first element.
    This is as if you were able to use NMinimize on a vector of shifts and find the shift with the smallest first value.
    Make up some example vectors of shifts and see if you are able to use this to correctly extract shifts.

    Clearly you will then want to do something more complicated than this. Perhaps you can use this idea to find what you need.

    If not then try to describe your problem even more clearly. Describe what data you have in what structure. Describe what conditions you need to enforce. Give a complete example and what the result should be and why.

    Attach all that to a post as a simple Mathematica notebook. Most others do not want to type back in your notebook from an image to try to test their code on your data.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook