CakePHP:不同文件/类之间的交互
我正在克隆一个商业学生管理系统。学生使用前端申请讲座,大学工作人员可以修改活动(时间、房间等)。 该应用程序的核心是向学生分配座位的算法。我已经在这里问过了: 如何实现大学讲座的座位分配算法
现在,我在这里找到了该算法的类: http://www.phpclasses.org/browse/file/10779.html
我将“class GA”放入应用程序/供应商中。我需要编写一个“类解决方案”,它代表一个对象(一个子对象,后来是进化过程的父对象)。
我还必须编写函数 mutate()、crossover() 和fitness()。健身根据是否存在超额预订的课程等计算解决方案的分数; crossover() 是疯狂的猴子性别函数,它从两个父母那里产生一个孩子,而 mutate() 在交叉后修改一个孩子。
现在,fitness()函数需要访问一些相关模型及其find()函数。它通过检查是否存在超额预订的课程或未实现的愿望来评估解决方案的适用性,并对其进行处罚。
我应该把 ga.php、solution.php 和这三个函数放在哪里? ga.php 必须访问函数,但函数必须访问模型。我也不想从fitness()函数中调用任何App::import(),因为当算法运行时它会被调用数千次。
希望有人能帮助我。预先感谢 =)
I'm cloning a commercial student management system. Students use the frontend to apply for lectures, uni staff can modify events (time, room, etc).
The core of the app will be the algortihm which distributes the seats to students. I already asked about it here:
How to implement a seat distribution algorithm for uni lectures
Now, I found a class for that algorithm here:
http://www.phpclasses.org/browse/file/10779.html
I put the 'class GA' into app/vendors. I need to write a 'class Solution', which represents one object (a child, and later a parent for the evolutionary process).
I'll also have to write functions mutate(), crossover() and fitness(). fitness calculates a score of a solution, based on if there are overbooked courses etc; crossover() is the crazy monkey sex function which produces a child from two parents, and mutate() modifies a child after crossover.
Now, the fitness()-function needs to access a few related models, and their find()-functions. It evaluates a solution's fitness by checking e.g. if there are overbooked courses, or unfulfilled wishes, and penalizes that.
Where would I put the ga.php, solution.php and the three functions? ga.php has to access the functions, but the functions have to access the models. I also don't want to call any App::import()'s from within the fitness()-function, because it gets called many thousand times when the algorithm runs.
Hope someone can help me. Thanks in advance =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你用的是PHP5吗?如果是这样,为什么不在 Fitness() 函数中执行类似的操作:
这样 App::import 仅在需要时才被调用。
*顺便说一句,如果该类名为 GA(全部大写),您应该将包含它的文件重命名为 g_a.php 以遵循 Cake 约定。*
Are you using PHP5? If so, why not do something like this inside the fitness() function:
That way App::import is only called when needed.
*By the way, if the class is called GA (all caps) you should rename the file that contains it to g_a.php to follow Cake conventions.*