【问题标题】:How could I use structure type in a function to calculate the time?如何在函数中使用结构类型来计算时间?
【发布时间】:2019-07-25 09:42:45
【问题描述】:

计算运行主代码时间的一种方法是我们将主代码用作函数。但如果代码包含结构类型,则会发生此错误。

using JuMP,CPLEX,Plots,CPUTime,DataStructures,StatsBase,Distributions

function NSG()
    #problem definition------------------------------------------------------------
    nvar=3;            # number of decision variables
    varmin=-4;       # lowerbound of variables
    varmax=4;        # upper bound of variables
    varsize=1:nvar;   # size of decision variables matrics
    nobj=length(MOP2(rand(Uniform(varmin,varmax),nvar))); # number of objective functions
    #-----NSGA-II parameters-------------------------------------------------------
    MaxIt=100;  #maximum iteration
    npop=50;  #population size
    pcrossover=0.7; #crossover percentage
    nc=2*round((pcrossover*npop)/2); #number of offsprings or parents
    pmutation=0.4;    #mutation percentage
    nm=round(pmutation*npop);    #number of mutants
    mu=0.02   #Mutation rate
    sigma=0.1*(varmax-varmin) #mutation step size

    #---intialization--------------------------------------------------------------
    struct individual
        position
        cost
        Rank
        Dominationset
        Dominatedcount
        Crowdingdostance
    end
    pop = [individual([],[],[],[],[],[])  for i in 1:npop]

    return pop
end
@time @CPUtime pop=NSG()


ERROR: error compiling nsga2: type definition not allowed inside a loca
l scope

这是MOP2函数

function MOP2(x)
    n=length(x);
    z1=1-exp(-sum((x-1/sqrt(n)).^2));
    z2=1-exp(-sum((x+1/sqrt(n)).^2));
    z=[z1;z2];
    return z
end

你能帮我解决一下吗?或者如何在不将代码放入函数类型的情况下计算主代码的时间。

非常感谢。

对于解决问题,它可能很有用。

我的问题解决如下:

using JuMP,CPLEX,CPUTime

struct individual
        position
        cost
        Rank
        Dominationset
        Dominatedcount
        Crowdingdostance
    end

function NSG()
    #problem definition------------------------------------------------------------
    nvar=3;            # number of decision variables
    varmin=-4;       # lowerbound of variables
    varmax=4;        # upper bound of variables
    varsize=1:nvar;   # size of decision variables matrics
    nobj=length(MOP2(rand(Uniform(varmin,varmax),nvar))); # number of objective functions
    #-----NSGA-II parameters-------------------------------------------------------
    MaxIt=100;  #maximum iteration
    npop=50;  #population size
    pcrossover=0.7; #crossover percentage
    nc=2*round((pcrossover*npop)/2); #number of offsprings or parents
    pmutation=0.4;    #mutation percentage
    nm=round(pmutation*npop);    #number of mutants
    mu=0.02   #Mutation rate
    sigma=0.1*(varmax-varmin) #mutation step size

    #---intialization--------------------------------------------------------------

    pop = [individual([],[],[],[],[],[])  for i in 1:npop]

    return pop
end
@time @CPUtime pop=NSG()

【问题讨论】:

  • 你能真正展示一个简化的可重现代码吗?还有你用什么来计时代码?
  • 经过您的编辑,这仍然不是一个最小、可重现的示例。请参阅stackoverflow.com/help/minimal-reproducible-example。但是,据我所知,您的问题应该在下面的答案中得到回答。
  • 谢谢。你是完全正确的。尽管它是代码的一部分,但它是可复制的。调试时,波纹管错误是结构类型(pop)。

标签: julia julia-jump


【解决方案1】:

如果您想停止某些代码所需的时间而不将其编写为函数,您可以使用 begin end

@time begin 
    struct ms 
       a 
    end 
    B = ms(rand(10,10,10)) 
    ms_sum = sum(B.a)
end 

【讨论】:

  • 非常感谢。但是CPU时间呢?怎么可能计算出来?
  • 我没看出问题,就我所见,和普通@time一样工作,只是放在begin-end块前面。
  • 确保time 没有问题。但是对于CPUTime?有什么方法可以计算吗?
  • 据我所知,我说CPUtime 与普通@time 的工作方式相同。你自己也试过吗?只需插入上面的 CPUTime 即可。
  • 我试过了。它不起作用。它给出了这个错误UndefVarError: @CPUTime not defined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
  • 2019-07-19
  • 2013-07-25
  • 1970-01-01
相关资源
最近更新 更多