#оптимизационные функции targetVector <- c() fitness <- function(solution){ return(-sum(targetVector == round(solution))) } library(compiler) fitnessCmp <- cmpfun(fitness) DiscrOptimizer <- function(fitness, minValue, maxValue, targetLength, fitnessCallLimit){ testVector <- rep(minValue, targetLength) fitnessAccessCounter <- 0 prevBestScore <- Inf bestSolution <- testVector for(i in 1:targetLength){ testVector <- bestSolution for(j in minValue:maxValue){ testVector[i] <- j fitnessAccessCounter <- fitnessAccessCounter + 1 score <- fitnessCmp(testVector) if(score < prevBestScore){ bestSolution <- testVector prevBestScore <- score if(i!=1 && j!=1){ break } } if(fitnessAccessCounter >= fitnessCallLimit) break } if(fitnessAccessCounter >= fitnessCallLimit) break } return(list(solution = bestSolution, n_calls = fitnessAccessCounter)) } DiscrOptimizerCmp <- cmpfun(DiscrOptimizer) #входные данные key <- c("A","a","B","b","C","c","D","d","E","e","F","f","G","g","H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v","W","w","X","x","Y","y","Z","z") test1 <- "SptstmLEjXhdESqotbRQbmsYKkJTZiOHBEenIbGqLHwxqfWTok" test2 <- "HXWSXpsaYbViFIQUlKYbpCiZfBdyHGKxNLFrigWMOlDjzpFlLHOKONHlkxILqfpmdHHoTwMfCwzqLELqceuLeHtrdpvjBxfdzDHzmopkgKPXKnYWItcjFRFsqpOQdQRQZNSTNtCwvxicircFznboUJ" test3 <- "QbgpsGPELKivNySHxdBjAJKudnJtvQDXtBlfKQBuUjFyovbSejUiPaByUjmMWHEGIHRqDCHbMDPdnRRcEMuXzCoBvifSQAdcbopwGBiDbECxbrzJLZTkOYdWdEmUhnelievPauljpmAmvbUnDuBVPvejBBBTbmqLatRbuPgjUMNSQXBVviWULZjslNLgFKfINJFGzOOusXnBDeCpthgOmlTEUPxQyrUOTKhtySNODxsqEtLjPvxceGodZRqWqspsEUQXeaNPeEQJNmaVOoMzYGjfONlPPTEpUtqPbigcrPlpCBxUKLFnHgAENIzDExGCcBoKdkTKXQgorapGkuGPrzPWjsSjThwffRjOYxFyQCWZhytILMltScNKgSuZUVlgRdWyaLFLGEwHVHJiuqOgxblyTshFTLPOpJQFZKNpPmSPghRsKxZyYIjxzoxyCWEqRpylkGHLccmQoGGdqHyLkfNVrMHViujGBpBpzFWASkeuvHXINEnVyAKIzCRabWrELmTEknYoWHNFDkteLimAJVvgrlgMvnwkCTthjHMPPxZNmqFulCkmHSxqaAXRHfYetIpvQmQPkBxDcVXlCzqLmqzQUpIWBgfTuNmIYbyhKqJBcrVmkDJRuNsNIOYXUVHPaclvEVgqDGeZrYemBXMQBBaTMlBHfCZJUfIGCmOQsrzHcEmdcermCYDyqkQEcBnUzkrypSGMwGgXoUoIgfmatuMYTxnKeqiWcqxcQaxNtPDOkgMAsRdRBcXQHvIMHTDrBHFlCvngcvpnXWoLFPSnvGLbEmJNFcwMhbllYogKDmltLUNwymaJenKnYOVhviXqYFkEFkoTQlFxJEeWUFRoBeLLrdQICasUmgOiWoGgxZegCZVAaBCGmhkhYtmqWqGPBGFJBcHcWWmSCFwDmrkCPxnWvJxbDoNYFNKhtzanuEawUpeEPgpmLZGZaFezWCZwExZWfDbBrLlaTyrGGfLMehhrqbImcjiPOQPKmgIF" key_numeric <- 1:length(key) test1_numeric <- c() for(i in 1:nchar(test1)){ test1_numeric[i] <- which(key == substr(test1, i, i)) } test2_numeric <- c() for(i in 1:nchar(test2)){ test2_numeric[i] <- which(key == substr(test2, i, i)) } test3_numeric <- c() for(i in 1:nchar(test3)){ test3_numeric[i] <- which(key == substr(test3, i, i)) } testRepeats <- 20 #выполнение теста startTime <- Sys.time() for (i in 1:testRepeats){ targetVector <- test1_numeric result1 <- DiscrOptimizerCmp(fitness = fitnessCmp, minValue = 1, maxValue = length(key_numeric), targetLength = length(targetVector), fitnessCallLimit = 20000) } executionTime <- (Sys.time() - startTime)/testRepeats #вывод данных cat("Accuracy: ", mean(targetVector == result1$solution), "; fitness calls: ", result1$n_calls, "; average execution time: ", executionTime, " s\n", sep="") #cat("result text: ", paste(key[result1$solution], collapse=""), "\n") #cat("original text: ", test1, "\n") #comparedText <- key[result1$solution] #comparedText[result1$solution != targetVector] <- "_" #cat("compared text: ", paste(comparedText, collapse=""), "\n") startTime <- Sys.time() for (i in 1:testRepeats){ targetVector <- test2_numeric result2 <- DiscrOptimizerCmp(fitness = fitnessCmp, minValue = 1, maxValue = length(key_numeric), targetLength = length(targetVector), fitnessCallLimit = 20000) } executionTime <- (Sys.time() - startTime)/testRepeats #вывод данных cat("Accuracy: ", mean(targetVector == result2$solution), "; fitness calls: ", result2$n_calls, "; average execution time: ", executionTime, " s\n", sep="") #cat("result text: ", paste(key[result2$solution], collapse=""), "\n") #cat("original text: ", test2, "\n") #comparedText <- key[result2$solution] #comparedText[result2$solution != targetVector] <- "_" #cat("compared text: ", paste(comparedText, collapse=""), "\n") startTime <- Sys.time() for (i in 1:testRepeats){ targetVector <- test3_numeric result3 <- DiscrOptimizerCmp(fitness = fitnessCmp, minValue = 1, maxValue = length(key_numeric), targetLength = length(targetVector), fitnessCallLimit = 20000) } executionTime <- (Sys.time() - startTime)/testRepeats #вывод данных cat("Accuracy: ", mean(targetVector == result3$solution), "; fitness calls: ", result3$n_calls, "; average execution time: ", executionTime, " s\n", sep="") #cat("result text: ", paste(key[result3$solution], collapse=""), "\n") #cat("original text: ", test3, "\n") #comparedText <- key[result3$solution] #comparedText[result3$solution != targetVector] <- "_" #cat("compared text: ", paste(comparedText, collapse=""), "\n") startTime <- Sys.time() for (i in 1:testRepeats){ targetVector <- test1_numeric result1 <- DiscrOptimizerCmp(fitness = fitnessCmp, minValue = 1, maxValue = length(key_numeric), targetLength = length(targetVector), fitnessCallLimit = Inf) } executionTime <- (Sys.time() - startTime)/testRepeats #вывод данных cat("Accuracy: ", mean(targetVector == result1$solution), "; fitness calls: ", result1$n_calls, "; average execution time: ", executionTime, " s\n", sep="") #cat("result text: ", paste(key[result1$solution], collapse=""), "\n") #cat("original text: ", test1, "\n") #comparedText <- key[result1$solution] #comparedText[result1$solution != targetVector] <- "_" #cat("compared text: ", paste(comparedText, collapse=""), "\n") startTime <- Sys.time() for (i in 1:testRepeats){ targetVector <- test2_numeric result2 <- DiscrOptimizerCmp(fitness = fitnessCmp, minValue = 1, maxValue = length(key_numeric), targetLength = length(targetVector), fitnessCallLimit = Inf) } executionTime <- (Sys.time() - startTime)/testRepeats #вывод данных cat("Accuracy: ", mean(targetVector == result2$solution), "; fitness calls: ", result2$n_calls, "; average execution time: ", executionTime, " s\n", sep="") #cat("result text: ", paste(key[result2$solution], collapse=""), "\n") #cat("original text: ", test2, "\n") #comparedText <- key[result2$solution] #comparedText[result2$solution != targetVector] <- "_" #cat("compared text: ", paste(comparedText, collapse=""), "\n") startTime <- Sys.time() for (i in 1:testRepeats){ targetVector <- test3_numeric result3 <- DiscrOptimizerCmp(fitness = fitnessCmp, minValue = 1, maxValue = length(key_numeric), targetLength = length(targetVector), fitnessCallLimit = Inf) } executionTime <- (Sys.time() - startTime)/testRepeats #вывод данных cat("Accuracy: ", mean(targetVector == result3$solution), "; fitness calls: ", result3$n_calls, "; average execution time: ", executionTime, " s\n", sep="") #cat("result text: ", paste(key[result3$solution], collapse=""), "\n") #cat("original text: ", test3, "\n") #comparedText <- key[result3$solution] #comparedText[result3$solution != targetVector] <- "_" #cat("compared text: ", paste(comparedText, collapse=""), "\n")