- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试实现 local search algorithm进行优化。我是 Lisp 的新手,所以这是我想出的代码(注意 FORMAT
s):
(defun local-search (solution)
"Improve solutions by changing the order of the points"
(flet ((improve (route)
(alexandria:shuffle (copy-tree route))))
(let ((best-solution solution)
(best-cost (solution-cost solution)))
(format t "~a~%~%" best-cost)
(format t "Counter, Current, Best~%")
(loop with counter = 0
while (< counter *max_iterations*)
do (let* ((new-solution (improve solution))
(new-cost (solution-cost new-solution)))
(format t "~a, ~a, ~a~%" counter new-cost best-cost)
(if (< new-cost best-cost)
(setf best-cost new-cost
best-solution new-solution
counter 0)
(incf counter)))
finally (return best-solution)))))
现在,这可能不是最惯用的方法,我有兴趣找到更好的方法来实现它,但我的主要问题是:SETF
不似乎正在更新我的参数。当我运行我的函数时,这是我得到的输出(*max_iterations*
设置为 10):
796.4436
Counter, Current, Best
0, 796.4436, 796.4436
1, 796.4436, 796.4436
2, 796.4436, 796.4436
3, 796.4436, 796.4436
4, 796.4436, 796.4436
5, 796.4436, 796.4436
6, 796.4436, 796.4436
7, 796.44354, 796.4436
0, 796.4436, 796.44354
1, 796.4436, 796.44354
2, 796.4436, 796.44354
3, 796.4436, 796.44354
4, 796.4436, 796.44354
5, 796.4436, 796.44354
6, 796.4436, 796.44354
7, 796.44366, 796.44354
8, 796.4436, 796.44354
9, 796.4436, 796.44354
从counter
中的重新启动,我可以看到代码找到了更好的解决方案,但它无法SETF
它们。我想这是因为一些我不知道的范围规则。我错过了什么?
对于上下文,这是 solution-cost
函数:
(defun solution-cost (solution)
"Calculate the total cost of a solution"
(reduce #'+ (mapcar #'cost solution)))
(defun cost (points &optional (acc 0))
"Calculate the total cost of a route"
(if (null points) acc
(cost (cdr points)
(+ acc (funcall *distance-fn* (first points) (second points))))))
完整代码是in this paste .我是这样调用它的:
(improve (initial-population *n_plants* (random-points)))
最佳答案
更新发生:
RRA-DARP 64 > (improve (initial-population *n_plants* (random-points)))
Was: 988.39277
Now is: 943.89106
Was: 943.89106
Now is: 895.5029
Was: 895.5029
Now is: 895.0123
900.5653
Counter, Current, Best
0, 900.56525, 900.5653 < old best
:SMALLER
0, 900.56525, 900.56525 < new best
1, 900.56525, 900.56525
2, 900.56525, 900.56525
3, 900.56525, 900.56525
4, 900.56525, 900.56525
5, 900.5653, 900.56525
6, 900.56525, 900.56525
7, 900.5653, 900.56525
8, 900.5653, 900.56525
9, 900.56525, 900.56525
Was: 895.0123
Now is: 900.56525
900.56525
关于scope - LET 和 LET* 中的范围有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33333368/
//Version A: var let = true; console.log(let);//true //Version B: let let = 0; //syntax Error: let i
//Version A: var let = true; console.log(let);//true //Version B: let let = 0; //syntax Error: let i
我在看a talk on JSON hijacking不到 2 分钟,已经有我不熟悉的 JavaScript。 let:let{let:[x=1]}=[alert(1)] 它似乎在 Edge 上工作并
(let ((x 2) (y 3) (let ((x 7) (z (+ x y))) (* z x))) 使用上面的代码,为什么答案是 35,而不是 70?在第二个 let
我正在编写一个以间隔作为参数并返回百分比错误的函数,但我坚持使用 let 或 let*。这是代码: 嵌套的 let 版本: (define (percent interval) (let (sta
我一直在阅读有关 Swift 中的Optional 的内容,并且看到过一些示例,其中 if let 用于检查Optional 是否包含值,如果包含值,则使用展开的值执行某些操作. 但是,我发现在 Sw
我正在尝试实现 local search algorithm进行优化。我是 Lisp 的新手,所以这是我想出的代码(注意 FORMATs): (defun local-search (solution
我一直在阅读有关 Swift 中的 Optionals 的文章,并且我看到了一些示例,其中 if let 用于检查 Optional 是否包含一个值,如果它包含 - 对未包装的值执行一些操作. 但是,
let () = Random.self_init();; let _ = Random.self_init ();; │- : unit = () 似乎“让()”什么也没返回? 真挚地! 最佳答案
有没有办法避免接下来的构造?一种在不向代码添加意图的情况下检查 null 的方法?我的意思是像 if (variableOne == null) return 但采用酷炫的 koltin 风格? va
什么时候使用 if-let 而不是 let 会使代码看起来更好以及对性能有影响吗? 最佳答案 我猜if-let当您想在代码的“then”部分引用 if 条件的值时,应该使用: 即而不是 (let [r
我有这些功能: (def i (atom {})) ;incremented/calculated file stats (defn updatei [n fic fos] (swap! i co
这个问题已经有答案了: Confused by the difference between let and let* in Scheme (2 个回答) 已关闭10 年前。 let、let* 和 l
因此,在 objective-c 、C/C++、.NET 以及我使用过的几乎所有其他语言中,您可以声明可以包含以前常量的常量,例如 #define PI 3.14159 #define HALFPI
在 Common Lisp 中,let 使用列表进行绑定(bind),即: (let ((var1 1) (var2 2)) ...) 虽然 Clojure 使用向量代替: (let
看下面两个使用相同代码的场景: 使用 IF LET: public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices er
这个问题在这里已经有了答案: Differences between "static var" and "var" in Swift (3 个答案) 关闭 5 年前。 class Foo {
我脑海中的例子是:什么更好? 示例 1: (define (foo x) ... (values a b c)) (let-values (((a b c) (foo 42))) .
考虑以下宏: (defmacro somemacro [] (list 'let ['somevar "Value"] 'somevar)) 展开它会产生以下结果: (macroexpand
可以来给我解释一下为什么必须使用 let !而不是在这种情况下让?只有当我包含 ! 时,测试才会通过。我以为我明白 let 会在该块中的每个“it”语句之前自动执行,但显然情况并非如此。 descri
我是一名优秀的程序员,十分优秀!