중첩 최적화에서 솔루션을 반환하는 방법은 무엇입니까? (How to return solutions in a nested optimization?)


문제 설명

중첩 최적화에서 솔루션을 반환하는 방법은 무엇입니까? (How to return solutions in a nested optimization?)

다음과 같은 문제가 있습니다. 여러 차원에서 최적화 문제를 해결해야 합니다. 그러나 문제는 어떤 의미에서 좋습니다. 문제를 외부와 내부 최적화로 분할하여 차원을 줄일 수 있습니다. 내부 최적화는 매우 효율적으로 풀 수 있습니다(선형 대수). 코드가 다소 깁니다. 이러한 이유로 저는 장난감 모델을 만들었습니다.

out.obj <‑ function(x,storage){
  inner.sol <‑ inner.obj(param = x)
  storage <‑ append(storage,inner.sol)
  return((x‑inner.sol)^2)
}

inner.obj <‑ function(x = NULL,param){
  return(log(param)^2‑param)
}
storage <‑ c()
sol <‑ optim(par = c(3),fn = out.obj,storage = storage)

이 예는 수학적 관점에서 별로 의미가 없습니다. 그러나 내부 솔루션 추출에 관한 내 문제를 보여줍니다. 위의 코드에는 x1,x2라는 2dim 문제가 있습니다. 보시다시피 저는 외부 목적 함수에 대해 optim을 호출합니다. x1에 대한 값이 주어지면 x2에 대해 "해결"할 수 있습니다.

마지막으로 저는 x1과 x2에 대한 두 가지 최적 값을 갖고 싶습니다. 이를 위해 매개변수 저장소를 사용했습니다. 그러나 저장소는 로컬 버전을 가져오고 x2에 대한 최적의 값을 검색할 수 없습니다. 나는 또한 다음을 시도했다:

    storage <‑ c()
    out.obj <‑ function(x){
      inner.sol <‑ inner.obj(param = x)
      storage <‑ append(storage,inner.sol)
      return((x‑inner.sol)^2)
    }

    inner.obj <‑ function(x = NULL,param){
      return(log(param)^2‑param)
    }

    sol <‑ optim(par = c(3),fn = out.obj)

In this case storage should be globally defined. Still the values for x2 are not getting populated. How can I get this value?


**EDIT**

f2 <‑ function(y=NULL){
  storage <‑ c()
sol <‑ optim(par = c(3),fn = out.obj)
return(list("sol" = sol,"storage"=storage))
}

f1 <‑ function(p=NULL){
  temp <‑ f2(NULL)
  return(temp)
}

out.obj <‑ function(x){
  inner.sol <‑ inner.obj(param = x)
  storage <<‑ append(storage,inner.sol)
  return((x‑inner.sol)^2)
}

inner.obj <‑ function(x = NULL,param){
  return(log(param)^2‑param)
}

참조 솔루션

방법 1:

Use <<‑

storage <<‑ append(storage,inner.sol)

or assign :

assign("storage", append(storage, inner.sol), .GlobalEnv))

Regarding modified problem where storage is not in the global environment pass the environment that storage is in to out.obj as shown:

f2 <‑ function(y=NULL){
  storage <‑ c()
  sol <‑ optim(par = c(3),fn = out.obj, e = environment())
  return(list("sol" = sol,"storage"=storage))
}

out.obj <‑ function(x, e){
  inner.sol <‑ inner.obj(param = x)
  assign("storage", append(e$storage, inner.sol), e)
  return((x‑inner.sol)^2)
}

The assign line could alternatively be written as:

 e$storage <‑ append(e$storage, inner.sol)

(by mathG. Grothendieck)

참조 문서

  1. How to return solutions in a nested optimization? (CC BY‑SA 2.5/3.0/4.0)

#optimization #R






관련 질문

세 개의 작은 숫자를 하나의 두 배에 저장하는 방법은 무엇입니까? (How to store three small numbers into one double?)

중첩 최적화에서 솔루션을 반환하는 방법은 무엇입니까? (How to return solutions in a nested optimization?)

C에서 "signed int"가 "unsigned int"보다 빠른 이유는 무엇입니까? (In C, why is "signed int" faster than "unsigned int"?)

값이 없는 경우 HTML5 로컬 저장소 개체는 무엇을 반환합니까? (What does the HTML5 Local Storage object return if there is no value?)

200K 게시물이 있는 Wordpress 사이트는 JOIN이 느린 SQL_CALC_FOUND_ROWS를 게시합니까? 속도 최적화? (Wordpress site with 200K posts SQL_CALC_FOUND_ROWS with JOINs slow? Optimize for speed?)

xlx의 각 시트에서 특정 셀을 추출하기 위해 이 R 코드를 개선하는 데 도움이 필요합니다. (I need help to improve this R code to extract specific cell from each sheet in xlxs)

이 for 루프에서 fminunc가 매번 동일한 결과를 제공하는 이유는 무엇입니까? (Why is fminunc in this for loop giving the same result each time?)

정수 스칼라 배열만 Hyperopt에서 스칼라 인덱스 오류로 변환될 수 있습니다. (only integer scalar arrays can be converted to a scalar index error in Hyperopt)

벡터 병합 알고리즘 도움말 (Help with algorithm for merging vectors)

이 MIPS 프로그램이 이렇게 오래 걸리나요? (파이에 가까운 프로그램) (Is this MIPS program supposed to take this long? (program that approximates pi))

힘을 최소화하기 위한 최적의 궤적, 최종 조건 문제 (Optimal trajectory to minimize force, issues with final conditions)

단일 svg 요소를 만들기 위해 여러 모양 병합 (Merging number of shapes to make a single svg element)







코멘트