KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ how to solve this allocation problem Re: how to solve this allocation problem

  • davidcrossey

    Member
    May 14, 2022 at 12:00 am

    If you had the following scenario, you could sort the reward desending, sort the pickSeq ascending and use indices to solve for your problem:

    q)n:10 
    q)people:([]pickSeq:0+til n;person:`$"person",/:string 1+til n;allowedToPick:n?01b)
    q)people 
    pickSeq person allowedToPick 
    ------------------------------ 
    0 person1 1 
    1 person2 1 
    2 person3 1 
    3 person4 1 
    4 person5 0 
    5 person6 0 
    6 person7 0 
    7 person8 1 
    8 person9 0 
    9 person10 0 
    
    q)rewards:([]prize:(1+til 10)*100) 
    q)rewards 
    prize 
    ----- 
    100 
    200 
    300 
    400 
    500 
    600 
    700 
    800 
    900 
    1000 
    
    q)(update ind:i from xdesc[`prize;rewards]) lj `ind xkey update ind:i from select person from xasc[`pickSeq;people] where allowedToPick 
    prize ind person 
    ----------------- 
    1000 0 person1 
    900 1 person2 
    800 2 person3 
    700 3 person4 
    600 4 person8 
    500 5 
    400 6 
    300 7 
    200 8 
    100 9

    You could also extract the person and their prize as follows:

    q){if[1=count x;x:enlist x]; if[1=count y;y:enlist y]; y!x iasc y}[exec desc prize from rewards;] exec person from xasc[`pickSeq;people] where allowedToPick 
    person1| 1000 
    person2| 900 
    person3| 800 
    person4| 700 
    person8| 600