Finding the most suitable objects by set of parameters in java

Moses

I have a set of objects. This objects calculate some numbers based on request parameters. Let's call them calculators. Each calculator has description where specified type of requests that this calculator the most suitable for. For example,

Calculator1 : with this parameters : price > 10, gender = male, geo_id = 1, 2 or 3.
Calculator2 : with this parameters : price < 5, gender = male,  geo_id = 1, 2. 

For request : price = 11, gender = male, geo_id = 2 I should get calculator1 like the most suitable and then calculator2.

For request : price = 4, gender = male, geo_id = 2 I should get calculator2 and then calculator1.

For request : price = 3, gender = female, geo_id = 5 I should get only the second one.

Now I'm doing it with Lucene, but it's not really fit for this task. Can you recommend me some library or approach?

Jakub Kotowski

My suggestion would be to use a comparator. See a sketch of the classes below.

import java.util.HashMap;
import java.util.Map;

public abstract class Calculator {
    public static Map<String, Integer> weights;
    static {
        weights = new HashMap<String, Integer>();
        weights.put("price", 10);
        weights.put("gender", 2);
        weights.put("geo", 5);
    }

    public abstract int calculate(Map<String, Integer> request);
    public abstract int fitnessFor(Map<String, Integer> request);
}

You can use the weights to adjust relative importance of the individual request parameters.

import java.util.Map;

public class Calculator1 extends Calculator {

    public int calculate(Map<String, Integer> request) {
        return -1;
    }

    @Override
    public int fitnessFor(Map<String, Integer> request) {
        int fitness = -1;
        Integer price = request.get("price");
        if (price == null)
            return fitness;

        if (price > 10)
            fitness += weights.get("price");

        return fitness;
    }

    public String toString() { return "Calculator1"; }
}

Calculator1 cares only about the pricey items.

import java.util.Map;

public class Calculator2 extends Calculator {

    public int calculate(Map<String, Integer> request) {
        return -1;
    }

    @Override
    public int fitnessFor(Map<String, Integer> request) {
        int fitness = -1;
        Integer price = request.get("price");
        if (price == null)
            return fitness;

        if (price < 5)
            fitness += weights.get("price");

        Integer gender = request.get("gender");
        if (gender == null)
            return fitness;

        if (gender == 1)
            fitness += weights.get("gender");

        return fitness;
    }

    public String toString() { return "Calculator2"; }  
}

Calculator2 cares about the less pricey items esp. if they are for gender 1.

The comparator just compares Calculators by their fitness relative to the request:

import java.util.Comparator;
import java.util.Map;

public class CalcComparator implements Comparator<Calculator> {
    private Map<String, Integer> request;

    public CalcComparator(Map<String, Integer> request) {
        this.request = request;
    }

    @Override
    public int compare(Calculator c1, Calculator c2) {
        int c1Fitness = c1.fitnessFor(request);
        int c2Fitness = c2.fitnessFor(request);

        if (c1Fitness == c2Fitness)
            return 0;

        if (c1Fitness < c2Fitness)
            return 1;

        return -1;
    }
}

Try it out with:

public class Main {

    public static void main(String[] args) {
        Map<String, Integer> request = new HashMap<String, Integer>();
        request.put("price", 5);
        request.put("gender", 1);

        List<Calculator> calculators = new ArrayList<Calculator>();
        calculators.add(new Calculator1());
        calculators.add(new Calculator2());

        Collections.sort(calculators, new CalcComparator(request));

        System.out.println("For request: "+request);
        for (Calculator c : calculators) {
            System.out.println("\t"+c.toString() + "( fitness " + c.fitnessFor(request) + ")");
        }
    }
}

This is just a sketch to illustrate the idea. You will probably want to introduce an enum for the request parameters, maybe introduce a Request class, most likely change completely how fitness is computed, make some of the fields private and encapsulate them, etc.

The advantage is that you easily get an ordering of all the Calculators based on their fitness for the request.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding matching objects in Java

From Dev

Finding matching objects in Java

From Dev

Finding a pattern in a set of values in Java

From Dev

Finding all partitions of a set in Java

From Dev

Most suitable combinatorics algorithm

From Dev

Finding the most similar documents (nearest neighbours) from a set of documents

From Dev

Finding the sum of values in a list of objects matching certain parameters

From Dev

Finding All Objects that are a Child of a Given Type in Java

From Dev

Updating the objects of a Set in Java

From Dev

Finding 'n' most frequent words from a file using Java?

From Dev

Improving the Java 8 way of finding the most common words in "War and Peace"

From Dev

Using Java multithreading, what is the most efficient to coordinate finding the best result?

From Dev

Java Filters Finding the most expensive house in each City

From Dev

Retrieve N most relevant objects in Java TreeMap

From Dev

Finding center of set of coordinates using Java

From Dev

Finding the top most parent

From Dev

Finding most repeated names

From Dev

Set Collection for mutable objects in Java

From Dev

Java how to set objects type

From Dev

Fast way of finding most and least significant bit set in a 64-bit integer

From Dev

Is there a way to compare two objects in a List and combine their values that is most optimal?(Java)

From Dev

Most efficient practice to access objects in other classes: Java

From Dev

What is the most suitable alternative for Linked List?

From Dev

Which data structure is most suitable to implement a Dictionary?

From Dev

How to identify the most suitable Design Pattern for a scenario

From Dev

Most suitable CMS Framework for a company of 500 employees

From Dev

What is the most suitable way to save that data in android

From Dev

calculating area of most suitable raster habitat in R

From Dev

Most suitable ionic component for linked inputs or a plugin

Related Related

  1. 1

    Finding matching objects in Java

  2. 2

    Finding matching objects in Java

  3. 3

    Finding a pattern in a set of values in Java

  4. 4

    Finding all partitions of a set in Java

  5. 5

    Most suitable combinatorics algorithm

  6. 6

    Finding the most similar documents (nearest neighbours) from a set of documents

  7. 7

    Finding the sum of values in a list of objects matching certain parameters

  8. 8

    Finding All Objects that are a Child of a Given Type in Java

  9. 9

    Updating the objects of a Set in Java

  10. 10

    Finding 'n' most frequent words from a file using Java?

  11. 11

    Improving the Java 8 way of finding the most common words in "War and Peace"

  12. 12

    Using Java multithreading, what is the most efficient to coordinate finding the best result?

  13. 13

    Java Filters Finding the most expensive house in each City

  14. 14

    Retrieve N most relevant objects in Java TreeMap

  15. 15

    Finding center of set of coordinates using Java

  16. 16

    Finding the top most parent

  17. 17

    Finding most repeated names

  18. 18

    Set Collection for mutable objects in Java

  19. 19

    Java how to set objects type

  20. 20

    Fast way of finding most and least significant bit set in a 64-bit integer

  21. 21

    Is there a way to compare two objects in a List and combine their values that is most optimal?(Java)

  22. 22

    Most efficient practice to access objects in other classes: Java

  23. 23

    What is the most suitable alternative for Linked List?

  24. 24

    Which data structure is most suitable to implement a Dictionary?

  25. 25

    How to identify the most suitable Design Pattern for a scenario

  26. 26

    Most suitable CMS Framework for a company of 500 employees

  27. 27

    What is the most suitable way to save that data in android

  28. 28

    calculating area of most suitable raster habitat in R

  29. 29

    Most suitable ionic component for linked inputs or a plugin

HotTag

Archive