Table of Contents

Class Program<T>

Namespace
AiDotNet.ProgramSynthesis.Models
Assembly
AiDotNet.dll

Represents a synthesized program with its source code and metadata.

public class Program<T>

Type Parameters

T

The numeric type used for calculations (e.g., double, float).

Inheritance
Program<T>
Inherited Members

Remarks

The Program class encapsulates a synthesized or analyzed program, including its source code, the programming language it's written in, validation status, and optional execution metrics.

For Beginners: This class represents a computer program created by AI.

Think of this as a container that holds:

  • The actual code (like a recipe holds instructions)
  • What language it's written in (Python, Java, etc.)
  • Whether the code is valid and will run
  • How well it performs
  • An optional numerical representation that AI can work with

Just like a recipe card has the recipe, cooking time, and difficulty level, this class holds a program and all its important information.

Constructors

Program()

Initializes a new instance of the Program<T> class with default values.

public Program()

Remarks

Creates an empty Program instance. Useful when the program will be populated later or when deserializing.

For Beginners: This creates an empty program instance.

Sometimes you need to create a Program object before you have all the information. This creates an empty one that you can fill in later, like having a blank form to fill out gradually.

Program(string, ProgramLanguage, bool, double, int)

Initializes a new instance of the Program<T> class.

public Program(string sourceCode, ProgramLanguage language, bool isValid = false, double fitnessScore = 0, int complexity = 0)

Parameters

sourceCode string

The source code of the program.

language ProgramLanguage

The programming language.

isValid bool

Whether the program is valid.

fitnessScore double

The fitness score (default is 0.0).

complexity int

The complexity measure (default is 0).

Remarks

Creates a new Program instance with the specified source code and metadata. This constructor is typically used when creating a synthesized program.

For Beginners: This creates a new program object.

When the AI generates or processes code, it creates a Program object to store all the information. You need to provide:

  • The actual code (required)
  • What language it's in (required)
  • Whether it's valid (required)
  • Optional: fitness score and complexity

Think of it like filling out a form with all the program's details.

Properties

Complexity

Gets or sets the complexity measure of the program.

public int Complexity { get; set; }

Property Value

int

Remarks

A metric indicating the complexity of the program, which could be based on various factors like number of statements, cyclomatic complexity, or abstract syntax tree size.

For Beginners: This measures how complicated the program is.

Just like recipes can be simple (toast) or complex (soufflé), programs have different complexity levels. This number tells you:

  • Low values: Simple, short programs that are easy to understand
  • High values: Complex, longer programs with many steps

Usually, simpler programs (lower complexity) are better when they solve the same problem.

Encoding

Gets or sets the encoded representation of the program.

public Tensor<T>? Encoding { get; set; }

Property Value

Tensor<T>

Remarks

An optional numerical encoding of the program that can be used by neural networks for further processing or refinement.

For Beginners: This is a numerical version of the code for AI to work with.

Computers and AI work better with numbers than text. This is the program converted into a numerical form that AI can easily process, like converting a photo into pixels. The original code is still in SourceCode - this is just an alternative representation for computation.

ErrorMessage

Gets or sets any error messages from compilation or execution attempts.

public string? ErrorMessage { get; set; }

Property Value

string

Remarks

If the program failed validation or execution, this contains the error messages explaining what went wrong.

For Beginners: This explains what's wrong if the program doesn't work.

When code has problems, we need to know why. This stores error messages like:

  • "Syntax error on line 5: missing semicolon"
  • "Variable 'x' not defined"

These help debug and fix the program, like having someone point out exactly what's wrong with a recipe.

ExecutionTimeMs

Gets or sets execution time in milliseconds if the program was executed.

public double? ExecutionTimeMs { get; set; }

Property Value

double?

Remarks

Records how long the program took to execute, which can be useful for performance comparison and optimization.

For Beginners: This is how long the program takes to run.

Measured in milliseconds (1000 milliseconds = 1 second). Helps answer:

  • Is this program fast or slow?
  • Which of two programs is faster?

Lower execution time is usually better - it means the program finishes faster.

FitnessScore

Gets or sets the fitness score of the program.

public double FitnessScore { get; set; }

Property Value

double

Remarks

A value between 0 and 1 indicating how well the program satisfies the synthesis requirements. Higher values indicate better performance. 1.0 means perfect, 0.0 means complete failure.

For Beginners: This is like a grade showing how well the program works.

Think of it as a score from 0% to 100%:

  • 1.0 (100%): Perfect! Passes all tests
  • 0.75 (75%): Pretty good, passes most tests
  • 0.5 (50%): Mediocre, passes half the tests
  • 0.0 (0%): Doesn't work at all

Higher scores mean the program better solves the problem you gave it.

IsValid

Gets or sets a value indicating whether the program is syntactically and semantically valid.

public bool IsValid { get; set; }

Property Value

bool

Remarks

Indicates whether the program passes validation checks, including syntax correctness and semantic validity. A valid program can potentially be executed.

For Beginners: This tells you if the code is correct and will run.

Like checking a recipe for mistakes before cooking:

  • Are all ingredients listed? (syntax)
  • Do the instructions make sense? (semantics)
  • Will following this recipe actually work? (validity)

If IsValid is true, the code should run without errors.

Language

Gets or sets the programming language of the program.

public ProgramLanguage Language { get; set; }

Property Value

ProgramLanguage

Remarks

Specifies which programming language the source code is written in. This affects how the code should be interpreted, compiled, or executed.

For Beginners: This tells you which programming language was used.

Just like knowing whether a recipe is in English or French, this tells you whether the code is in Python, Java, C#, etc. Different languages have different rules and syntax.

SourceCode

Gets or sets the source code of the program.

public string SourceCode { get; set; }

Property Value

string

Remarks

The actual program text in the target programming language. This is the human-readable code that can be executed or compiled.

For Beginners: This is the actual code - the instructions the computer will follow.

Just like a recipe has step-by-step cooking instructions, this contains the step-by-step commands that tell the computer what to do.

Methods

ToString()

Returns a string representation of the program.

public override string ToString()

Returns

string

A string containing the source code.

Remarks

Provides a string representation of the Program for display purposes.

For Beginners: This converts the program to a readable string.

When you need to display or print the program, this method returns the source code as a string. Useful for debugging and logging.