package main
import (
"bufio"
"fmt"
"log"
"math"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
// Constants for field indices (0-based)
const (
FieldsCount = 147
A1 = 0
A2 = 1
A3 = 2
A4 = 3
A5 = 4
// ... (for commonly referenced ones define const for better readability, else just use int)
)
// Helper: floating-point equality tolerance
func floatEquals(a, b float64) bool {
return math.Abs(a-b) < 1e-9
}
// Parses one line from [ca/:A1,A2,...] into a float64 slice
func parseLine(line string) ([]float64, error) {
if !strings.HasPrefix(line, "[ca/:") || !strings.HasSuffix(line, "]") {
return nil, fmt.Errorf("invalid line format")
}
content := line[5:len(line)-1]
parts := strings.Split(content, ",")
if len(parts) < FieldsCount {
return nil, fmt.Errorf("not enough fields: %d", len(parts))
}
fields := make([]float64, FieldsCount)
for i, p := range parts[:FieldsCount] {
v, err := strconv.ParseFloat(p, 64)
if err != nil {
return nil, fmt.Errorf("parse float error at field %d: %v", i+1, err)
}
fields[i] = v
}
return fields, nil
}
// Formats fields slice back to line string
func formatLine(fields []float64) string {
parts := make([]string, len(fields))
for i, v := range fields {
if floatEquals(v, float64(int64(v))) {
parts[i] = strconv.FormatInt(int64(v), 10)
} else {
parts[i] = fmt.Sprintf("%.5g", v)
}
}
return "[ca/:" + strings.Join(parts, ",") + "]"
}
// Reads file, returns slice of line offsets and (A2,A3) values and maxID
// We store offsets so we can seek and read lines on demand, not full contents
func scanFileForParents(path string) (parent1Offsets []int64, parent2Offsets []int64, maxID int64, err error) {
f, err := os.Open(path)
if err != nil {
return nil, nil, 0, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
offset := int64(0)
for scanner.Scan() {
line := scanner.Text()
fields, err := parseLine(line)
if err != nil {
return nil, nil, 0, err
}
id := int64(fields[A1])
if id > maxID {
maxID = id
}
a2 := fields[A2]
a3 := fields[A3]
if floatEquals(a2, 1) && floatEquals(a3, 1) {
parent1Offsets = append(parent1Offsets, offset)
} else {
parent2Offsets = append(parent2Offsets, offset)
}
offset += int64(len(line)) + 1 // +1 for newline
}
if err := scanner.Err(); err != nil {
return nil, nil, 0, err
}
return parent1Offsets, parent2Offsets, maxID, nil
}
// Reads one line at offset (assumes lines end with \n)
func readLineAtOffset(f *os.File, offset int64) ([]float64, error) {
_, err := f.Seek(offset, 0)
if err != nil {
return nil, err
}
reader := bufio.NewReader(f)
line, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
return parseLine(strings.TrimRight(line, "\r\n"))
}
// Helper for random choose 1 element from slices of float64
func choice(s []float64) float64 {
return s[rand.Intn(len(s))]
}
// Helper for choosing randomly between two options (parent1 or parent2)
func pickParent1or2(p1Val, p2Val float64) float64 {
if rand.Intn(2) == 0 {
return p1Val
}
return p2Val
}
// Helper for picking randomly all fields from p1 or p2 in a range [from,to] inclusive (indices 0-based)
func pickRangeFields(fields [][]float64, p1, p2 int, from, to int) []float64 {
n := to - from +1
out := make([]float64, n)
source := fields[p1]
if rand.Intn(2) == 0 {
source = fields[p2]
}
for i := 0; i < n; i++ {
out[i] = source[from+i]
}
return out
}
// Helper clamp to 0-255 ints
func clampInt(i int) int {
if i < 0 {
return 0
} else if i > 255 {
return 255
}
return i
}
// Create offspring fields per rules, given parents p1 and p2 fields, new ID
func createOffspring(p1, p2 []float64, newID int64) []float64 {
offspring := make([]float64, FieldsCount)
offspring[A1] = float64(newID)
// Rule 2
a2Candidates := []float64{p1[A2], p1[A3], p2[A2], p2[A3]}
a2 := choice(a2Candidates)
offspring[A2] = a2
if a2 == p1[A2] || a2 == p1[A3] {
offspring[A3] = choice([]float64{p2[A2], p2[A3]})
} else {
offspring[A3] = choice([]float64{p1[A2], p1[A3]})
}
// Rule 3
a4Candidates := []float64{p1[A4], p1[A5], p2[A4], p2[A5]}
a4 := choice(a4Candidates)
offspring[A4] = a4
if a4 == p1[A4] || a4 == p1[A5] {
offspring[A5] = choice([]float64{p2[A4], p2[A5]})
} else {
offspring[A5] = choice([]float64{p1[A4], p1[A5]})
}
// Rule 4: A6..A9 from p1 or p2 + random offset
source := p1
if rand.Intn(2) == 1 {
source = p2
}
for i := A6; i <= A9; i++ {
base := int(source[i])
offset := rand.Intn(21) - 10
offspring[i] = float64(clampInt(base + offset))
}
// Rule 5: A10..A12 pick all from p1 or p2 randomly
if rand.Intn(2) == 0 {
copy(offspring[10:13], p1[10:13])
} else {
copy(offspring[10:13], p2[10:13])
}
// Rule 6 A13
a2a311 := floatEquals(offspring[A2], 1) && floatEquals(offspring[A3], 1)
a2a322 := floatEquals(offspring[A2], 2) && floatEquals(offspring[A3], 2)
if a2a311 || a2a322 {
offspring[12] = float64(rand.Intn(26)+25) / 100 // 0.25 to 0.5
} else {
offspring[12] = 1
}
// Rule 7: A14..A16 pick parent1 or 2
if rand.Intn(2) == 0 {
copy(offspring[13:16], p1[13:16])
} else {
copy(offspring[13:16], p2[13:16])
}
// Rule 8: A17 alike A13
if a2a311 || a2a322 {
offspring[16] = float64(rand.Intn(26)+25) / 100
} else {
offspring[16] = 1
}
// Rule 9: A18..A20 pick parent1 or 2
if rand.Intn(2) == 0 {
copy(offspring[17:20], p1[17:20])
} else {
copy(offspring[17:20], p2[17:20])
}
// Rule 10: A21
if a2a311 {
offspring[20] = float64(rand.Intn(26)+25) / 100
} else {
offspring[20] = 0
}
// Rules 11-16 repeated, e.g., A22-24, A25, A26-28, A29, etc.
copyConditionalFields := []struct {
from, to int
condition bool
trueVal, falseVal float64
}{
{21, 23, false, 0, 0}, // Rule 11: A22-A24 pick parent
{24, 24, a2a311 || a2a322, 0, 1}, // Rule12: A25
{25, 27, false, 0, 0}, // Rule 13: A26-A28 pick parent
{28, 28, a2a311 || a2a322, 0, 1}, // Rule 14: A29
{29, 31, false, 0, 0}, // Rule 15: A30-A32 pick parent
{32, 32, a2a311, 0, 1}, // Rule 16: A33
}
// For ranges without conditional value (condition==false), pick randomly from parents else set trueVal or falseVal
for _, rule := range copyConditionalFields {
if rule.condition {
for i := rule.from; i <= rule.to; i++ {
offspring[i] = rule.trueVal
}
} else if rule.trueVal == 0 && rule.falseVal == 0 {
// pick parent
if rand.Intn(2) == 0 {
copy(offspring[rule.from:rule.to+1], p1[rule.from:rule.to+1])
} else {
copy(offspring[rule.from:rule.to+1], p2[rule.from:rule.to+1])
}
} else {
for i := rule.from; i <= rule.to; i++ {
offspring[i] = rule.falseVal // default fallback if condition false and has falseVal
}
}
}
// Rule 17: A34-39 pick parent independently, then change 1-3 randomly to 1-10 different from old
for i := 33; i <= 38; i++ {
if rand.Intn(2) == 0 {
offspring[i] = p1[i]
} else {
offspring[i] = p2[i]
}
}
// Randomly change 1 to 3 distinct indices in A34-A39 (indices 33..38)
toChangeCount := rand.Intn(3) + 1
indices := rand.Perm(6)[:toChangeCount]
for _, idx := range indices {
pos := 33 + idx
old := offspring[pos]
newVal := float64(rand.Intn(10) + 1)
for floatEquals(newVal, old) {
newVal = float64(rand.Intn(10) + 1)
}
offspring[pos] = newVal
}
// Rule 18,19: A40,A41 = random 1-10
offspring[39] = float64(rand.Intn(10) + 1)
offspring[40] = float64(rand.Intn(10) + 1)
// Rule 20: A42 rules
a4val := int(offspring[A4])
a2val := int(offspring[A2])
a3val := int(offspring[A3])
var a42val float64
switch {
case a2val == 1 && a3val == 1 && a4val == 1:
a42val = 1
case a2val == 1 && a3val == 1 && a4val == 2:
a42val = 2
case a2val == 1 && a3val == 1 && a4val == 3:
a42val = 3
case a2val == 2 && a3val == 2 && a4val == 1:
a42val = 4
case a2val == 2 && a3val == 2 && a4val == 2:
a42val = 5
case a2val == 2 && a3val == 2 && a4val == 3:
a42val = 6
case (a2val == 1 && a3val == 2 && a4val == 1) || (a2val == 2 && a3val == 1 && a4val == 1):
a42val = 7
case (a2val == 1 && a3val == 2 && a4val == 2) || (a2val == 2 && a3val == 1 && a4val == 2):
a42val = 8
case (a2val == 1 && a3val == 2 && a4val == 3) || (a2val == 2 && a3val == 1 && a4val == 3):
a42val = 9
default:
a42val = 0
}
offspring[41] = a42val
// Rule 21-22: A43, A44
A147 := p1[146]
if a2a311 {
offspring[42] = A147
offspring[43] = A147
} else {
offspring[42] = 404
offspring[43] = 404
}
// Rule 23: A45
if a2a311 || a2a322 {
offspring[44] = 404
} else {
offspring[44] = A147
}
// Rule 24: A46
if a2a311 {
offspring[45] = 404
} else {
offspring[45] = A147
}
// Rule 25: A47..A146 sum parent values
for i := 46; i <= 145; i++ {
offspring[i] = p1[i] + p2[i]
}
// Rule 26: Find max in A47..A146, set A147 = max index-46
maxIdx := 46
maxVal := offspring[46]
for i := 47; i <= 145; i++ {
if offspring[i] > maxVal {
maxVal = offspring[i]
maxIdx = i
}
}
offspring[146] = float64(maxIdx - 46)
return offspring
}
func main() {
rand.Seed(time.Now().UnixNano())
file := "data.txt"
// Step 1: Scan file offsets of parents (store minimal info to be memory-friendly)
parent1Offsets, parent2Offsets, maxID, err := scanFileForParents(file)
if err != nil {
log.Fatalf("Failed scanning file: %v", err)
}
if len(parent1Offsets) == 0 || len(parent2Offsets) == 0 {
log.Fatal("No suitable parents found")
}
// Pick random offsets:
offset1 := parent1Offsets[rand.Intn(len(parent1Offsets))]
offset2 := parent2Offsets[rand.Intn(len(parent2Offsets))]
// Open file for reading parents
f, err := os.Open(file)
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer f.Close()
p1, err := readLineAtOffset(f, offset1)
if err != nil {
log.Fatalf("Failed to read parent1 at offset %d: %v", offset1, err)
}
p2, err := readLineAtOffset(f, offset2)
if err != nil {
log.Fatalf("Failed to read parent2 at offset %d: %v", offset2, err)
}
offspring := createOffspring(p1, p2, maxID+1)
// Append offspring to file
outf, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Failed opening file to append: %v", err)
}
defer outf.Close()
line := formatLine(offspring)
_, err = outf.WriteString(line + "\n")
if err != nil {
log.Fatalf("Failed to write offspring: %v", err)
}
fmt.Println("Offspring created and appended:")
fmt.Println(line)
}
Can you make // Constants for field indices (0-based)
const (
FieldsCount = 147
A1 = 0
A2 = 1
A3 = 2
A4 = 3
A5 = 4
// ... (for commonly referenced ones define const for better readability, else just use int)
) shorter and smarter?