JavaScript best combination algorithm of complex data with multiple contraints best combination algorithm,find best combination algorithm

I guess we can say that this is very similar to an already asked question here (Optimisation/knapsack algorithm with multiple contraints in JavaScript), which hasn't yet an answer.

Let say we like javascript, C, C++, java. Any of this languages work for me. Anyone know algorithms to solve the problem?

PROBLEM: find the best subset of items which grants minimum cost and maximum number of objects, knowing that there's a limitation of resource:

var items = [
    {name: "Rome", cost: 1000, hours: 5, peoples: 5},
    {name: "Venice", cost: 200, hours:  1, peoples: 10},
    {name: "Torin", cost: 500, hours: 3, peoples: 2},
    {name: "Genova", cost: 700, hours: 7, peoples: 8},
    {name: "Rome2", cost: 1020, hours: 5, peoples: 6},
    {name: "Venice2", cost: 220, hours:  1, peoples: 10},
    {name: "Torin2", cost: 520, hours: 3, peoples: 2},
    {name: "Genova2", cost: 720, hours: 7, peoples: 4},
    {name: "Rome3", cost: 1050, hours: 5, peoples: 5},
    {name: "Venice3", cost: 250, hours:  1, peoples: 8},
    {name: "Torin3", cost: 550, hours: 3, peoples: 8},
    {name: "Genova3", cost: 750, hours: 7, peoples: 8}
];
var maxCost = 10000, maxHours = 100, maxPeoples = 50;
// find subset of items that minimize cost, hours and peoples
// and maximize number of items
// do not exceed max values!!!

IDEAS I HAD: I imagined I could do a solution to knapsack problem for each couple of cost (let call them "KPcost-hours", "KPhours-cost", "KPcost-peoples" etc.), which grants me the solution to optimize single costs. Then, if I'm lucky, take the common parts of this subsets and work from there... but i don't think it's a good path...

If you can give a script sample, or a pseudo-script sample, you're welcome! Thank you!

Answer:1

General solution

PROBLEM: find the best subset of items which grants minimum cost and maximum number of objects, knowing that there's a limitation of resource.

I see two optimization criteria here (I'll talk about the case where you want to minimize people, hours and cost below as well).
A possible approach is to build a program that will return a maximum Pareto-optimal set of solutions.

A Pareto set is a set of non-dominating solutions, meaning that for any two solutions S1 and S2, S1 does not dominate S2, and vice versa. A solution S1 dominates a solution S2 if it is better or equal than S2 regarding all criterias, and strictly better regarding at least one criteria.

For example, in your case, we can consider the following solutions:

S1: cost = 10, nb_objects = 4
S2: cost = 10, nb_objects = 7
S3: cost = 0, nb_objects = 0
S4: cost = 14, nb_objects = 6

Then our Pareto-optimal set of solutions is {S1, S3, S4}. That is because they do not dominate each other (for example, S1 does not dominate S4 because S4 is better regarding the number of objects). S2 is not part of the Pareto-optimal solution because it is dominated by both S1 and S4.

In the general case, Pareto-set are very hard to calculate, and can be extremely big. In your particular case, 4 criteria seem somehow reasonable, but it always can be surprising.
Here is a pseudocode on how to compute such a set of solutions:

Result = array of size nb_objects, initialized with empty sets
for i from 0 to total_nb_of_objects:
    for each feasible solution 'new_solution' to the problem with fixed number of objects:
        for each solution of Result[i]:
            if hours(new_solution) >= hours(solution) and  \
               cost(new_solution) >= cost(solution) and    \
               people(new_solution) >= people(solution):
                dominated = true
                break
        if not dominated:
            add new_solution to Result[i]
return Result

This little pseudocode here has more a value to try and understand the concept of Pareto Efficiency, I would not advice looping on all the feasible solutions of a variation to a knapsack problem (too costy).

Answer:2

A brute force approach by checking all combinations.

function getItems(array, constraints, [optimum, equal]) {
    function iter(index = 0, right = [], add) {

        function update() {
            if (!result.length || optimum(right, result[0])) return result = [right];
            if (equal(right, result[0])) result.push(right);
        }

        if (index >= array.length || !constraints.every(fn => fn(right))) return;
        if (add && right.length) update();

        var temp = right.find(({ ref }) => ref === array[index]),
            old = JSON.parse(JSON.stringify(right));

        if (temp) {
            temp.count++;
        } else {
            right.push({ count: 1, ref: array[index] });
        }

        iter(index, right, true);
        iter(index + 1, old);
    }

    var result = [];
    iter();
    return result;
}

const
    addBy = k => (s, { count, ref: { [k]: value } }) => s + count * value,
    addCount = (s, { count }) => s + count;

// find subset of items that minimize cost, hours and peoples
// and maximize number of items
// do not exceed max values!!!
var items = [{ name: "Rome", cost: 1000, hours: 5, peoples: 5 }, { name: "Venice", cost: 200, hours: 1, peoples: 10 }, { name: "Torin", cost: 500, hours: 3, peoples: 2 }, { name: "Genova", cost: 700, hours: 7, peoples: 8 }],
    maxCost = 10000,
    maxHours = 100,
    maxPeoples = 50,
    result = getItems(
        items,
        [
            array => array.reduce(addBy('cost'), 0) <= maxCost,
            array => array.reduce(addBy('hours'), 0) <= maxHours,
            array => array.reduce(addBy('peoples'), 0) <= maxPeoples
        ],
        [
            (a, b) => a.reduce(addCount, 0) > b.reduce(addCount, 0),
            (a, b) => a.reduce(addCount, 0) === b.reduce(addCount, 0)
        ]
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:3

Assuming one can only select 0 or 1 of each item, there are 2^12=4096 combinations possible. The number of feasible solutions is 3473. The number of non-dominated (or Pareto optimal) solutions is 83.

I used two different approaches:

  1. Enumerate all feasible solutions. Then filter out all dominated solutions (each solution must be better in at least one objective than all other solutions).

  2. Write a Mixed Integer Programming. It finds a solution, and adds a constraint that says: it should be better in at least one of the objectives than previous solutions. (Along the lines of this model).

Both methods find these 83 solutions. For this problem complete enumeration is faster.

Note that the number of Pareto optimal solutions can grow quickly. Here are some pictures of such a Pareto optimal set of a real-world design problem.

Note that there is no "single" best solution. All Pareto optimal solutions are optimal. Only when you make assumptions on the trade-offs between objectives, you can reduce the number of optimal solutions further.

Answer:4

Premise Let's say I have two different AMD-based AngularJS apps, each of them with their own sets of controllers, directives, services, etc. Each of them are bundled in their own dist/{app-name}.min....

Premise Let's say I have two different AMD-based AngularJS apps, each of them with their own sets of controllers, directives, services, etc. Each of them are bundled in their own dist/{app-name}.min....

  1. do containers share common dependencies

So, I have a page, where when a user holds left mouse button, Shrek images are drawn where his/her cursor is. The problem is, only one picture is created when the mouse is down, but I need an ENDLESS ...

So, I have a page, where when a user holds left mouse button, Shrek images are drawn where his/her cursor is. The problem is, only one picture is created when the mouse is down, but I need an ENDLESS ...

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in ...

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in ...

  1. formatting json string
  2. formatting json string in java
  3. formatting json string c#
  4. formatting json string js
  5. python string formatting json
  6. javascript formatting json string

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

  1. facebook game invite
  2. facebook game invite friends
  3. facebook instant game invite
  4. facebook instant game invite friend