Java implementation of a bilateral decision example _java

Source: Internet
Author: User
Tags abs

There are many problems in real life, such as how to achieve the profit maximization of merchandise trading? How to achieve the best overall effect of enrollment admission for college students? How does a patient doctor achieve the highest level of overall service? We can all turn him into a bilateral decision-making issue. Let's talk about your understanding of bilateral decisions.

Bilateral decisions--personal understanding

To help you understand, I use a simple example to describe what is a bilateral decision, the market now has 10 customers, namely A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, the market is a commodity, respectively, B0, B1, B2, B3, B4, B5, B6, B7 , B8, B9, now require that these 10 items are divided to the 10 customers, requirements of the overall satisfaction of the highest, of course, each customer's rating for each item is not the same, add m customer satisfaction for n pieces of goods for AMBN, then how to allocate these goods to make the overall satisfaction of the highest? A question like this is a bilateral decision.

Algorithm Introduction

At present, there are many implementation algorithms for bilateral decision-making, the following introduces a kind of their own thought (if there is a similar, pure coincidence), this algorithm is based on the previous write a genetic algorithm of the article thought. This algorithm requires the customer and the number of goods must be consistent, and is a one-to-one relationship, if the number is inconsistent or a pair of N (n is a specific value), we can build virtual goods (customers) to use this algorithm, I will briefly introduce the algorithm under the idea:

1 We first select a distribution scheme, here we do not assume that the initial distribution scheme is m pieces of merchandise to the M-customer;

2) We will compare step steps to set to 1;

3 Determines whether step exceeds the length of the array, if it exceeds the end algorithm, if it does not exceed the continuation of the next steps;

4 comparison of the two customers under step steps, assuming that their distribution scheme swap, if the balance after the swap is greater than the satisfaction before the reversal of the swap, otherwise remain the same, will be compared back to move a 4th step forward;

5 There is no distribution scheme that can be swapped on the step steps, and step steps plus 1;

6) Step 3rd to continue the execution.

In the above algorithm description, we focus on the 4th step, here we assume that the 1th customer distribution of goods is 1th, the 2nd customer distribution of goods is 2nd, their satisfaction with the goods are a1b1, a2b2, then the total satisfaction of these two customers is score1=a1b1+ A2B2, here we will their distribution scheme, that is, the 1th customer distribution of goods is 2nd, the 2nd customer distribution of goods is 1th, when their satisfaction with the goods are a1b2, A2B1, the total satisfaction of these two customers is SCORE2=A1B2+A2B1, If the SCORE1 is less than SCORE2, then we change the allocation policy, otherwise we keep the original allocation policy.

Java Code Analysis

For the above description may not be too specific, or do not know how to do with Java, the following we have to do the disassembly:

1 in the writing algorithm, we first need to define some constants, save the allocation scheme, and so on:

public class Twosideddecision { 
  private int num = 10;//individual number 
  private Boolean maxflag = true;//ask for maximum value 
  private in T[][] Scorearray;//ab The mutual evaluation score 
  private int[] decisionarray;//a choose b Way 
}   

Here is a Maxflag attribute that is used to identify whether our bilateral decision is to take the maximum value or the minimum value, true to the maximum, false to the minimum, the number of NUM to identify the individual, and the Scorearray array to represent the user's satisfaction with the product. Decisionarray is used to preserve the distribution of goods, decisionarray[0] indicates that the customer assigned the item numbered 0 is decisionarray[0];

2 before running the algorithm, we need to set the number of individuals

public void setnum (int num) { 
  if (num < 1) { 
    System.out.println ("Num must is greater than 0"); 
    return; 
  } 
  This.num = num; 
} 

3 Customer satisfaction rating of goods and determine the initial distribution plan

public void Setscorearray (int[][] scorearray) { 
  if (Scorearray = = null) { 
    System.out.println ("Scorearray is Null "); 
  } 
  if (!) ( scorearray.length = = num && scorearray[0].length = num)) { 
    System.out.println ("Scorearray ' must be" + num) ; 
  } 
  This.scorearray = Scorearray; 
  Decisionarray = new Int[num]; 
  Initial decision, diagonal for 
  (int i = 0; i < num; i++) { 
    Decisionarray[i] = i 
  } 
  Decision (); 
} 

4) then proceed to the 4th step of the algorithm description to confirm whether the allocation scheme is reversed

Private Boolean compare (int stepsize) {for (int i = 0; i < num-stepsize; i++) {int a1 = i; 
    int a2 = i + stepsize; 
    int b1 = decisionarray[a1]; 
    int b2 = decisionarray[a2]; 
    Original two score and int score1 = SCOREARRAY[A1][B1] + SCOREARRAY[A2][B2]; 
    int between1 = Math.Abs (SCOREARRAY[A1][B1]-scorearray[a2][b2]); 
    After the exchange of two scores and int score2 = SCOREARRAY[A1][B2] + SCOREARRAY[A2][B1]; 
    int between2 = Math.Abs (Scorearray[a1][b2]-scorearray[a2][b1]); if (Maxflag) {//FINAL score maximum if (score1 <= score2) {//The score after the exchange is not less than the before//after exchange of the score greater than the exchange before or after the exchange of the difference is greater than before the exchange of I 
          F (Score1 < Score2 | | | between2 > BETWEEN1) {decisionarray[a1] = B2; 
          DECISIONARRAY[A2] = B1; 
        return true; 
        }} else {//Last score min if (score1 >= score2) {//The score after the exchange is not less than the before//Exchange score greater than the exchange before or after the exchange of the difference is greater than before the exchange of 
     if (Score1 > Score2 | | | between2 > BETWEEN1) {decisionarray[a1] = B2;     DECISIONARRAY[A2] = B1; 
        return true; 
}}} return false; 
 }

The return value of this method is to confirm whether the switch occurs under that step, and if the step does not change, we can compare the next step. This completes the bilateral decision algorithm, below we look at the test results.

Run results

Max-value Test

Min-Value test

Complete code

 /** * @Description: Bilateral matching decision algorithm/package com.lulei.twosided.matching.decisionmaking; 
  
Import Com.lulei.util.JsonUtil; public class Twosideddecision {private int num = 10;//individual number private Boolean Maxflag = true;//ask for maximum value private int[] [] Scorearray;//ab score Private int[] decisionarray;//a Choose b the Way public boolean Ismaxflag () {return Maxflag 
  ; 
  } public void Setmaxflag (Boolean maxflag) {this.maxflag = Maxflag; 
    /** * @return * @Author: Lulei * @Description: Get final decision/public int[] Getdecisionarray () { 
  return decisionarray; /** * @return * @Author: Lulei * @Description: Get the score of the decision/public int getscoresum () {int 
    sum = 0; 
    for (int i = 0; i < num i++) {sum + = scorearray[i][decisionarray[i]]; 
  return sum; 
    /** * @param num * @Author: Lulei * @Description: Set the number of bilateral decision/public void setnum (int num) { 
 if (num < 1) {     SYSTEM.OUT.PRINTLN ("Num must be greater than 0"); 
    Return 
  } this.num = num; /** * @param scorearray * @Author: Lulei * @Description: Setting the evaluation between a-class and B-class individuals * * public void Setsco 
    Rearray (int[][] scorearray) {if (Scorearray = = null) {System.out.println ("Scorearray is null"); } if (! ( scorearray.length = = num && scorearray[0].length = num)) {System.out.println ("Scorearray ' must be" + Nu 
    m); 
    } This.scorearray = Scorearray; 
    Decisionarray = new Int[num]; 
    Initial decision, diagonal for (int i = 0; i < num; i++) {decisionarray[i] = i; 
  } decision (); /** * @Author: Lulei * @Description: Calculating optimal decision/private void Decision () {if (Scorearray = = Nu ll | | 
    Decisionarray = = null) {System.out.println ("please init Scorearray"); 
    for (int stepsize = 1; stepsize < num; stepsize++) {//Exchange while (compare (stepsize)) under specific step size; 
}  /** * @param stepsize * @return * @Author: Lulei * @Description: Specific step size comparison, return value to confirm whether the exchange * * 
      Private Boolean compare (int stepsize) {for (int i = 0; i < num-stepsize; i++) {int a1 = i; 
      int a2 = i + stepsize; 
      int b1 = decisionarray[a1]; 
      int b2 = decisionarray[a2]; 
      Original two score and int score1 = SCOREARRAY[A1][B1] + SCOREARRAY[A2][B2]; 
      int between1 = Math.Abs (SCOREARRAY[A1][B1]-scorearray[a2][b2]); 
      After the exchange of two scores and int score2 = SCOREARRAY[A1][B2] + SCOREARRAY[A2][B1]; 
      int between2 = Math.Abs (Scorearray[a1][b2]-scorearray[a2][b1]); 
          if (Maxflag) {//FINAL score maximum if (score1 <= score2) {//The score after the exchange is not less than the before/after exchange of the score greater than the exchange before or after the exchange of the difference is greater than before the exchange of 
            if (Score1 < Score2 | | | between2 > BETWEEN1) {decisionarray[a1] = B2; 
            DECISIONARRAY[A2] = B1; 
          return true; }} else {//Last score min if (score1 >=Score2) {//After the exchange of the score is not less than the exchange before//after the exchange of the score is greater than or after the exchange of the difference is greater than before the exchange if (Score1 > Score2 | | between2 > Betwee 
            N1) {DECISIONARRAY[A1] = B2; 
            DECISIONARRAY[A2] = B1; 
          return true; 
  }}} return false; public static void Main (string[] args) {int[][] Scorearray = {{0,1,2,3,4,5,6,7,8,9}, {1, 2, 
        3,4,5,6,7,8,9,0}, {2,3,4,5,6,7,8,9,0,1}, {3,4,5,6,7,8,9,0,1,2}, {4,5,6,7,8,9,0,1,2,3,}, 
        {5,6,7,8,9,0,1,2,3,4}, {6,7,8,9,0,1,2,3,4,5}, {7,8,9,0,1,2,3,4,5,6}, {8,9,0,1,2,3,4,5,6,7}, 
    {9,0,1,2,3,4,5,6,7,8}}; 
    Twosideddecision test = new Twosideddecision (); 
    Test.setnum (10); 
    Test.setmaxflag (FALSE); 
    Test.setscorearray (Scorearray); 
    SYSTEM.OUT.PRINTLN ("optimal decision"); 
    System.out.println (Jsonutil.parsejson (Test.getdecisionarray ())); 
    SYSTEM.OUT.PRINTLN ("decision scoring"); System.out.println (Test.getscoResum ()); 
 } 
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.