Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>votingNew to Visual Studio Code? Get it now.
voting

voting

H1

|
1 install
| (0) | Free
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

DFS Water Jug

% Initial state start((0,0)).

%Goal State goal((2,0)).

% Move rules move((X,Y),(5,Y)):- X<5. % Fill 5L jug move((X,Y),(X,4)):- Y<4. % fill 4L jug move((X,Y),(0,Y)):- X>0. % Empty 5L jug move((X,Y),(X,0)):- Y>0. % Empty 4L jug

%Pour from 5L to 4L move((X,Y),(NX,NY)):- X>0, Y<4, T is min(X,4-Y), NX is X - T, NY is Y + T.

%Pour from 4L to 5L move((X,Y),(NX,NY)):- Y>0, X<5, T is min(Y,5-X), NX is Y - T, NY is X + T.

To run: start(S). move((0,0), Next). move((5,0), Next). move((1,4), Next). move((2,3), Next).

BFS TicTacToe

% Show board show([A,B,C,D,E,F,G,H,I]):- write(A), write(' '),write(B), write(' '), write(C), nl, write(D), write(' '),write(E), write(' '), write(F), nl, write(G), write(' '),write(H), write(' '), write(I), nl.

% Put X put([-,B,C,D,E,F,G,H,I],1,x,[x,B,C,D,E,F,G,H,I]). put([A,-,C,D,E,F,G,H,I],2,x,[A,x,C,D,E,F,G,H,I]). put([A,B,-,D,E,F,G,H,I],3,x,[A,B,x,D,E,F,G,H,I]). put([A,B,C,-,E,F,G,H,I],4,x,[A,B,C,x,E,F,G,H,I]). put([A,B,C,D,-,F,G,H,I],5,x,[A,B,C,D,x,F,G,H,I]). put([A,B,C,D,E,-,G,H,I],6,x,[A,B,C,D,E,x,G,H,I]). put([A,B,C,D,E,F,-,H,I],7,x,[A,B,C,D,E,F,x,H,I]). put([A,B,C,D,E,F,G,-,I],8,x,[A,B,C,D,E,F,G,x,I]). put([A,B,C,D,E,F,G,H,-],9,x,[A,B,C,D,E,F,G,H,x]).

% Put O put([-,B,C,D,E,F,G,H,I],1,o,[o,B,C,D,E,F,G,H,I]). put([A,-,C,D,E,F,G,H,I],2,o,[A,o,C,D,E,F,G,H,I]). put([A,B,-,D,E,F,G,H,I],3,o,[A,B,o,D,E,F,G,H,I]). put([A,B,C,-,E,F,G,H,I],4,o,[A,B,C,o,E,F,G,H,I]). put([A,B,C,D,-,F,G,H,I],5,o,[A,B,C,D,o,F,G,H,I]). put([A,B,C,D,E,-,G,H,I],6,o,[A,B,C,D,E,o,G,H,I]). put([A,B,C,D,E,F,-,H,I],7,o,[A,B,C,D,E,F,o,H,I]). put([A,B,C,D,E,F,G,-,I],8,o,[A,B,C,D,E,F,G,o,I]). put([A,B,C,D,E,F,G,H,-],9,o,[A,B,C,D,E,F,G,H,o]).

% Win conditions (for any player X or O) win([P,P,P,,,,,,],P). win([,,,P,P,P,,,],P). win([,,,,,,P,P,P],P). win([P,,,P,,,P,,],P). win([,P,,,P,,,P,],P). win([,,P,,,P,,,P],P). win([P,,,,P,,,,P],P). win([,,P,,P,,P,,],P).

% Game play:- B = [-,-,-,-,-,-,-,-,-], show(B),

% X move
write('X move: '), read(P1),
put(B,P1,x,B1), show(B1),

% O move
write('O move: '), read(P2),
put(B1,P2,o,B2), show(B2),

% X move
write('X move: '), read(P3),
put(B2,P3,x,B3), show(B3),

% O move
write('O move: '), read(P4),
put(B3,P4,o,B4), show(B4),

% X move
write('X move: '), read(P5),
put(B4,P5,x,B5), show(B5),

% Check winner (win(B5, W) -> write(W), write(' wins!'); write('No winner yet') ).

To run: play.

8 puzzle (Hill CLimbing)

% Start and goal states start(1/2/3/4/8/0/7/6/5). goal(1/2/3/4/5/6/7/8/0).

% Move definitions move(1/2/3/4/8/0/7/6/5, down, 1/2/3/4/8/5/7/6/0, 1). move(1/2/3/4/8/5/7/6/0, left, 1/2/3/4/8/5/7/0/6, 1). move(1/2/3/4/8/5/7/0/6, up, 1/2/3/4/0/5/7/8/6, 1). move(1/2/3/4/0/5/7/8/6, right, 1/2/3/4/5/0/7/8/6, 1). move(1/2/3/4/5/0/7/8/6, down, 1/2/3/4/5/6/7/8/0, 1).

solve:- start(SO), move(SO, M1, S1, C1), write('Move: '), write(M1), write(' -> '), write(S1), write(', Cost: '), write(C1), nl, move(S1, M2, S2, C2), C12 is C1 + C2, write('Move: '), write(M2), write(' -> '), write(S2), write(', Cost: '), write(C12) ,nl, move(S2, M3, S3, C3), C123 is C12+ C3, write('Move: '), write(M3), write(' -> '), write(S3), write(', Cost: '), write(C123), nl, move(S3, M4, S4, C4), C1234 is C123 + C4, write('Move: '), write(M4), write(' -> '), write(S4), write(', Cost: '), write(C1234), nl, move(S4, M5, S5, C5), TotalCost is C1234 + C5, write('Move: '), write(M5), write(' -> '), write(S5), write(', Cost: '), write(TotalCost), nl, goal(S5), write('Goal reached! Total Cost = '), write(TotalCost), nl.

To run: solve.

Perceptron

import numpy as np

class Perceptron: def init(self, learning_rate=0.01, n_iterations=100): self.learning_rate= learning_rate self.n_iterations = n_iterations self.weights = None self.bias = None

def fit(self, X, y) :
    n_samples, n_features = X.shape
    self.weights = np.zeros(n_features)
    self.bias = 0
    y_ = np.array([1 if i > 0 else 0 for i in y])
    for _ in range (self.n_iterations): 
        for idx, x_i in enumerate(X):
            linear_output = np.dot(x_i, self.weights) + self.bias 
            y_predicted = self.activation_function(linear_output)
            update = self.learning_rate * (y_[idx] - y_predicted) 
            self.weights += update * x_i 
            self.bias += update

def activation_function(self, x): 
    return np.where(x>=0, 1, 0)

def predict(self, X):
    linear_output = np.dot (X, self.weights) + self.bias
    y_predicted = self.activation_function (linear_output) 
    return y_predicted

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([0, 1, 1, 1])

perceptron = Perceptron(learning_rate=0.1, n_iterations=10) perceptron.fit(X, y)

predictions = perceptron.predict(X) print(predictions)

Gradient Descent

X = [ [0, 0], [0, 1], [1, 0], [1, 1] ]

y = [0, 0, 0, 1]

w1, w2 = 0.0, 0.0 b = 0.0 lr= 0.1 epochs = 10

for epoch in range(epochs): for i in range(len(X)): x1, x2 = X[i] z=w1x1+w2x2 + b y_pred = 1 if z >= 0 else 0 error = y[i] - y_pred w1 = w1 + lr * error * x1 w2 = w2 + lr* error * x2 b = b + lr * error

print("Weights:", w1, w2) print("Bias: ", b)

for i in range(len(X)): x1, x2 = X[i] z = w1x1+w2x2 + b y_pred = 1 if z >= 0 else 0 print(X[i], "->", y_pred)

Adaline

import numpy as np

class Adaline: def init(self, input_size, learning_rate=0.1, epochs=100): self.weights = np. zeros (input_size) self.bias = 0 self.learning_rate = learning_rate self.epochs = epochs

def activation (self, x):
    return x

def predict(self, X) :
    return self.activation (np.dot (X, self.weights) + self.bias)

def train (self, X, y):
    for epoch in range(self.epochs) :
        for i in range(len(X)):
            prediction = self.predict(X[i])
            error = y[i] - prediction
            self.weights += self.learning_rate * error * X[i]
            self.bias += self.learning_rate * error

def evaluate (self, X):
    return np.where(self.predict(X) >= 0.5, 1, 0) 

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([0, 0, 0, 1])

adaline = Adaline(input_size=2, learning_rate=0.1, epochs=100)

adaline.train(X, y)

predictions = adaline.evaluate(X)

print("Predictions on the AND operation: ") for i, prediction in enumerate(predictions): print(f"Input: {X[i]} => Predicted: {prediction} => Actual: {y[i]}")

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2026 Microsoft