CS 314 Project 2: Boolean Satisfiability SolverPython

Java Python CS 314

Project 2: Boolean Satisfiability Solver

1 Introduction

In this project, you will implement a Boolean satisfiability  (SAT) solver for OCaml.  The program takes a string representing a Boolean formula as input. This formula is in conjunctive normal form (CNF). CNF is a way of organizing logical statements used in mathematical logic and computer science.  A state- ment is in CNF if it is a conjunction (AND) of one or more clauses, where each clause is a disjunction (OR) of literals, and a literal is either a variable or the negation of a variable.

For example, a statement in CNF might look like this:

(a OR NOT b) AND (b OR c OR NOT d) AND (d OR NOT e)

In our project,  a literal also includes two boolean constants TRUE and FALSE. Your program should return a list of variable assignments that make the CNF formula true. For instance, to make (AND a b) true, both “a” and “b” need to be TRUE. To make (OR a b) true, there are three possible solutions, both “a” and “b” are TRUE, or “a” is TRUE, “b” is FALSE, or “b” is TRUE, “a” is FALSE.

The context-free grammar for the CNF formula is straightforward and de- fined in our project as below:

1.  < E >   ::=  (  < W >  ) AND  < E >  |  (  < W >  )

2.  < W >   ::=   < T >  OR  < W >  |   < T >

3.  < T >   ::=   < V >  | NOT < V >

4.  < V >    ::=  a  |  b |  ... | z | TRUE | FALSE

2 Input and Output of the Program

The input to the program is a string list. An example is below:

“( a OR b OR NOT c ) AND ( NOT a ) AND TRUE”

The program’s output is of the type (string, bool) list, which gives the first possible boolean value assignment of the variables to satisfy the CNF formula. An example is below:

[(“a”, false); (“b”, true); (“c”, false)]

If such an assignment doesn’t exist, please return a list of one tuple, as in the example shown below:

[(“error”, true)]

3    Basic Function Implementations

First, you need to break down the input string into a string list by eliminating   whitespaces. This function is already provided to you in the “project2 driver.ml” file, called “tokensListFromString (str : string)” .

Next,  you will need to implement a basic called  “partition”  in  the code package given to you.  The purpose of the “partition” function is to break down a string list with respect to a delimiter. It will help you get the input string list represented in a way that facilitates further processing.

Name                                             Input Type                                     Output Type

partition                                    (string list), string                                 string list list

getVariables                                     string list                                          string list

generateDefaultAssignments              string list                                    (string * bool) list

generateNextAssignments           (string * bool) list                          (string * bool) list * bool

lookupVar                             (string * bool) list, string                                   bool

Table 1: Required Functions

The input and output type of the  “partition” function is given in Table 1. An example of the input and output of the “partition” function is below:

Name:  partition

Input:  ["(";  "a";  "OR";  "NOT";  "b";  ")";  "AND";  "(";  "b";  ")"]  "AND" Output:  [["(";  "a";  "OR";  "NOT";  "b";  ")"];[  "(";  "b";  ")"]]

There are a few other basic functions you have to implement, listed below:

The next function you will have to implement is called  “getVariables” .  It takes a string list and gets the list of the variable names from the CNF. The input and output types are specified in Table  1.   It filters out the left/right parenthesis, AND/OR, TRUE/FALSE, and NOT items out of the string list representing the input. The output should not have duplicates. An example is given below:

Name:  getVariables

Input:  ["(";  "a";  "OR";  "NOT";  "b";  ")";  "AND";  "(";  "b";  ")"] Output:  ["a";  "b"]

You will also have to implement the function called “generateDefaultAssign- ments” .  It takes the list of variables,  and outputs a list of tuples, each tuple being a variable name and the boolean value “false” . It is used to initialize the set of variables to the “false” value. An example is given below:

Name:  generateDefaultAssignments

Input:  ["a";"b"]

Output:  [("a",  false);("b",  false)]

You will also have to implement the function called “generateNextAssign- ments” .  This function takes a list of string-boolean tuples, and returns an up- dated list of string-boolean tuples as if the singular binary number represented by the booleans is incremented by 1, as well as an additional boolean value carry for when it overflows.  Essentially, starting with the rightmost variable, if the checked variable is assigned to false, it is set to true.  If the checked variable is true, it is set to false and the algorithm carries to next variable to the left. The returned boolea CS 314 Project 2: Boolean Satisfiability SolverPython n value carry  is true only if the resulting binary number overflows the given space, i.e.  the algorithm reaches the leftmost variable and still carries.

Name:  generateNextAssignments

Input:  [("a",  false);("b",  false)]

Output:  ([("a",  false);("b",  true)],  false)

Input:  [("a",  false);("b",  true)]

Output:  ([("a",  true);("b",  false)],  false)

Input:  [("a",  true);("b",  true)]

Output:  ([("a",  false);("b",  false)],  true)

You will also have to implement the function called  “LookupVar” .   This function takes a assignment list of string-boolean tuples as well as a string, and returns the boolean value associated with the given string in the assignment list. Behavior for string values not in the assignment list will not be tested.

Name:  LookupVar

Input:  [("a",  false);("b",  true)]  "a"

Output:  false

4    Advanced Function Implementations

Now,  we will handle the boolean  satisfiability evaluation  function  and  data structures to represent the satisfiability function.  There are three functions you will have to implement here. Their types are specified below.

1. buildCNF: string list → (string * string) list list

2. evaluateCNF: (string * string) list list → (string * bool) list → bool

3.  satisfy: string list → (string * bool) list

We will describe each of the functions below.

4.1 The buildCNF function

This function builds a data structure to represent the CNF. It takes a string list as input, which you obtain from a string by calling “tokensListFromString” to break down a string.  Next, you can take advantage of the helper function “partition” built earlier, plus some further implemetnation to construct a list of (string*string) list, which represents a list of clauses from the CNF. Examples are shown below:

Input:  ["(";  "a";  "OR";  "NOT";  "b";  "OR";  "c";  ")";  "AND";  "(";  "b";  ")"] Output:  [[("a",  "");  ("b",  "NOT");  ("c",  "")];  [("b",  "")]]

Input:  ["(";  "NOT";  "a";  "OR";  "b";  ")";  "AND";  "(";  "NOT";  "b";  ")"] Output:  [[("a",  "NOT");  ("b",  "")];  [("b",  "NOT")]]

Note that one literal could be the negation of a variable or the variable itself. The tuple string*string in the list specifies that. If a NOT is decorating a variable, then the second item in the tuple is  “NOT”; otherwise, it is an empty string. In the above example, there are two clauses, hence, there is a list of two lists, each list representing a clause.  Each list representing a clause consists of the components divided by the “OR” operator in the clause.

4.2 The evalauteCNF function

This function takes the data structure of a CNF returned by the “buildCNF” function and an assignment list as input and returns a boolean variable.   It evaluates the variable assignment on the CNF; if it satisfies, it returns true; otherwise, it returns false. An example is given below:

Input:  [[("a",  "");  ("b";"NOT")];  [("b",  "")]]  [("a",  true),  ("b,  false")] Output:  false

Input:  [[("a",  "");  ("b";"NOT")];  [("b",  "")]]  [("a",  true),  ("b,  true")] Output:  true

Input:  [[("a",  "");  ("b";"NOT")];  [("b",  "")]]  [("a",  false),  ("b,  true")] Output:  false

4.3 The satisfy function

This function is the top-level function for the entire program. It takes a string list and returns the first variable assignment it finds that satisfies the CNF or an error (specified in Section 2). The order of trying variable assignment should be that you try the all-false assignment first, and then use  “getNextAssignment” similar to a carry adder to update the next variable assignment.   It  should terminate when it exhausts all possible variable assignments or finds the first one that works.  An example of input and output to the  “satisfy” function is given below.

Name:  satisfy

Input:  ["(";  "a";  "OR";  "NOT";  "b";  ")";  "AND";  "(";  "b";  ")"]

Output:  [("a",  true);  ("b",  true)]

5 Code Package

A code package is given to you.   You  will  need to implement everything in “project2.ml” . A few helper functions are provided in the “project2 driver.ml” . Please do not modify “project2 driver.ml” . All your own helper functions should go into “project2.ml” . We will test each function with independent inputs and outputs.  For instance, when you implement “getVariable” wrong, you will get no credit for  “getVariable” .  But we will use a correct  “getVariable” function to create inputs when we test others functions.  However, any functions called within other functions will behave defined by you, and are not guaranteed to work properly. For example, calling “getVariable” in the “satisfy” function used your implementation.

Please do not modify the type signature of each function that you are re- quired to implement.

Testing on An Interpreter

You can test your code in the OCaml interpreter. If you can type “ocaml” in the terminal on anilab machine, it will invoke the interpreter environment. You can load your program into the OCaml interactive toplevel, and invoke the functions from the toplevel.  In the OCaml interpreter, enter the following commands in OCaml top-level:

#  #use  "project2 .ml";;

It will load the code in “project2.ml” into the environment. After you load this file, you can then call functions you have implemented in “project2.ml” .  To use these functions in the  “project2 driver.ml” file, enter the following commands in the interpreter:

#  #mod_use  "project2 .ml";; #  #load  "str .cma";;

#  #use  "project2_driver .ml";;

Submission

Please only submit the “project2.ml” file to GradeScope         

在Vue中遇到 `No overload matches this call` 错误通常是由于传入的参数类型与期望的参数类型不匹配导致的。对于传入参数类型为 `[{ projectKey: string; userId: string; report: { url: string; reportType: string; }; switchs: { xhr: boolean; fetch: boolean; error: boolean; hashchange: boolean; history: boolean; whitescreen: boolean; performance: boolean; } }]` 不匹配 `[options: IOptions]` 类型的问题,可以按照以下思路解决: ### 1. 明确 `IOptions` 类型定义 首先需要明确 `IOptions` 类型的具体定义。可以在项目中查找 `IOptions` 类型定义的文件,一般可能在 `.d.ts` 文件或者接口定义文件中。 ### 2. 检查参数结构 对比传入参数和 `IOptions` 类型的结构是否一致。根据描述,传入的是一个数组,而 `IOptions` 可能期望的是单个对象,或者结构在内部属性上有差异。 ### 3. 类型转换 如果结构基本一致,但类型不匹配,可以进行类型转换。例如,如果 `IOptions` 中某个属性期望是数字类型,而传入的是字符串类型,可以进行相应的转换。 ### 4. 代码示例 假设 `IOptions` 类型定义如下: ```typescript interface IOptions { projectKey: string; userId: string; report: { url: string; reportType: string; }; switchs: { xhr: boolean; fetch: boolean; error: boolean; hashchange: boolean; history: boolean; whitescreen: boolean; performance: boolean; }; } ``` 而传入的参数是一个数组,我们可以取数组的第一个元素作为 `IOptions` 类型的对象: ```typescript const inputParams: [{ projectKey: string; userId: string; report: { url: string; reportType: string; }; switchs: { xhr: boolean; fetch: boolean; error: boolean; hashchange: boolean; history: boolean; whitescreen: boolean; performance: boolean; } }] = [ { projectKey: 'testKey', userId: 'testUser', report: { url: 'testUrl', reportType: 'testType' }, switchs: { xhr: true, fetch: true, error: true, hashchange: true, history: true, whitescreen: true, performance: true } } ]; const options: IOptions = inputParams[0]; // 调用函数,假设函数名为 someFunction // someFunction(options); ``` ### 5. 类型断言 如果确定传入的参数在结构上是兼容的,但类型检查仍然报错,可以使用类型断言来绕过类型检查: ```typescript const options = inputParams[0] as IOptions; ``` 通过以上步骤,应该可以解决 `No overload matches this call` 错误。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值