Skip to content

Commit

Permalink
[cps] [refactoring] rename ProblemSpace to Space
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 25, 2024
1 parent 21e9e24 commit 55cf3c0
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 25 deletions.
6 changes: 4 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[cps] `problem-solver/` -- 用单个的 `Solver` class 来实现 -- 而不是 `ProblemSpace` + `Searching`
[cps] [refactoring] rename ProblemPath to Path

[cps] [problem-space] [subway] `SubwayProblemSpace` & `createSubwayProblemSpace`
[cps] `solver/` -- 用单个的 `Solver` class 来实现 -- 而不是 `Space` + `Searching`

[cps] [problem-space] [subway] `SubwaySpace` & `createSubwaySpace`

- Problem = Station

Expand Down
46 changes: 42 additions & 4 deletions docs/diary/2024-08-25-space-and-searching-vs-solver.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,40 @@ date: 2024-08-25
在实现 Problem Space Model 的时候,
发现有两种组织 class 的方式:

- 一、用单个的 `Solver` class 来实现。
- 二、将 `Solver` 分解为 `ProblemSpace` + `Searching` 这两个 class 来实现。
一、用单个的 `Solver` class 来实现。

```typescript
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>>
solved: Array<ProblemPath<Problem, Branch>>
}
```
二、将 `Solver` 分解为 `Space` + `Searching` 这两个 class 来实现。
```typescript
export type Space<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
}

export type Searching<Problem, Branch> = {
space: Space<Problem, Branch>
problem: Problem
queue: Array<ProblemPath<Problem, Branch>>
solved: Array<ProblemPath<Problem, Branch>>
}
```
对比二者的 API:
Expand All @@ -17,14 +49,20 @@ searching.space.problemIsSolved(path.current)
```
一个 `Solver` class 的 API 更简单。
但是实际上描述一个问题空间的 interface 是 `ProblemSpace`
但是实际上描述一个问题空间的 interface 是 `Space`
`Searching` 是 search 运行是所需要的搜索状态。
书中的实现是只有 `ProblemSpace`
书中的实现是只有 `Space`
search 函数的搜索状态没有作为一个 `Searching` class 暴露出来。
我将同时做出这两种风格的实现,作为一个 API 设计的练习。
我怀疑,Space + Searching 是更好的设计,
但是 Space 的所有 interface 好像都是为了 search 来服务的,
所以也许 Space 和 Space 应该合并为 Solver。
另外,`Space<Problem, Branch>`
是否应该改为 `Space<Node, Edge>`
此时可以说 `Space` 并不只是为 search 服务的,
而是代表了一个更一般的图论意义上的空间。
这样空间和搜索两部分 interface 看起来就相互独立了。
11 changes: 0 additions & 11 deletions src/cps/problem-solver/Solver.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/cps/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>>
// solved: Array<ProblemPath<Problem, Branch>>
// }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ProblemSpace } from "./ProblemSpace.js"
import { type Space } from "./Space.js"

// 假设我们用 (p) 来表示 Problem,用 [b] 来表示 Branch,
// 那么形如 (p0) -> [b0] -> (p1) -> [b1] -> (p2) 的 Path,
Expand All @@ -7,13 +7,13 @@ import { type ProblemSpace } from "./ProblemSpace.js"
// - current: p2

export type ProblemPath<Problem, Branch> = {
space: ProblemSpace<Problem, Branch>
space: Space<Problem, Branch>
prefix: Array<[Problem, Branch]>
current: Problem
}

export function initialProblemPath<Problem, Branch>(
space: ProblemSpace<Problem, Branch>,
space: Space<Problem, Branch>,
problem: Problem,
): ProblemPath<Problem, Branch> {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { type ProblemPath } from "./ProblemPath.js"
import { type ProblemSpace } from "./ProblemSpace.js"
import { type Space } from "./Space.js"

export type Searching<Problem, Branch> = {
space: ProblemSpace<Problem, Branch>
space: Space<Problem, Branch>
problem: Problem
queue: Array<ProblemPath<Problem, Branch>>
solved: Array<ProblemPath<Problem, Branch>>
}

export function createSearching<Problem, Branch>(
space: ProblemSpace<Problem, Branch>,
space: Space<Problem, Branch>,
problem: Problem,
): Searching<Problem, Branch> {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ProblemSpace<Problem, Branch> = {
export type Space<Problem, Branch> = {
name: string
problemIsSolved: (problem: Problem) => boolean
problemEqual: (left: Problem, right: Problem) => boolean
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/cps/problem-space/index.ts → src/cps/space/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./breadthFirstExplore.js"
export * from "./ProblemPath.js"
export * from "./ProblemSpace.js"
export * from "./Space.js"
export * from "./Searching.js"

0 comments on commit 55cf3c0

Please sign in to comment.