如何避免Python 2.7类实例的深度复制?(How to avoid deepcopy of Python 2.7 class instances?)
我正在运行一个演化算法来优化单独模拟环境中的函数(也用Python编写)。 模拟环境本身是一个python类实例,并且计算量很大(每次运行约4秒)并占用大约1Gb的内存。 由于我在这个模拟环境中生成大约十万个不同的函数来测试,我需要一种方法尽可能多地离线模拟。
我能做的是预先计算大部分模拟(即初始化类的一个实例,然后运行一些类函数的子集),然后在算法中仅针对一个小组件(例如,其余的几个函数)班上)。 这通常将评估时间缩短到约1秒,这显着改善了运行时间。
但是,每次将新函数传递到模拟类实例时,函数都会更改类实例(从而影响下一个函数求值的结果)。 我可以通过在每次评估之前对整个类实例进行深度复制来防止这种情况发生,但最终比每次运行整个模拟要慢。
有没有办法避免在每个阶段深入复制整个班级,但要获得相同的结果? 基本上每个功能评估都需要在完全相同的类实例上执行。
谢谢
I'm running an evolutionary algorithm to optimise functions within a separate simulation environment (also written in Python). The simulation environment itself is a python class instance, and is computationally expensive (~4 seconds per run) and takes up about 1Gb of memory. As I'm generating about a hundred thousand different functions to test in this simulation environment, I need a way to take as much of the simulation off-line as possible.
What I can do is pre-compute the majority of the simulation (i.e. initialise an instance of the class and then run some sub-set of class functions) and then fire in the algorithm for only a small component (e.g. the remaining few functions within the class). This in general reduces the evaluation time to ~1 sec, which improves the run-time significantly.
However, every time a new function is passed into the simulation class instance, the class instance is changed by the function (and thus affects the outcome of the next function evaluation). I can prevent this from happening by doing a deepcopy of the entire class instance before every evaluation, but that ends up being slower than running the entire simulation each time.
Is there a way to avoid deepcopying the entire class at each stage but to get the same result? Essentially each function evaluation needs to be performed on exactly the same class instance.
Thanks
原文:https://stackoverflow.com/questions/36796993
最满意答案
您的提示缺少
{||}
模式元素 。 它应该是:[Prompt("Please select the appropriate category for your problem (optional). {||}")]
Your prompt is missing the
{||}
pattern element. It should be:[Prompt("Please select the appropriate category for your problem (optional). {||}")]
相关问答
更多-
Object.keys(training); 仅在Chrome中受支持 更改 function choices() { return Object.keys(training); } 至 function choices() { var keys = []; for(var key in this) keys.push(key); return keys; } Object.keys(training); is only supported in Chrome Change f ...
-
表单流枚举提示错误(Form Flow enum prompt error)[2023-07-06]
您的提示缺少{||} 模式元素 。 它应该是: [Prompt("Please select the appropriate category for your problem (optional). {||}")] Your prompt is missing the {||} pattern element. It should be: [Prompt("Please select the appropriate category for your problem (optional). {||}") ... -
一旦表单由用户完成/填写, EnquiryFormSubmitted被触发,这样您就可以获得用户输入的值以及应该进行API调用的位置。 private async Task EnquiryFormSubmitted(IDialogContext context, IAwaitable
result) { var enquiry = await result; //All the user entered details are ... -
您应该使您的函数成为静态成员。 您的类声明如下所示: class AudioEngine { ... enum mad_flow input(void *data, mad_stream *stream); ... }; 你应该这样做: class AudioEngine { ... static enum mad_flow input(void *data, mad_stream *stream); ... }; mad_decoder_ ...
-
Python导入枚举错误(Python import enum error)[2023-06-07]
需要找到安装enum的路径。 尝试这个: import os path = os.path.dirname(.__file__) print path 您可以使用 math模块来查找包的路径。 在Mac上,通常是path = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/浏览到导演并寻找enum 。 如果不存在,请找出pip在您的计算机上安装软件包的 ... -
Java枚举给出一个错误?(Java enum giving an error?)[2021-11-11]
你需要添加一个构造函数给枚举。 public enum Status { STATUS_OPEN(0), STATUS_STARTED(1), STATUS_INPROGRESS(2), STATUS_ONHOLD(3), STATUS_COMPLETED(4), STATUS_CLOSED(5); private final int number; Status(int number) { this.number = num ... -
有什么错误的枚举(Something wrong with enum)[2021-09-30]
更改: template至 template gcc上的错误更具描述性: prog.cpp: In member function ‘void X ::set_direction(Direction)’: prog.cpp:11: error: declaration of ‘Direction dir’ prog.cpp:3: error: shadows template pa ... -
如何使用Mongoid Enum(How to use Mongoid Enum)[2023-10-02]
我遇到了同样的问题:new是枚举的有效符号之一。 当我更改:新的:默认,它开始工作。 I had the same problem when :new is one of the valid symbols for the enum. When I changed :new to :default, it began working. -
使用enum swift问题(Use enum swift issue)[2022-01-12]
首先需要打开VAXSettingsCells返回可选值。 如果使用原始值类型定义枚举,则枚举会自动接收一个初始值设定项,该初始值设定项接受原始值类型的值(作为名为rawValue的参数)并返回枚举大小写或nil if let cellID = VAXSettingsCells(rawValue: indexPath.row) { switch cellID { case .SwitchModeCell: // do whatever you want to do here default: break ... -
在Enum值上使用Where(Using Where on Enum Values)[2022-12-14]
太多的LINQ思维方式,所以有时候我们会忘记它们来自哪里:)从第一个.NET版本开始, Enum.GetValues(typeof(MyEnum))实际上返回MyEnum[] ,所以你需要的不是OfType ,也不是Cast ,但简单的C# cast : string s = string.Join(", ", ((MyEnum[])Enum.GetValues(typeof(MyEnumType))) .Where(x => (MyIntValue & (int)x) != 0)); 由于这种类 ...