How to use cost function

Help
2013-08-02
2013-08-15
  • SHANSHAN WANG

    SHANSHAN WANG - 2013-08-02

    Hi,

    I'm new in JaCoP and constraint programming. I mentioned that JaCoP provides cost function in search to get optimal solution? If so, could someone give me a simple example for using cost function in search? Because in Guide PDF, this part is omitted, and I have no idea about how to use cost function to optimize the result.

    Thanks in advance. I've learned a lot from this forum.

     
  • kris

    kris - 2013-08-02

    Hi!

    You define, using constraints, a variable that is equal a cost you want to minimize. For example, x is a maximum of a number of variable. Then you can minimize x. For more details please, refer to JaCoP examples included in the JaCoP distribution, for example Diet.java.

    Best,
    /Kris

     
  • SHANSHAN WANG

    SHANSHAN WANG - 2013-08-14

    Hi!

    I tried to maximize cost and I found the following code from the guide PDF:
    =============================A================================
    label.setSolutionListener(new CostListener<IntVar>());
    store.setLevel(store.level+1);
    boolean Result = true, optimalResult = false;
    while (Result) {
    Result = label.labeling(store, select);
    store.impose(new XgtC(cost, CostValue));
    optimalResult = optimalResult || Result;
    }
    store.removeLevel(store.level);
    store.setLevel(store.level1);
    =============================B================================
    public class CostListener<T extends="" Var=""> extends
    SimpleSolutionListener<T> {
    public boolean executeAfterSolution(Search<T> search,
    SelectChoicePoint<T> select) {
    boolean returnCode = super.executeAfterSolution(search,
    select);
    CostValue = cost.value();
    return returnCode;
    }
    }

    I copied the two pieces of code, but it cannot work.
    I have two questions:

    1. What are the "cost" object and the "CostValue" object in code B, and where should I define them?
    2. How can I get the CostValue in code A?

    Thank you for your help :)

     
  • kris

    kris - 2013-08-15

    Hi!

    You question is about restart search, that is described in section 5.1.1. In this search cost is a global variable that can be accessed both from CostListener and from the main code defining the search. The trick is that when you reach a solution you store the value of current cost in global integer variable CostValue. Then you use this value to constraint the next restarted search (constraint XgtC). In this way you achieve maximization.

    BTW, you do not need to use restart search to achieve this. You can use ordinary branch-and-bound search, defined for minimization to get maximization. The trick is rather simple. You minimize negated cost, that is -cost. You create this using constraint cost + negCost = 0. Minimization of negCost will, in fact maximize, cost.

    I hope this short explanation helps.

    Best
    /Kris

     
  • SHANSHAN WANG

    SHANSHAN WANG - 2013-08-15

    Thank you Kris!

    What a clever solution!!!

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks