Skip to content

Commit

Permalink
[cps] [refactoring] Space<Problem, Branch> 改为 Space<Node, Edge>
Browse files Browse the repository at this point in the history
- 相应的 interface 函数名也要修改
  • Loading branch information
xieyuheng committed Aug 25, 2024
1 parent 2472e92 commit 3a3d69a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 53 deletions.
4 changes: 0 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[cps] [refactoring] `Space<Problem, Branch>` 改为 `Space<Node, Edge>`

- 相应的 interface 函数名也要修改

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

- Problem = Station
Expand Down
60 changes: 29 additions & 31 deletions src/solvers/cps/Path.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
import { type Space } from "./Space.js"

// 假设我们用 (p) 来表示 Problem,用 [b] 来表示 Branch
// 那么形如 (p0) -> [b0] -> (p1) -> [b1] -> (p2) 的 Path,
// 假设我们用 (n) 来表示 Node,用 [e] 来表示 Edge
// 那么形如 (n0) -> [e0] -> (n1) -> [e1] -> (n2) 的 Path,
// 会被实现为:
// - prefix: [[p0, b0], [p1, b1]]
// - current: p2
// - prefix: [[n0, e0], [n1, e1]]
// - current: n2

export type Path<Problem, Branch> = {
space: Space<Problem, Branch>
prefix: Array<[Problem, Branch]>
current: Problem
export type Path<Node, Edge> = {
space: Space<Node, Edge>
prefix: Array<[Node, Edge]>
current: Node
}

export function initialPath<Problem, Branch>(
space: Space<Problem, Branch>,
problem: Problem,
): Path<Problem, Branch> {
export function initialPath<Node, Edge>(
space: Space<Node, Edge>,
node: Node,
): Path<Node, Edge> {
return {
space,
prefix: [],
current: problem,
current: node,
}
}

export function extendPath<Problem, Branch>(
path: Path<Problem, Branch>,
branch: Branch,
problem: Problem,
): Path<Problem, Branch> {
export function extendPath<Node, Edge>(
path: Path<Node, Edge>,
edge: Edge,
node: Node,
): Path<Node, Edge> {
return {
space: path.space,
prefix: [...path.prefix, [path.current, branch]],
current: problem,
prefix: [...path.prefix, [path.current, edge]],
current: node,
}
}

export function pathHasLoop<Branch, Problem>(
path: Path<Branch, Problem>,
): boolean {
for (const [problem, _branch] of path.prefix) {
if (path.space.problemEqual(problem, path.current)) {
export function pathHasLoop<Edge, Node>(path: Path<Edge, Node>): boolean {
for (const [node, _edge] of path.prefix) {
if (path.space.nodeEqual(node, path.current)) {
return true
}
}

return false
}

export function ramify<Branch, Problem>(
path: Path<Problem, Branch>,
): Array<Path<Problem, Branch>> {
const branches = path.space.validBranches(path.current)
const newPaths = branches.map((branch) =>
extendPath(path, branch, path.space.branchApply(branch, path.current)),
export function ramify<Edge, Node>(
path: Path<Node, Edge>,
): Array<Path<Node, Edge>> {
const edges = path.space.validEdgees(path.current)

Check failure on line 51 in src/solvers/cps/Path.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Property 'validEdgees' does not exist on type 'Space<Node, Edge>'.
const newPaths = edges.map((edge) =>

Check failure on line 52 in src/solvers/cps/Path.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Parameter 'edge' implicitly has an 'any' type.
extendPath(path, edge, path.space.edgeApply(edge, path.current)),
)
return newPaths.filter((newPath) => !pathHasLoop(newPath))

Check failure on line 55 in src/solvers/cps/Path.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Parameter 'newPath' implicitly has an 'any' type.
}
20 changes: 10 additions & 10 deletions src/solvers/cps/Searching.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { type Path } from "./Path.js"
import { type Space } from "./Space.js"

export type Searching<Problem, Branch> = {
space: Space<Problem, Branch>
problem: Problem
queue: Array<Path<Problem, Branch>>
solved: Array<Path<Problem, Branch>>
export type Searching<Node, Edge> = {
space: Space<Node, Edge>
node: Node
queue: Array<Path<Node, Edge>>
solved: Array<Path<Node, Edge>>
}

export function createSearching<Problem, Branch>(
space: Space<Problem, Branch>,
problem: Problem,
): Searching<Problem, Branch> {
export function createSearching<Node, Edge>(
space: Space<Node, Edge>,
node: Node,
): Searching<Node, Edge> {
return {
space,
problem,
node,
queue: [],
solved: [],
}
Expand Down
10 changes: 5 additions & 5 deletions src/solvers/cps/Space.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Space<Problem, Branch> = {
export type Space<Node, Edge> = {
name: string
problemIsSolved: (problem: Problem) => boolean
problemEqual: (left: Problem, right: Problem) => boolean
validBranches: (problem: Problem) => Array<Branch>
branchApply: (branch: Branch, problem: Problem) => Problem
nodeIsEnd: (node: Node) => boolean
nodeEqual: (left: Node, right: Node) => boolean
branch: (node: Node) => Array<Edge>
edgeApply: (edge: Edge, node: Node) => Node
}
6 changes: 3 additions & 3 deletions src/solvers/cps/breadthFirstExplore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ramify } from "./Path.js"
import { type Searching } from "./Searching.js"

export function breadthFirstExplore<Problem, Branch>(
searching: Searching<Problem, Branch>,
export function breadthFirstExplore<Node, Edge>(
searching: Searching<Node, Edge>,
): void {
const path = searching.queue.shift()
if (path === undefined) return

if (searching.space.problemIsSolved(path.current)) {
if (searching.space.nodeIsSolved(path.current)) {

Check failure on line 10 in src/solvers/cps/breadthFirstExplore.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Property 'nodeIsSolved' does not exist on type 'Space<Node, Edge>'.
searching.solved.push(path)
} else {
const newPaths = ramify(path)
Expand Down

0 comments on commit 3a3d69a

Please sign in to comment.