-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-08-25-space-and-searching-vs-solver.md
- Loading branch information
Showing
4 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Space and Searching v.s. Solver | ||
date: 2024-08-25 | ||
--- | ||
|
||
在实现 Problem Space Model 的时候, | ||
发现有两种组织 class 的方式: | ||
|
||
- 一、用单个的 `Solver` class 来实现。 | ||
- 二、将 `Solver` 分解为 `ProblemSpace` + `Searching` 这两个 class 来实现。 | ||
|
||
对比二者的 API: | ||
|
||
```typescript | ||
solver.problemIsSolved(path.current) | ||
searching.space.problemIsSolved(path.current) | ||
``` | ||
|
||
一个 `Solver` class 的 API 更简单。 | ||
但是实际上描述一个问题空间的 interface 是 `ProblemSpace`, | ||
而 `Searching` 是 search 运行是所需要的搜索状态。 | ||
|
||
书中的实现是只有 `ProblemSpace`, | ||
search 函数的搜索状态没有作为一个 `Searching` class 暴露出来。 | ||
|
||
我将同时做出这两种风格的实现,作为一个 API 设计的练习。 | ||
|
||
我怀疑,Space + Searching 是更好的设计, | ||
但是 Space 的所有 interface 好像都是为了 search 来服务的, | ||
所以也许 Space 和 Space 应该合并为 Solver。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type Solver<Problem, Branch> = { | ||
name: string | ||
problemIsSolved: (problem: Problem) => boolean | ||
problemEqual: (left: Problem, right: Problem) => boolean | ||
validBranches: (problem: Problem) => Array<Branch> | ||
branchApply: (branch: Branch, problem: Problem) => Problem | ||
|
||
problem: Problem | ||
queue: Array<ProblemPath<Problem, Branch>> | ||
Check failure on line 9 in src/cps/problem-solver/Solver.ts
|
||
solved: Array<ProblemPath<Problem, Branch>> | ||
Check failure on line 10 in src/cps/problem-solver/Solver.ts
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Solver.js" |