Skip to content

Commit

Permalink
2024-08-25-space-and-searching-vs-solver.md
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 25, 2024
1 parent 936da1a commit 21e9e24
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
16 changes: 7 additions & 9 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[cps] `problem-solver/` -- 用单个的 `Solver` class 来实现 -- 而不是 `ProblemSpace` + `Searching`

[cps] [problem-space] [subway] `SubwayProblemSpace` & `createSubwayProblemSpace`

- Problem = `Station`
- Problem = Station

这里读起来有点问题
因为 Station 本身不是问题,
虽然这里写的是 Problem = Station
但是其实 Station 本身不是问题,
到达某个 Station 才是问题。

- Branch = `TakeLine: { oldStation, line, newStation }`
Expand All @@ -18,12 +20,8 @@

# later

[cps] [problem-space] `Searching` 的 API 应该是 `take` 之类的。

[cps] 也许用深圳的地铁作为例子,并且写一个模仿《百度地图》的前端出来试试。

[cps] 实现第三章的代数例子

[cps] `problem-solver/` -- 用单个的 `Solver` class 来实现 -- 而不是 `ProblemSpace` + `Searching`

- 因为 `ProblemSpace` + `Searching` 这两个 class 看来太复杂了,
比如 `searching.space.problemIsSolved(path.current)`
v.s. `solver.problemIsSolved(path.current)`
30 changes: 30 additions & 0 deletions docs/diary/2024-08-25-space-and-searching-vs-solver.md
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。
11 changes: 11 additions & 0 deletions src/cps/problem-solver/Solver.ts
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

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find name 'ProblemPath'.

Check failure on line 9 in src/cps/problem-solver/Solver.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Cannot find name 'ProblemPath'.
solved: Array<ProblemPath<Problem, Branch>>

Check failure on line 10 in src/cps/problem-solver/Solver.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find name 'ProblemPath'.

Check failure on line 10 in src/cps/problem-solver/Solver.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Cannot find name 'ProblemPath'.
}
1 change: 1 addition & 0 deletions src/cps/problem-solver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Solver.js"

0 comments on commit 21e9e24

Please sign in to comment.