Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表示给定范围内所有数字的最佳方法是什么?(一些限制)_Java_Multithreading_Algorithm_Recursion - Fatal编程技术网

Java 表示给定范围内所有数字的最佳方法是什么?(一些限制)

Java 表示给定范围内所有数字的最佳方法是什么?(一些限制),java,multithreading,algorithm,recursion,Java,Multithreading,Algorithm,Recursion,背景:制作一款类似于智囊团的游戏,但使用类固醇。目前正试图优化人工智能,并提高我的限制。让我解释一下: 对于我的AI算法,我需要生成某种数据结构,它可以表示给定0-a限制的所有可能的数字。我还需要从这种结构中删除数字,以反映可能的数字 现在在我的游戏中,我使用列表来表示一个数字本身。这样,我可以像任何基地,并根据事件在游戏中,我可以添加一个数字,这样的数字。我知道这不是最好的方法,但关于游戏,只有人工智能才是所有繁重工作都在做的地方 我创建了一个ArrayList>的方法,但问题是当限制大于In

背景:制作一款类似于智囊团的游戏,但使用类固醇。目前正试图优化人工智能,并提高我的限制。让我解释一下:

对于我的AI算法,我需要生成某种数据结构,它可以表示给定0-a限制的所有可能的数字。我还需要从这种结构中删除数字,以反映可能的数字

现在在我的游戏中,我使用列表来表示一个数字本身。这样,我可以像任何基地,并根据事件在游戏中,我可以添加一个数字,这样的数字。我知道这不是最好的方法,但关于游戏,只有人工智能才是所有繁重工作都在做的地方

我创建了一个ArrayList>的方法,但问题是当限制大于Integer.MAX_值时,我遇到了很多问题,导致程序冻结等等,还导致了巨大的内存问题。 我提取了一些代码,显示我当前如何生成所有序列。我还实现了糟糕的多线程工作,这样你们运行代码时就不必等待太久:

public class Recursive {

    private static List<List<Integer>> allSeqL = new ArrayList<List<Integer>>();
    private static final Object addPossibleSequenceLock = new Object();
    public static void main(String[] args){

        //Create Digits
        List<Integer> digits = new ArrayList<Integer>();
        int index = 0;
        while(10 > index){
            digits.add(index);
            index++;
        }
        System.out.println(digits.size()+"SIZE OF DIGITS");

        //Get Amount of Threads to run at once
        int recommendedAmountWT = Runtime.getRuntime().availableProcessors()*3/4;

        //Create Working List
        List<Thread> workTL = new ArrayList<Thread>();
        for(Integer number : digits){
            Thread t = new Thread(new Runnable(){
                private int number;
                private List<Integer> digits;
                public void run(){
                    int sizeOfSequence = 10;
                    boolean dupAllowed = true;
                    Integer[] sequence = new Integer[sizeOfSequence];
                    sequence[0] = number;
                    if(dupAllowed){
                        Recursive.recursiveAllPossible(new ArrayList<Integer>(digits), sequence, 1, dupAllowed);
                    }   else{
                        digits.remove(new Integer(number));
                        Recursive.recursiveAllPossible(digits, sequence, 1, dupAllowed);
                    }
                }
                public Runnable init(int number, List<Integer> digits){
                    this.number = number;
                    this.digits = digits;
                    return this;
                }
            }.init(number, new ArrayList<Integer>(digits)));
            workTL.add(t);
        }


        System.out.println(workTL.size()+"SIZE WORKTL");

        //Run only Recommended amount of work threads at once
        List<Thread> tempTL = new ArrayList<Thread>();
        while(workTL.size() != 0){
            int startThread = 0;
            while(workTL.size() != 0 && recommendedAmountWT > startThread){
                Thread t1 = workTL.get(0);
                tempTL.add(t1);
                workTL.remove(t1);
                startThread++;
                t1.start();
            }
            System.out.println(startThread);
            System.out.println(tempTL.size());
            System.out.println("##########################");
            for(Thread t : tempTL){
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            tempTL.clear();
        }
        System.out.println("TOTAL ALLSEQL: "+Recursive.allSeqL.size());
    }


    private static void recursiveAllPossible(List<Integer> digits, Integer[] sequence, int index, boolean dupAllowed){
        for(Integer number : digits){
            sequence[index] = number;
            if(sequence.length-1 > index){
                List<Integer> newPositionL = new ArrayList<Integer>(digits);
                if(!dupAllowed){
                    newPositionL.remove(number);
                }
                Recursive.recursiveAllPossible(newPositionL, sequence, new Integer(index+1), dupAllowed);
            }   else{
                Recursive.addToL(Arrays.asList(sequence));
            }
        }
    }

    /**
     * Method : Add to all sequence List
     * @param sequence : Sequence being added
     * Reason this has a lock is because when creating this list there are multiple threads
     */
    public static void addToL(List<Integer> sequence){
        synchronized(Recursive.addPossibleSequenceLock){
            Recursive.allSeqL.add(sequence);
            System.out.println(sequence);
        }
    }
}
公共类递归{
私有静态列表allSeqL=newarraylist();
私有静态最终对象addPossibleSequenceLock=新对象();
公共静态void main(字符串[]args){
//创建数字
列表位数=新的ArrayList();
int指数=0;
而(10>索引){
数字。添加(索引);
索引++;
}
System.out.println(digits.size()+“数字大小”);
//获取一次要运行的线程数
int recommendedAmountWT=Runtime.getRuntime().availableProcessors()*3/4;
//创建工作列表
List workTL=new ArrayList();
for(整数:位数){
线程t=新线程(新的可运行线程(){
私有整数;
私有列表数字;
公开募捐{
int-sizeOfSequence=10;
布尔值=真;
整数[]序列=新整数[sizeOfSequence];
序列[0]=编号;
如果(允许重复){
Recursive.recursiveAllable(新的ArrayList(数字),序列,1,允许重复);
}否则{
删除(新整数(数字));
递归。递归所有可能(数字,序列,1,允许重复);
}
}
公共可运行init(整数,列表数字){
这个数字=数字;
这个。数字=数字;
归还这个;
}
}.init(数字,新数组列表(数字));
增加(t);
}
System.out.println(workTL.size()+“size-workTL”);
//一次只运行推荐数量的工作线程
List testl=new ArrayList();
while(workTL.size()!=0){
int startThread=0;
while(workTL.size()!=0&&recommendedAmountWT>startThread){
线程t1=workTL.get(0);
添加(t1);
工作t1.移除(t1);
startThread++;
t1.start();
}
System.out.println(startThread);
System.out.println(testl.size());
系统、输出、打印(“\35;;
用于(线程t:TENTL){
试一试{
t、 join();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
明确的;明确的;
}
System.out.println(“TOTAL ALLSEQL:+Recursive.ALLSEQL.size());
}
私有静态void recursiveAllable(列表数字、整数[]序列、整数索引、允许布尔值){
for(整数:位数){
序列[索引]=编号;
if(sequence.length-1>索引){
List newPositionL=newarraylist(数字);
如果(!允许){
新位置L.移除(编号);
}
recursiveAllable(newPositionL,sequence,新整数(索引+1),允许重复);
}否则{
Recursive.addToL(Arrays.asList(sequence));
}
}
}
/**
*方法:添加到所有序列列表
*@param sequence:正在添加序列
*此列表具有锁定的原因是,创建此列表时有多个线程
*/
公共静态void addToL(列表顺序){
已同步(Recursive.addPossibleSequenceLock){
递归.allSeqL.add(序列);
系统输出打印项次(顺序);
}
}
}
基本上,这是通过将数字设置为序列数组的索引来实现的,循环直到无法再设置为止,然后将其添加到所有序列列表中。然后它循环回到上一个数字,查看是否还有其他数字要添加。添加和重复

与游戏的其他部分相比,AI真的不必遵循列表格式。然而,要比较数字,我必须将任何东西转换为列表或重写我的结果类,但我宁愿不这样做

因此,我应该做什么来解决这个问题的任何提示都将不胜感激。 我提出的解决方案是实现某种树。老实说,虽然我对树木了解不多,也没有信心。 另一个解决方案是,当数组列表达到Integer.MAX_值时,用新序列创建一个新的ArrayList,但这似乎是在掩盖问题


第三种解决方案是使用LinkedList,但也可以说是大便(我已经试过了)。

考虑到问题的规模,您是否考虑过使用稀疏矩阵

我想也许你可以表示一个不相交的非连续数字列表 仅作为范围列表:

my_ranges = [(0,10),(12,1500),(1502,25000000)]
这样,当你
insert():
for subrange in my_ranges:
   if 27 in my_ranges:
       # do nothing
   else:
       # build intermediate ranges with 27 included
return new_ranges

delete():
for subrange in my_ranges:
   if 27 in my_ranges:
       # split this range into two ranges, before and after 27
return new_ranges


exists():
for subrange in my_ranges:
   if 27 in my_ranges:
       return 1
return return 0