首页 > 解决方案 > 查找数组的相邻解

问题描述

我有一名谈判助理和两名代理人。谈判助理创建报价,例如[1,2,3,4,5,6,7,8,9,10]。然后,他将这些报价发送给两个代理商,这些代理商自己计算价格并返回适合他们的真实报价或不适合他们的虚假报价。

我现在做什么,如果报价合适,那么我将其作为父母并尝试从这个解决方案中生成一个更好的子部分,换句话说,我从这个解决方案中搜索两个代理都说是的更好的解决方案社区。

我的方法如下,我将报价存储在一个int[]数组中并随机交换两个位置,然后询问代理商是否适合他们,因为是的,我使用之前创建的报价重复该步骤,如果不是,我接受双方都说的报价是的,然后尝试从这里找到一个邻域解决方案。


我还考虑过将循环分成三个部分。困难、中等和容易。他很难搜索远方的邻居,例如他交换所有地方。中等是他只交换了 50% 的名额,而简单的是他只交换了 2 个名额。

有没有人对我如何生成良好的邻居解决方案以找到更好的结果有更好的想法?


就我个人而言,我不太喜欢这个主意,而且我认为我并没有真正找到最好的邻居。有谁知道如何改进这一点或采用什么算法?

// Note: Quotes are not just 10 digits long but can be up to 100, 200, 300 long.
int[] initalOffer= new int[10]{1,2,3,4,5,6,7,8,9,10};
int[] newOffer = new int[initalOffer.length];
for(int i =0; i < 100000; i++) {
     newOffer = changeOffer(initalOffer);
     boolean voting_agentA = agentA.voting(newOffer, initalOffer);
     boolean voting_agentB = agentB.voting(newOffer, initalOffer);
     if(voting_agentA && voting_agentB) {
         initalOffer = Arrays.copyOf(newOffer, newOffer.length);
      }

}

 // How Could I improve this?
 // Example:
 // offer is [1,2,3,4,5,6,7,8,9,10]
 // look for two random numbers like 1 and 10
 // new offer is [10,2,3,4,5,6,7,8,9,1]
 // If both agent give a true back, the this is the new best offer
 // and after that after that it selects neighbors from it
 // If only one agent says false, then he takes the last best offer 
 public int[] changeOffer(int[] offer) {
            int[] newOffer = Arrays.copyOf(offer, offer.length);
            int posistion1 = 0;
            int posistion2 = 0;
            while (true) {
                posistion1 = (int) (Math.random() * offer.length) + 1;
                posistion2 = (int) (Math.random() * offer.length) + 1;
                if (posistion1 != posistion2) {
                    break;
                }
            }
            int randValue1 = offer[posistion1 - 1];
            int randValue2 = offer[posistion2 - 1];
            newOffer[posistion1 - 1] = randValue2;
            newOffer[posistion2 - 1] = randValue1;

    return newOffer;
}

标签: javaalgorithmnearest-neighbor

解决方案


推荐阅读