首页 \ 问答 \ 如何避免Python 2.7类实例的深度复制?(How to avoid deepcopy of Python 2.7 class instances?)

如何避免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
更新时间:2022-08-06 13:08

最满意答案

您的提示缺少{||} 模式元素 。 它应该是:

[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 ...
  • 您的提示缺少{||} 模式元素 。 它应该是: [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_ ...
  • 需要找到安装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在您的计算机上安装软件包的 ...
  • 你需要添加一个构造函数给枚举。 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 ...
  • 更改: 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 ...
  • 我遇到了同样的问题: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.
  • 首先需要打开VAXSettingsCells返回可选值。 如果使用原始值类型定义枚举,则枚举会自动接收一个初始值设定项,该初始值设定项接受原始值类型的值(作为名为rawValue的参数)并返回枚举大小写或nil if let cellID = VAXSettingsCells(rawValue: indexPath.row) { switch cellID { case .SwitchModeCell: // do whatever you want to do here default: break ...
  • 太多的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)); 由于这种类 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)