小编Lin*_*nus的帖子

Python解释器错误,x不带参数(给定1个)

我正在写一小段python作为家庭作业,我不会让它运行!我没有那么多的Python经验,但我知道很多Java.我正在尝试实现粒子群优化算法,这就是我所拥有的:

class Particle:    

    def __init__(self,domain,ID):
        self.ID = ID
        self.gbest = None
        self.velocity = []
        self.current = []
        self.pbest = []
        for x in range(len(domain)):
            self.current.append(random.randint(domain[x][0],domain[x][1])) 
            self.velocity.append(random.randint(domain[x][0],domain[x][1])) 
            self.pbestx = self.current          

    def updateVelocity():
    for x in range(0,len(self.velocity)):
        self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 * random.random()*(self.gbest[x]-self.current[x]) 


    def updatePosition():    
        for x in range(0,len(self.current)):
            self.current[x] = self.current[x] + self.velocity[x]    

    def updatePbest():
        if costf(self.current) < costf(self.best):
            self.best = self.current        

    def psoOptimize(domain,costf,noOfParticles=20, noOfRuns=30):
        particles = []
        for i in range(noOfParticles):    
            particle = Particle(domain,i)    
            particles.append(particle)    

        for i in range(noOfRuns): …
Run Code Online (Sandbox Code Playgroud)

python methods arguments object

60
推荐指数
3
解决办法
9万
查看次数

为什么我的位图排序不比我的mergesort快得多?

我正在编写Peals编程,第一篇论文涉及在已知范围内对数字进行排序.作为一种智能解决方案,他们提供实现位图,将输入文件中的所有数字设置为位图中的一个,然后简单地迭代它以打印结果.假设这应该比更传统的排序算法(如quicksort或mergesort)快得多.

为了测试这个,我用Java编写了自己的位图排序.当我发现使用合并排序的Unix排序命令仍然快得多时,我并不感到惊讶.我把它归结为它用C语言编写的事实,并且可能由一些非常聪明的人高度优化.

所以,然后我用Java编写了自己的合并排序.令我惊讶的是,我的BitmapSort速度更快,但只是略有增加.使用非常大的输入文件(+ -800000整数),bitmapsort只会快30%左右.

这是我的位图排序和位图实现:

import java.util.Scanner;
import java.io.FileReader;
import java.io.File;

class BitmapSort {
    Scanner sc;

    BitmapSort() throws Exception {
        sc = new Scanner(new File("numbers.txt"));
    }

    void start() {
        BitMap map = new BitMap(3000000);
        while (sc.hasNextInt()) {
            map.set(sc.nextInt());
        }
        for (int i = 0; i < 3000000; i++) {
            if (map.isSet(i)) {
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new BitmapSort().start();
    }
}


class BitMap {

    byte[] bits;
    int size;


    BitMap(int n) {
        size = …
Run Code Online (Sandbox Code Playgroud)

java algorithm big-o mergesort bitmap

4
推荐指数
1
解决办法
725
查看次数

使用星号字符作为Java扫描仪分隔符

嘿大家,这个问题对我来说似乎很傻,但我不能为我的生活找到答案.我想要做的就是扫描一个用星号(*)分隔的字符串.但是,当我尝试foo.useDelimiter("*");时,Java将星号解释为通配符,并使用每个字符作为分隔符...这显然不是我想要的.我尝试使用反斜杠作为转义字符,但这给了我编译器错误"非法转义字符".

这可能很简单,但我又一次不知道在哪里找到答案!

非常感谢!

莱纳斯

java delimiter java.util.scanner

2
推荐指数
1
解决办法
3066
查看次数

Java中的线程不是同时启动,而是按顺序启动

我在Java中编写多线程算法时遇到了一些问题.这是我得到的:

public class NNDFS implements NDFS {

//Array of all worker threads
private Thread[] threadArray; 

//Concurrent HashMap containing a mapping of graph-states and 
//algorithm specific state objects (NDFSState)
private ConcurrentHashMap<State, NDFSState> stateStore;

//Whether the algorithm is done and whether a cycle is found
private volatile boolean done;
private volatile boolean cycleFound;


/**
  Constructor that creates the threads, each with their own graph

  @param  file        The file from which we can create the graph
  @param  stateStore  Mapping between graph-states and state …
Run Code Online (Sandbox Code Playgroud)

java parallel-processing multithreading

0
推荐指数
1
解决办法
267
查看次数