jawshawaaa
jawshawaaa

Reputation: 15

How to find possible combinations of 3 values given their maximums?

Given 3 maximum values, (m1 m2 m3), I'm trying to find the closest possible combination of n objects with the same values (m1, m2, m3) to satisfy the given maximums. If there are multiple solutions, the first can be picked, and multiple instances of an object can be used for the solution.

An example of the input would be something like:

[A: {m1: 20, m2: 15, m3: 10}, B: {m1: 10, m2: 12, m3: 10}, C: {m1: 15, m2: 10,: m3: 30}]

So for example, when (m1=30, m2=17, m3=20) a possible solution would be [A,B]

I've been looking into the knapsack problem, and it's variations (like multiple-knapsack), however, after some attempts I can't figure out how to modify it to have multiple values that can be put into multiple knapsacks. Am I on the right track? Or is there another algorithm better suited for this problem that can be used/modified? Thanks

Upvotes: 0

Views: 59

Answers (1)

sveyda
sveyda

Reputation: 76

let space = { 'A':{m1: 20, m2: 15, m3: 10}, 'B': {m1: 10, m2: 12, m3: 10}, 'C': {m1: 15, m2: 10, m3: 30}} 
let inp = {m1: 30, m2: 17, m3: 20}

Object.keys(space).forEach(key => {
  console.log(key + ' - ' +  JSON.stringify(space[key])+ '- distance: ' +distance(inp, space[key])) 
})

function distance(A,B){
  return Math.sqrt(Math.pow(A.m1 - B.m1,2) + Math.pow(A.m2 - B.m2,2) + Math.pow(A.m3 - B.m3,2))
}

I tried simple implementation in javascript. A and C more close in 3 dimensional space. what do you mean by close?

Upvotes: 1

Related Questions