Redian新闻
>
怎样才能使一个算法用于不同的数据结构?
avatar
怎样才能使一个算法用于不同的数据结构?# Java - 爪哇娇娃
w*r
1
I stuck at a problem in how to implement a algorithm(simulate annealling)
in java.
I want to apply this algorithm to different datastructure(not different
object, I find visitor pattern cannot be used here). For example, pass a data
structure A to algorithm F, F will generate many A and does the algorithm on
A. It need to mutate A, clone A, get fitness of A. If we pass another data
structure B to F, then F will do all those things on B. Suppose B has those
methods including mutate, clone,
avatar
g*g
2
Generic programming.
A typic example is Java's collection framework and Comparable interface.
function f know no details about A or B, you just pass f(W), and
call methods in W in f.

【在 w********r 的大作中提到】
: I stuck at a problem in how to implement a algorithm(simulate annealling)
: in java.
: I want to apply this algorithm to different datastructure(not different
: object, I find visitor pattern cannot be used here). For example, pass a data
: structure A to algorithm F, F will generate many A and does the algorithm on
: A. It need to mutate A, clone A, get fitness of A. If we pass another data
: structure B to F, then F will do all those things on B. Suppose B has those
: methods including mutate, clone,

avatar
w*r
3
Thank you very much

annealling)
different
data
on
those
and
or
many
C++里

【在 g*****g 的大作中提到】
: Generic programming.
: A typic example is Java's collection framework and Comparable interface.
: function f know no details about A or B, you just pass f(W), and
: call methods in W in f.

avatar
m*t
4

[snip]
I believe the Factory pattern is what you are looking for.

【在 w********r 的大作中提到】
: Thank you very much
:
: annealling)
: different
: data
: on
: those
: and
: or
: many

avatar
c*t
5
1. factory pattern
2. java's build-in reflection.
However, since you are doing SA. It is probably not efficient to create
an entire new list each time an iteration is run. What you can do is
to add an reset() function such that you can re-use object A and B.
If both A and B are the same object type, what you can do is to swap
A and B. B reset after each iteration. This way, redundant object
creation can be minimized (and save you memory/performance). You can
do the similar thing to each ind

【在 w********r 的大作中提到】
: Thank you very much
:
: annealling)
: different
: data
: on
: those
: and
: or
: many

avatar
w*r
6
谢谢,I am always puzzled by factory.
那么我可以把SA作为一个factory传到A 或B 中
准备instantiate两个SA, 一个给A,一个给B. 每次construct A 或 B 的时候都reset
SA.
这样可以吗?

annealling)
different
data
on
those
and
or
many
C++里

【在 c*****t 的大作中提到】
: 1. factory pattern
: 2. java's build-in reflection.
: However, since you are doing SA. It is probably not efficient to create
: an entire new list each time an iteration is run. What you can do is
: to add an reset() function such that you can re-use object A and B.
: If both A and B are the same object type, what you can do is to swap
: A and B. B reset after each iteration. This way, redundant object
: creation can be minimized (and save you memory/performance). You can
: do the similar thing to each ind

avatar
L*r
7
template method pattern.. stupid..

【在 w********r 的大作中提到】
: 谢谢,I am always puzzled by factory.
: 那么我可以把SA作为一个factory传到A 或B 中
: 准备instantiate两个SA, 一个给A,一个给B. 每次construct A 或 B 的时候都reset
: SA.
: 这样可以吗?
:
: annealling)
: different
: data
: on

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。